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
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.
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];
}
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.
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.
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.