Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a program to take two numerical lists of the same length ended by a sen- t

ID: 3545872 • Letter: W

Question

write a program to take two numerical lists of the same length ended by a sen- tinel value and store the lists in arrays x and y , each of which has 20 elements. Let n be the actual number of data values in each list. Store the product of corresponding elements of x and y in a third array, z , also of size 20. Display the arrays x , y , and z in a three-column table. Then compute and display the square root of the sum of the items in z . Make up your own data, and be sure to test your program on at least one data set with number lists of exactly 20 items. One data set should have lists of 21 numbers, and one set should have significantly shorter lists.



a ) hange the maximum number of elements in each array to 10, rather than 20.


b). Instead of reading in the list of (x,y) data until a sentinel value is reached, read the (x,y) data from the input file lab10p3.in until either 10 items are read or until EOF is reached, whichever is sooner.


c) Do not compute the square root of the sum of z. Just compute the sum of z.


D) Below are two sample runs. Display the results on the screen





If the input file contains: 3.51 7.72 6.1 -4.3 3.4 13.4 -9.8 8.6 5.2 12.4 4.59 -4.32 -3.47 17.6 Then the formatted output should be: If the input file contains: 3.51 7.72 6.1 -4.3 3.4 13.4 -9.8 8.6 5.2 12.4 4.59 -4.32 -3.47 17.6 -19.6 4.12 8.7 14.3 2.18 16.4 -16.9 3.0 17.3 -1.84 Then the formatted output should be:

Explanation / Answer

#include<stdio.h>
int main()
{
//declare three float arrays.
float x[10],y[10],z[10];
//declare sum array.
float sum=0.0;
int n=0,i;
// pointer to FILE to open file.
FILE *fp;
// open file lab10p3.in
fp = fopen("lab10p3.in", "r");
// if file pointer is not NULL
if( fp != NULL )
{
// if end of file is not reached.
while ( !feof(fp ) )
{
// SCAN two variables on each line.
fscanf(fp,"%f %f",&x[n],&y[n]);
// once read the two variable into x[n] and y[n]
// store their product in variable z[n]
z[n] = x[n]*y[n];
// increment array index.
n++;
// if we reach 10 array elements break.
if(n==10)
break;
}
//close file pointer.
fclose(fp);
}
// print formatted output.
printf(" x * y = z ");
printf(" ---- ---- -----");
for(i=0; i<n; i++)
{
// print x[i], y[i] and z[i].
printf(" %5.2f %5.2f %5.2f",x[i],y[i],z[i]);
// sum the value of z one by one.
sum = sum+z[i];
}
// print their sum
printf(" ----- (+)");
printf(" %5.2f",sum);
// below line is just to stop the command screen window. not required in unix.
// commnet below line, if u are running in UNIX.
getchar();
return 0;
}