Add sum of rows and colums for array

I have an array that prints out, but I need to calculate the rows and columns using a for loop but unsure of how to actually go about it.


using namespace std;


int main()
{
int square[3][3] = {
{4, 3, 6},

{8, 1, 7},

{2, 3, 4}
};



for (int i = 0; i < 3; i++)//row
{

for (int j = 0; j < 3; j++)//column
{


cout << square[i][j]<<" ";

for ()

}cout << endl;
}

}
its similar. for a row, for each column, add to a sum,repeat for each row.
so you still need to loop every single value, exactly as you printed it. I suggest int rowsum[3] and then inside the column loop you have rowsum[i] += square[i][j]

and a similar set of loops for the column sums. its possible to do it all in one loop, doing rows and columns together, but its messy and you may not be ready to try that. If you feel up to it, I highly recommend doing it that way.

for (int i = 0; i < 3; i++)//row
{

for (int j = 0; j < 3; j++)//column
{


cout << square[i][j]<<" ";

for (int i = 0; i < 3; i++) {
rowtotal = 0;
for (int j = 0; j < 3; j++) {
rowtotal = rowtotal + square[i][j];

}
}

for (int j = 0; j < 3; j++) {
coltotal = 0;
for (int i = 0; i < 3; i++) {
coltotal = coltotal + square[i][j];
}

}
}


}cout << endl;

cout << endl << "The total for each row is: " << rowtotal;
cout << endl << "The total for each column is: " << coltotal;
}

I've done this and it works fine.. But my main problem is how do I get it to sum both diagonals, diagonal 1 = [i]+[j] and diagonal 2 = i+j-3 but how do I write that?
Last edited on
I've done this and it works fine..


Actually, it doesn't work fine. Your looping is a little bit messed up.

While you are printing out the elements of your matrix, for EACH ELEMENT you are calculating the total for each row and the total for each column. However, only the last row and last column are saved, and you print it out after each row, stating "The total for each row is:". It's actually only 1 row and 1 column printed multiple times.

For testing purposes, why don't you use a different matrix that is not a magic square so you can verify that you are actually calculating the values of each row and column correctly.

What I think you really want to do is the following:

Your main loop is printing out each row separately. While you are printing out this row, also calculate the total, and then print it at the end. Maybe print some special character like "|" to signify that you are printing a sum rather than a matrix element.

Then when you are done printing out the rows, calculate the sums of the columns. Print each one separately (maybe lining them up below the columns they represent. You can use a row of "_" to signify that you are printing the sums rather than another row.

To calculate the diagonals, you will loop through i OR j--the index can be used for both row and column. Figure out how to use the index by listing the indices that make up the diagonals.
I understand what you mean but I don't know how to actually do it as I'm still very new to this. I've now got this code, but the answers aren't right.



using namespace std;

int rowsum;

int main()
{
int magic[3][3] = {
{4, 3, 6},

{8, 1, 7},

{2, 3, 4}

};



for (int i = 0; i < 3; i++)//row
{

for (int j = 0; j < 3; j++)//column
{


cout << magic[i][j] << " ";

}cout << endl;
}


//sum of row
for (int i = 0; i < 3; i++)
{
rowsum = 0;
for (int j = 0; j < 3; j++)
{
rowsum= +magic[i][j];
cout << "the total of row is: " << rowsum <<endl;


}

}

}
Last edited on
Let's say you want to get the sum of row 0.
It's rowSum = magic[0][0] + magic[0][1] + magic[0][2];
Do you see the pattern?
A simple loop is enough.
@rocketboy,

PLEASE USE CODE TAGS
Either put [code] and [/code] round your code, or select it and use the <> button on the formatting menu. This will allow us to see your original indentation and, if it has a main() routine, run it in c++ shell online.

Add the line #include <iostream> at the start of your code. Then it will actually compile.

rowsum= +magic[i][j]; should be rowsum += magic[i][j]; - note the difference! Alternatively, do it explicitly with rowsum = rowsum + magic[i][j];

You are outputting rowsum immediately after adding an element. Move it below the next bracket (i.e., outside the j loop)


Finally, your magic square ... isn't magic.
I've done what you said and it's working fine but I don't understand how it get added together.. I know rowsum += magic[i][j] is the same as rowsum = rowsum +magic[i][j].. but how does it add the numbers.

Does it work like this: for adding the rows - 0 is less than 3 so it moves to the nested loop (which is the column), 0 is less than 3 and the number is 4, rowsum+4 = 4... the column can be incremented again, so it is now 1, 1 is less than 3, the number is 3, rowsum is currently 4 so +3= 7, can be incremented again, 2 is less than 3, the number is 6, rowsum is currently 7+6 = 13.

But how would I now add the diagonals together? diagonal 1 and diagonal 2

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
61
62
63
64
65
66
#include <iostream>


using namespace std;

int rowsum;
int columnsum;

int main()
{
	int magic[3][3] = {
	{4, 3, 6},

	{8, 1, 7},

	{2, 3, 4}

	};



	for (int i = 0; i < 3; i++)//row
	{

		for (int j = 0; j < 3; j++)//column
		{


			cout << magic[i][j] << " ";

		}cout << endl;
	}



	//sum of row
	for (int i = 0; i < 3; i++)
	{
		rowsum = 0;
		for (int j = 0; j < 3; j++)
		{
			rowsum += magic[i][j]; //same as rowsum = rowsum + magic[i][j]
			


		}
cout << "the total of row is: " << rowsum <<endl;
	}

	//sum of column
	for (int j = 0; j < 3; j++)
	{
		columnsum = 0;
		for (int i = 0; i < 3; i++)
		{
			columnsum += magic[i][j];
		}

		cout << "the total of column is: " << columnsum << endl;
	}

	// sum of diag1

	

}
Last edited on
For the main diagonal you only need to add numbers of the form
magic[i][i]
so there will only be one loop, in i.

For the reverse diagonal you only need to add numbers of the form
magic[i][2-i]
so, again, there will only be one loop, in i.

The easiest way to do this is to write down, with pen and paper, the matrix indices that you are adding up. e.g. for the main diagonal:
0,0 ..... 1,1 ..... 2,2
Do you spot a pattern.

When you've done enough you won't need the pen and paper any longer.
It works, but how come I don't declare what diag2 is equalled to, e.g. diag2= 0; ? How does it work without saying what it is actually equalled to because diag2 + is equalled to diag2 + magic[i][2-i]; but how can that work? Thanks for your help.

1
2
3
4
5
6
7
8
 for (int i = 0; i < 3; i++)
	{
		diag2 += magic[i][2-i];
	}
	cout << "diag2 is: " << diag2 << endl;
}

	
Last edited on
If you don't initialise it to 0 then the behaviour is "undefined" and the compiler could use anything, including whatever was in that memory slot beforehand. Some people might even re-use another variable that was no longer needed for anything else.

So to guarantee that you get it correct, make sure that you set diag2 to zero before accumulating the sum.
Topic archived. No new replies allowed.