Sum

How would I go about summing up the individual rows and columns? Output would look like this
______ total
1 4 3 __ 7
5 6 2 __ 13
3 7 2 __ 12
9 12 7
total

Any input would be appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
   #include <iostream>
#include <conio.h>

using namespace std;

int main()
{

    int A[10][10], i, j, r[10], c[10], row, col;
    cout<<"Enter the number of rows & columns :\n";
    cin>> row >> col;
    cout << "Enter the Elements: \n";
    for(i=0; i<row; ++i)
    {
        for(j=0; j<col; ++j)
        {
            cin>> A[i][j];
        }
    }

        cout<< "\nGiven : ";

        for(i = 0; i<row; ++i)
        {
                cout<< "\n";
                for(j = 0; j<col; ++j)
                {
                    cout<< A[i][j]<< " ";
                }
        }

        cout<<"\n";

        for(i=0; i<row; i++)
        {
            r[i] =0;
            for(j=0; j<col; ++j)
            {
                r[i] += A[i][j];
            }
        }

        for(j=0; j<col; ++j)
        {
            c[j] = 0;
            for(i=0; i<row; ++i)
            {
                c[j] += A[i][j];
            }
        }
		return 0;
}
Last edited on
I think I would use vectors instead of fixed array. What do you do when col or row are greater than 10? Totals go in a vector(s) too...
The answer of how to sum array row and columns is here: http://www.cplusplus.com/forum/beginner/114387/
Topic archived. No new replies allowed.