Finish the function inner_product below. The function should return a[0] * b[0] + a[1] * b[1] + ... + a[n-1] * b[n-1]:
寫inner_product函數。函數會return a[0] * b[0] + a[1] * b[1] + ... + a[n-1] * b[n-1]:
#include <stdio.h>
double inner_product(double a[],double b[],int n)
{
/*INSERT YOUR CODE HERE*/
/*END OF YOUR CODE*/
//The function should return a[0] * b[0] + a[1] * b[1] + ... + a[n-1] * b[n-1].
}
int main(void)
{
double arrayA[100], arrayB[100];
int c, n;
scanf("%d", &n); //Enter number of elements in array
for ( c = 0 ; c < n ; c++ ) //Enter array A
scanf("%lf", &arrayA[c]);
for ( c = 0 ; c < n ; c++ ) //Enter array B
scanf("%lf", &arrayB[c]);
printf("%g",inner_product(arrayA,arrayB,n));
return 0;
}
Example input:
The first input is the number of elements in arrays (3); The array values of A and B follow;
第一行是陣列大小,第二行A元素,第三行B元素
3
4 7 8
1 2 3
Example output
42
Remember: You may correct the cases, but your code always be revised!