Hey Guys, first year compsci student and I'm struggling with 2D arrays.
Here is my code:
double classAverage(int assignments, int students)
{
double average,sum;
int row,col;
FILE *input_file;
input_file=fopen("grades.txt","r");
for (col=0;col<assignments;col++)
{
sum=0;
for (row=0;row<students;row++)
{
fscanf(input_file,"%lf",&grades[row][col]);
while(fgetc(input_file) == ',');
sum+=grades[row][col];
printf("%.2lf\n",sum);
}
}
average=sum/students;
fclose(input_file);
return (average)
;
int assignments and int students are commandline inputs in the main function, specifying how many students and assignments the teacher wants to mark.
I'm scanning in a grades.txt into a global variable grades[row][col], I need to sum the columns for each assignment.
grades.txt:
8.5 10.5 90.5
49.5 99 97
88 88 100
88.5 99 0
The problem Im having is the loops are summing 4 times each row, not 4 times each column. Also, after the loop completes 4 times, the sum variable becomes 2x4 returning 8 values from sum.
I'm pretty confused. I've searched here and found no clear solution.
I appreciate any help.