Question
Q3) Write a function that takes two arrays and their size as inputs, and calculates their inner product (note that both arrays must have the
Q3) 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:
N1 c=AB= A(i)B(i)=A(0)B(0)+A(1)B(1)++A(N1)B(N1),
i=0
where A(i) and B(i) are the ith elements of arrays A and B, respectively. For example, theinnerproductofA=(1,2)andB=(3,3)isc1 =9;andtheinnerproductofC= (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);
Note: You must write your function prototype in a header file named libvector.h and you must write your function definition in a source file named libvector.c. We are providing you the main source file vector.c, which you can use to test your function. Do not change anything in vector.c.
#include
int main() { int* array1; int* array2; int n, c, result;
printf("Enter number of elements: "); scanf("%d", &n);
/* dynamic memory allocation */ array1 = (int*) malloc(n * sizeof(int)); array2 = (int*) malloc(n * sizeof(int));
printf("Enter the %d elements of vector A: ", n);
for (c = 0; c < n; c++) scanf("%d", &array1[c]);
printf("Enter the %d elements of vector B: ", n);
for (c = 0; c < n; c++) scanf("%d", &array2[c]);
result = innerProduct(array1, array2, n);
printf("The inner product of vectors A and B is: %d. ", result);
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started