TotalUp Array

Hey, guys, I got this code written but I don't know how to following things:
a. Total up the numbers in each row and print the total at the end of the row
b. Total up the numbers in each column and print the columns total below each column
c. Total up all the numbers in the array and print this total in the bottom right corner of the print
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
53
54
55
56
57
58
59
60
/******************************************
 *     library includes
 ******************************************/
#include "stdafx.h"
#include <iostream>                // needed for I/O

/******************************************
 *     pre-processor
 ******************************************/
#define PI 3.14159
using namespace std;

/****************************************
 *          function prototypes
 ****************************************/


/*****************************************
 *   main() - the function that executes
 *****************************************/
int main()
{
    /*******************************
     *   local variables            *
     *******************************/
    int row = 0;                    // row counter
    int col = 0;                    // column counter
    
    int maxRow = 20;                // max rows
    int maxCol = 10;                // max columns
    
    int intArray [20][11];          // 2 dimensional array of ints
    
    // load the array with random numbers from 0 ~ 9
    for(row=0; row<maxRow; row++)
    {
        for(col=0; col<maxCol; col++)
        {
            intArray[row][col] = rand() % 10;
        }
    }
    
    // print out the initial array
    cout << "\n\n";
    cout << "*******************\n";
    cout << "*  initial array  *\n";
    cout << "*******************\n";
    for(row=0; row<maxRow; row++)
    {
        cout << "\n";
        for(col=0; col<maxCol; col++)
        {
            cout << intArray[row][col] << " ";
        }
    }
    cout << "\n\n";
    
    system("pause");
    return 0;
}  // end main 




Right now output for this code is this:

*******************
* initial array *
*******************

7 9 3 8 0 2 4 8 3 9
0 5 2 2 7 3 7 9 0 2
3 9 9 7 0 3 9 8 6 5
7 6 2 7 0 3 9 9 9 1
7 2 3 6 5 5 8 1 4 7
1 3 8 4 8 0 4 6 0 3
2 6 9 4 1 3 7 8 8 3
8 1 5 3 5 4 3 6 5 9
5 4 9 1 7 5 5 4 1 8
8 3 5 2 2 6 6 7 8 4
1 7 1 8 7 8 7 7 4 4
9 1 5 5 5 8 2 9 8 2
0 7 4 8 5 8 3 0 6 2
2 5 2 2 7 1 5 2 1 1
0 1 8 7 6 0 0 9 5 6
2 8 5 9 4 3 9 0 5 6
3 6 3 0 8 4 0 4 6 4
6 4 3 8 4 0 0 9 3 7
5 7 2 9 7 5 6 5 3 0
4 8 5 5 5 4 2 1 9 2

But I want the one output as above and second is like this but with sum of the row and column and total array


*******************
* Summed up array *
*******************

7 9 3 8 0 2 4 8 3 9
0 5 2 2 7 3 7 9 0 2
3 9 9 7 0 3 9 8 6 5
7 6 2 7 0 3 9 9 9 1
7 2 3 6 5 5 8 1 4 7
1 3 8 4 8 0 4 6 0 3
2 6 9 4 1 3 7 8 8 3
8 1 5 3 5 4 3 6 5 9
5 4 9 1 7 5 5 4 1 8
8 3 5 2 2 6 6 7 8 4
1 7 1 8 7 8 7 7 4 4
9 1 5 5 5 8 2 9 8 2
0 7 4 8 5 8 3 0 6 2
2 5 2 2 7 1 5 2 1 1
0 1 8 7 6 0 0 9 5 6
2 8 5 9 4 3 9 0 5 6
3 6 3 0 8 4 0 4 6 4
6 4 3 8 4 0 0 9 3 7
5 7 2 9 7 5 6 5 3 0
4 8 5 5 5 4 2 1 9 2
Last edited on
Please use code tags. http://www.cplusplus.com/articles/jEywvCM9/


a. Total up the numbers in each row and print the total at the end of the row
b. Total up the numbers in each column and print the columns total below each column
c. Total up all the numbers in the array and print this total in the bottom right corner of the print
 
int intArray [20][11]; // 2 dimensional array of ints 

You need an extra row as well.
I'd recommend that you declare the array like so:
 
int intArray [maxRow + 1][maxCol + 1] = { 0 }; // 2 dimensional array of ints 

It's always a good habit to initialise your variables.
That doesn't answer my question I'm looking for a code that can add row and column.
1
2
3
4
5
6
7
8
9
10
11
for( int col{}, row{}; row < maxRow; col++ ) {
    if( i % maxCol ) {
        /*
        reached the end of the row,
        so move on to next row
        */
        row++;
    }

    intArray[row][maxCol + 1] += intArray[row][col];
}

Enjoy.
Thanks for the code but I'm confuse where to put this code?
Maybe if you read and understood the code you'll notice that I made a few logic errors on purpose. After you've fixed those, you should have a full understanding of how it works and where it fits into your code.
I'm new to c++ you suppose to help me with right code not wrong.
1
2
3
4
5
6
7
8
9
10
11
12
int sum = 0;
for( int col = 0, row = 0; row < maxRow; col++ ) {
    // reached the end of the row
    if( col == maxCol ) {
        row++;
        col = 0;
        
        intArray[row][col + 1] = sum;
    }

    sum += intArray[row][col];
}

A much more easier version to understand. This snippet sums each of the rows until it has reached the maximum number of rows.
Here is what I did but still not printing the total of row, column, or total array.

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
 
    // print out the Summed up array
    cout << "\n\n";
    cout << "*********************\n";
    cout << "*  Summed up array  *\n";
    cout << "*********************\n";

    for(row=0; row<maxRow; row++)
    {
        cout << "\n";
        for(col=0; col<maxCol; col++)
        {
            cout << intArray[row][col] << " ";
        }
    }
    
    int sum = 0;
    for( int col = 0, row = 0; row < maxRow; col++ ) {
        // reached the end of the row
        if( col == maxCol ) {
            row++;
            col = 0;
            
            intArray[row][col + 1] = sum;
        }
        
        sum += intArray[row][col];
    }


Here is what I did but still not printing the total of row, column, or total array.

That's because you're printing the array before you've summed it.
My code snippet only sums the rows. Summing the columns and total array should be very similar though.
Topic archived. No new replies allowed.