processing array problem

Hello all... have a relatively simple problem regarding processing single rows in a two dimensional array. I want to add the data in a single row and output it to the screen. I'm having trouble with nailing down a single row at a time though. Heres what ive got so far:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdlib>
#include <iostream>

using namespace std;
const int NUMROWS=3;
const int NUMCOLS=4;
int main(int argc, char *argv[])
{
    int i, j, total=0, val[NUMROWS][NUMCOLS]={8,16,9,52,3,15,27,6,14,25,2,10};
    for(i=0;i<NUMROWS;i++)
    {
    
    cout<<endl;
    for(j=0;j<NUMCOLS;j++)
    cout<<val[i][j]<<"   ";
    total+=val[0][j];
}
    
   
   cout<<endl<<endl;
   cout<<total<<endl;
    system("PAUSE");
    return 0;
}


Thanks for any advice...
Something like this. Also note the spacing and how it is more readable.
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
#include <iostream>
#include <iomanip>
using namespace std;

const int NUMROWS = 3;
const int NUMCOLS = 4;

int main( int argc, char *argv[] )
{
    int val[NUMROWS][NUMCOLS] = { {  8, 16,  9, 52 },
                                  {  3, 15, 27,  6 },
                                  { 14, 25,  2, 10 } };
    int total = 0;

    for( int r = 0; r < NUMROWS; ++r)
    {
        int c, rowtotal = 0;
        for( c = 0; c < NUMCOLS; ++c)
        {
            cout << setw(4) << val[r][c];
            rowtotal += val[r][c];
        }
        cout << "  =  " << setw(4) << rowtotal << endl;
        total += rowtotal;
    }
    cout << setw(25) << "====";
    cout << "\n       Overall sum: "
         << setw(5) << total << "\n\n";

    system( "PAUSE" );
    return 0;
}
Topic archived. No new replies allowed.