Dec 11, 2012 at 4:34am UTC
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)
cout<< " Sum of row " << (i+1) << " = " << totalRow << endl;
Dec 11, 2012 at 7:40am UTC
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.
Dec 11, 2012 at 2:31pm UTC
Every time your outer loop begins another pass, you reset
to zero. So the value passed back from
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
Dec 11, 2012 at 2:32pm UTC