Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here's my incorrect code

C#
cout<<"The numbers are"<<endl;
    for(r=0; r<rn; r++)
     {
        cout<<endl;
       for(c=0; c<cn; c++)
        {
          cout<<a[r][c]<<"\t";
        }
     }

    for(r=0; r<rn; r++)//my code for getting the sum of 2d array
     {
       for(c=0; c<cn; c++)
        {
          cs=cs+a[r][c];
        }
       cout<<cs<<" ";//but the output is incorrect
     }
}

This should be the output
CSS
Enter number of columns: 4
Enter number of rows: 3
Enter twelve numbers: 1 1 1 1 1 1 1 1 1 1 1 1
The numbers are:
1   1   1   1
1   1   1   1
1   1   1   1
Sum of number 1 column is: 3
Sum of number 2 column is: 3
Sum of number 3 column is: 3
Sum of number 4 column is: 3
Sum of number 1 row is: 4
Sum of number 2 row is: 4
Sum of number 3 row is: 4


[EDIT - OP code from a "solution" which may be deleted]

Like this sir but the sum of the columns displays only one answer...
C#
for(r=0; r<rn; r++)
       {
       cs = 0;   
       for(c=0; c<cn; c++)
          {
          cs=cs+a[r][c];
          }
       cout<<"Sum of number "<<r<<" row is"<<cs<<endl;
       }
       cout<<"Sum of number "<<c<<" column is"<<cs<<endl;//displays only one answer
Posted
Updated 2-Aug-14 4:26am
v3
Comments
Richard MacCutchan 2-Aug-14 6:44am    
Where is the code that creates the matrix, and what answers do you get?
CPallini 2-Aug-14 9:25am    
Why don't you code a function for summing up the items of a row and another one for summing up the items of a column?

Well...You don't say what values you get, but I'd suggest that place to start would be by resetting the value of your counter before you start summing the columns...
At the moment, you are just adding to cs continually!
C++
for(r=0; r<rn; r++)
   {
   cs = 0;   // *** ADD THIS ***
   for(c=0; c<cn; c++)
      {
      cs=cs+a[r][c];
      }
   cout<<cs<<" ";
   }
Then, when that works, add a second pair of loops which do the rows on the inside and the columns on the outside.
 
Share this answer
 
v2
Like this sir but the sum of the columns displays only one answer...

C#
for(r=0; r<rn; r++)
       {
       cs = 0;   
       for(c=0; c<cn; c++)
          {
          cs=cs+a[r][c];
          }
       cout<<"Sum of number "<<r<<" row is"<<cs<<endl;
       }
       cout<<"Sum of number "<<c<<" column is"<<cs<<endl;//displays only one answer
 
Share this answer
 
v3
Comments
CHill60 2-Aug-14 10:25am    
Members will not know that you have responded if you post comments as solutions. You can always update your question with additional information (like this) and then use the Reply link next to a post to respond. The poster will be notified of your reply.
GwapoKho 2-Aug-14 17:55pm    
Like this sir but the sum of the columns displays only one answer...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900