ANSWER IS REQUIRED IN C. Write a program that will input two arrays of informati
ID: 3802290 • Letter: A
Question
ANSWER IS REQUIRED IN C.
Write a program that will input two arrays of information. Each array will contain exactly 4 floating point values. Your program will enter these values from standard input. Once the values have been read in, your program should call a function that will compute the inner product of these two arrays. The function should return the value back to the main program. The main program will then print out the value. You should not print the value out inside the inner product function. The inner product of two arrays is a single number obtained by multiplying corresponding elements and then adding up their sums. E.g., if array u = (5, 1, 6, 2) and array v = (1, 2, 3, 4) then the inner product is 33 because (5 * 1) = 5 (1 * 2) = 2 (6 * 3) = 18 (2 * 4) = 8 and 5 + 2+18+ 8 is 33. Your main function will call a function called inner that has the following declaration: float inner (float u[], float v[], int size); Do not hardcode the loop inside inner to go from 0 to 3. It should go from 0 to size-1.Explanation / Answer
C Program
#include<stdio.h>
float inner(float u[],float v[],int size); // function decleration
int main()
{
int size = 4,i; // variable declerations
float u[4],v[4],result;// variable declerations
// prompting the user to enter the vector u
printf("Enter the four values of vector u ");
for(i =0;i<size;i++)
scanf("%f",&u[i]); // reading the vector u from keyboard
// prompting the user to enter the vector v
printf("Enter the four values of the vector v ");
for(i = 0;i<size;i++)
scanf("%f",&v[i]);// reading the vector u from keyboard
result = inner(u,v,size); // calling the function inner()
// printing the result
printf("The Result of innerproduct of u and v is %f ",result);
return 0;
}
float inner(float u[],float v[],int size) // function definition
{
int i; // variable declerations
float result = 0; // variable declerations
for(i = 0;i <size;i++) // loop to perform the inner product
result += u[i]*v[i]; // computing the inner product
return result; // returning the result
}
Testing the code
maths@maths-HP-Z440-Workstation:~/Chegg$ ./a.out
Enter the four values of vector u
5 1 6 2
Enter the four values of the vector v
1 2 3 4
The Result of innerproduct of u and v is 33.000000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.