I\'m having major trouble writing the code for this. Every video that would teac
ID: 3588491 • Letter: I
Question
I'm having major trouble writing the code for this. Every video that would teach me online is in a foreign language. Could anyone help me please, and please explain what parts of the function do what? I'm hopelessly lost!
Write a program that calls two functions in the main program namely “inner_product” and “inner_product_reverse”.
The first function is declared as: int inner_product(int a[], int b[], int n);
The function should return a[0]*b[0] + a[1]*b[1] + ... +a[n-1] *b[n-1].
The second function is declared as: int inner_product_reverse(int a[], int b[], int n);
The function should return a[0]*b[n-1] + a[1]*b[n-2] + ... +a[n-1] *b[0].
Note: Let us assume that both arrays “a” and “b” are of size 10: a[0]-a[9] and b[0]-b[9]
Enter the first array of size 10: 10 1 4 -16 0 12 13 -17 90 0
Enter the second array of size 10: 0 0 0 -2 9 12 -3 4 0 -40
Inner product is: 69 Inner product reverse is: -254
Explanation / Answer
The first function is declared as: int inner_product(int a[], int b[], int n);
It means the function name is inner_product and the parameters passed are one integer array a(int a[]),one integer array b(int b[]), and one integer n.
We are passing the two arrays containig the elements taken from user input, to the functions with the number of elements n.
The function should return a[0]*b[0] + a[1]*b[1] + ... +a[n-1] *b[n-1].
It means the function will return the sum of elements in the array a nd b after multiplying the corresponding contents of array a and b. i.e a[0] * b[0] means the 0th index of the array will be multiplied from both array a and b. Like this it will continue upto n terms and the sum will be returned by the function.
The second function is declared as: int inner_product_reverse(int a[], int b[], int n);
It means the function name is inner_product_reverse and the parameters passed are one integer array a(int a[]),one integer array b(int b[]), and one integer n.
We are passing the two arrays containig the elements taken from user input, to the functions with the number of elements n.
The function should return a[0]*b[n-1] + a[1]*b[n-2] + ... +a[n-1] *b[0].
Now here the reverse is done. a[0]* b[n-1] means the array a is traversed from first to last i.e from 0 to n, while the array b is traversed last to first i.e n-1 to 0. The corresponding elemets of the index location of both the arrays are multiplied and the sum is generated which is returned by the function.
Please find the code below:
#include <stdio.h>
int inner_product(int a[], int b[], int n);
int inner_product_reverse(int a[], int b[], int n);
int main() {
// your code goes here
int a[50],b[50],i,j,n;
printf(" Enter no of terms n");
scanf("%d",&n);
printf("Enter array a");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter array b");
for(j=0;j<n;j++)
scanf("%d",&b[j]);
printf("Inner product is: %d",inner_product(a,b,n));
printf("Inner product reverse is: %d",inner_product_reverse(a,b,n));
return 0;
}
int inner_product(int a[], int b[], int n) {
int i,j,p=0;
for(i=0;i<n;i++)
{
p+=a[i]*b[i];
}
return p;
}
int inner_product_reverse(int a[], int b[], int n) {
int i,j,p=0;
for(i=0;i<n;i++)
{
p+=a[i]*b[n-1-i];
}
return p;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.