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

\'So I\'m doing a program that multiplies two arrays x and y and stores them int

ID: 3567319 • Letter: #

Question

'So I'm doing a program that multiplies two arrays x and y and stores them into z[] when I run my program output doesnt match my lab handout and Im pretty sure I added the arrays up right. So if somebody could go through it and it match the required output. Please do it in c because thats my language Im using and use gcc because that is also what im using. Right answers get a top rating the most points thanks in advance here is my code

#include <stdio.h>
int main(void)
{
  
   int i = 0;               /* counter for the loop */
   double sum;               /* adds the value of z[i] */
   int status;
   /* arrays */
   double x[10];
   double y[10];
   double z[10];
   /* input stuff */
   FILE *inp;
   inp = fopen("lab10p3.in", "r");    /* reads the input file */
   if (inp == NULL)
   return (-1);

status = fscanf(inp, "%lf %lf", &x[i], &y[i]);
      
   printf(" x * y = z ");
   printf("----- ------ ------ ");
   sum = 0;
   while (status != EOF)   /* reads file untill the end of file */
{
   z[i] = x[i] * y[i];
   sum += z[i]; /* this graps the sum of each value of each element in the third array and places it in sum */
   if(i > 9)        /* if the loop trys to put more than 10 elements it wll jump out */
   break;
   else
   printf("%5.2lf%9.2lf%11.2lf ", x[i], y[i], z[i]);
   i++;
   status = fscanf(inp, "%lf %lf ", &x[i], &y[i]);

} /* close while loop */
   printf(" ------ (+) ");
   printf("%25.2f ", sum);            /* prints the sum */

      
   fclose(inp);
   return(0);
} /* closes main */

here is one it so to be in the input file

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

and here is the second input file

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

finally here is the formatted output for the first input file

x * y = z
------ ------ ------
3.51 7.72 27.10
6.10 -4.30 -26.23
...
-3.47 17.60 -61.07
------ (+)
67.87

and the second formatted output

x * y = z
------ ------ ------
3.51 7.72 27.10
6.10 -4.30 -26.23
...
2.18 16.40 35.75
------ (+)
147.28

Thanks again

Explanation / Answer

#include <stdio.h>
int main(void){

int i = 0; /* counter for the loop */
double sum; /* adds the value of z[i] */
int status;
/* arrays */
double x[10];
double y[10];
double z[10];
/* input stuff */
FILE *inp;
inp = fopen("lab10p3.in", "r"); /* reads the input file */
if (inp == NULL)
return (-1);

status = fscanf(inp, "%lf %lf", &x[i], &y[i]);

printf(" x * y = z ");
printf("----- ------ ------ ");
sum = 0;
while (status != EOF) /* reads file untill the end of file */
{
z[i] = x[i] * y[i];
sum += z[i]; /* this graps the sum of each value of each element in the third array and places it in sum */
if(i >= 9) /* if the loop trys to put more than 10 elements it wll jump out */
break;
else
printf("%5.2lf%9.2lf%11.2lf ", x[i], y[i], z[i]);
i++;
status = fscanf(inp, "%lf %lf ", &x[i], &y[i]);

} /* close while loop */
printf(" ------ (+) ");
printf("%25.2f ", sum); /* prints the sum */


fclose(inp);
return(0);
} /* closes main */