The C language implementation reads a 3 by 3 array from a file and calculates the average of each row
- 2020-06-23 01:29:14
- OfStack
Topic request
Read a 3 by 3 array from the file and calculate the average of each row.
The reference solution
1. Data file: ES7en.dat
Create 1.dat file to hold the array file:
1 3 6
2 9 7
3 6 4
The file name is arbitrary.
Code 2.
avg = (a[i][0] + a[i][1] + a[i][2]) / 3 if the row average is calculated. This line of code will do.
Too easy to bother to write method:
#include<stdio.h>
void main(){
FILE *fr;
int i, j, a[3][3];
float avg;
fr = fopen("array.dat", "r");
while(!feof(fr))
{
printf("Command successfully executed.\n");
for(i=0; i<3; i++)
for(j=0; j<3; j++)
fscanf(fr, "%d", &a[i][j]);
}
printf(" The array read in is: \n");
for(i=0; i<3; i++)
for(j=0; j<3; j++){
printf("%d\t", a[i][j]);
if(j == 2)
printf("\n");
}
printf("\n The average of the array rows is: \n");
for(i=0; i<3; i++){
printf(" The first %d The average of the rows is: ", i+1);
avg = (a[i][0] + a[i][1] + a[i][2]) / 3;
printf("%.2f\n", avg);
}
}