Write a function that takes two arrays and their size as inputs, and calculates
ID: 3818895 • Letter: W
Question
Write a function that takes two arrays and their size as inputs, and calculates their inner product (note that both arrays must have the same size so only one argument is needed to specify their size). The inner product of two arrays A and B with N elements is a scalar value c defined as follows: c = A middot B = sigma^N-1_i=0 A(i)B(i) = A(0)B(0) + A(1)B(1) +. .. + S(N - 1)B(N - 1) where A(i) and B(i) are the ith elements of arrays A and B, respectively. For example, the inner product of A = (1, 2) and B = (3, 3) is c_1 = 9; and the inner product of C = (2, 5, 4, -2, 1) and D = (3, 4, 2, 0, 2) is c2 = 36. You must use the following function prototype: int innerProduct(int A[], int B[], int size);Explanation / Answer
/*Contents of libvector.h*/
int innerProduct(int *a, int *b, int size);
************************************************************
/*Contents of libvector.c*/
#include <stdio.h>
#include "libvector.h"
int innerProduct(int *a, int *b, int size){
int i, ans = 0;
for (i = 0; i < size; i++)
ans += a[i] * b[i];
return ans;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.