Sum of row of 2D array

To find the sum of row of a 2D array ( 4 X 4 matrix)i've inserted the formula in function count and the values are sent to function printout via reference variable. The problem is only the sum of the last row is printed out.Help please? Thanks

void count( double L[][size],int n,double &rowTotal)
{

for (int i = 0; i < 4; i++)
{
rowTotal = 0;

for (int j = 0; j < 4; j++)

rowTotal += L[i][j];


}
void print_out(double totalRow,int n)
{
for(i=0;i<4;i++)
cout<< " Sum of row " << (i+1) << " = " << totalRow << endl;
}
What is the parameter n supposed to be, and why don't you use it?

Since you're only saving the result of summing the last row, it's not really surprising that's what you're displaying.
Every time your outer loop begins another pass, you reset rowTotal to zero. So the value passed back from count will only be the sum of the final 4 items.

Edit: And if you'd used a debugger to step through your code, you would have been able to see this for yourself.
Last edited on
Topic archived. No new replies allowed.