## Butum

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;

for (col=0;col<assignments;col++)
{
sum=0;
for (row=0;row<students;row++)
{
while(fgetc(input_file) == ',');
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.

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.

Normally, you would sum up via row like this:

``````for(row=0;row<MaxRows;row++) {
for(col=0,sum=0;;col<MaxCols;col++) {
sum+=Array[row][col];
}
printf("\nThis row sums up to %d", sum);
}

//Now you just "turn it sideways", to sum by column:
for(col=0;col<MaxCols;col++) {
for(row=0,sum=0;;row<MaxRows;row++) {
sum+=Array[row][col];
}
printf("\nThis col sums up to %d", sum);
}``````

Give that a shot and see if it makes sense. ;) I used int's out of habit, only. Same pattern when you sum up columns of doubles or floats.

## Butum

So that solution is working. However it's only working for that function when int assignments is set to 3. so, only working on the 3rd assignment.

I don't know if its because my functions are calling the wrong parameters or what's going on.

``````double classAverage(int assignments, int students)
{

double average,sum;
int row,col;
FILE *input_file;

for (row=0;row<students;row++)
{
for (col=0;col<assignments;col++)
{
while(fgetc(input_file) == ',');
}
}

for(row=0;row<students;row++)
{
sum=0;
for(col=0;col<assignments;col++)
{
}
printf("\nThis row sums up to %lf", sum);
}
for(col=0;col<assignments;col++)
{
sum=0;
for(row=0;row<students;row++)
{
}
printf("\nThis col sums up to %lf", sum);
}

average=sum/students;

fclose(input_file);
return (average);

}``````

That's my updated code. When int assignments is 3, everything works great but if its 2, or 1 it doesnt sum properly. It's like it doesn't "turn the array sideways", it's still summing the first row, and then the next item on the 2nd row.

OK, well let's troubleshoot this:

1) Do you have the array values that you think you have? Print out the array, lined up by columns, and see if it's all OK.

2) As you step through the loop with a bad number of columns, what is going on when you watch the values in the array, and in sum?

Clearly, something fishy should be spotted! ;)

This is common, btw. So often, we think our code is just GOLDEN, and find out "oops!", it really hasn't been tested, until it's REALLY been tested! ;)

## Butum

Alright, so I'm going to troubleshoot step by step.

The return values from my functions when assignments==3 are telling me that my functions are working as intended. They are summing the columns, finding minimum and maximum and average.

However, when the Assignment parameter is set to anything below 3, it's returning logic errors. The values aren't what they're supposed to be.

The printf statements in the loops are telling me that the columns and rows are not summed accurately until they loop atleast 3 times. So the function has to loop atleast 3 times, and then move backwards from there.

This row sums up to 109.500000
This row sums up to 245.500000
This row sums up to 276.000000
This row sums up to 187.500000
This col sums up to 234.500000
This col sums up to 296.500000
This col sums up to 287.500000

those are the printf statements for assignment==3, the rows and columns are perfect.

My problem is, once it's looped 3 times, how do I pull that column sum from the array...do I have to add another function parameter with a double array value?

``````#
double classAverage(int assignments, int students,a[i][j])``````

then when I call the function in main to be:

``````classAverage(assignments,students,a[4][1]);//assignment1 average
classAverage(assignments,students,a[4][2]);//assignment 2 average
classAverage(assignments,students,a[4][3]);//assignment 3 average``````

I'm having trouble wrapping my head around this

"As am I!", as the priceline commercial says, as am I. ;)

You haven't debugged it until you can sum up one or more columns, with one or more students (rows), and get the correct answers.

If you want to "pull" (another priceline moment?), the sum out of the function, then you need to send the address of sum, from the calling function, into your called function:

calling function(add &sum to the parameter list), called function(add double *sum to the parameter list).

Then within the called function, use *sum, instead of sum, because sum is just a pointer now.

## Butum

Problem solved.

``````for(col=0;col<=assignments;col++)
{
sum=0.0;
for(row=0;row<students;row++)
{