### Magic Sqaure -- sum of diagonals help

Here's part of my code so far:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````void main () { int magic [row] [col]; //call functions getData( magic, row , col); double Determine = isMagic (magic, row, col); } void getData (int magic [] [col], const int row, const int col) { cout << "Enter 4 numbers for each row. \n"; for ( int row = 0; row < 4 ; row ++ ) { cout << "\n"; cout << " Row " << row + 1 << endl; cout << "-----------\n"; for ( int col = 0; col < 4 ; col ++) { cout << "Column " << col + 1 << ": "; cin >> magic [row] [col]; } } } double isMagic (int magic [][col], const int row, const int col) { int sum = 0; // Add rows for (int row = 0; row < 4 ; row ++) { sum = 0; for (int col = 0 ; col < 4 ; col ++) { sum += magic [row] [col]; } cout << " The sum for row " << row + 1 << " is " << sum << endl; } //Add columns for (int col = 0; col < 4 ; col ++) { sum = 0 ; for (int row = 0 ; row < 4 ; row ++) { sum += magic [row] [col]; } cout << " The sum for column " << col + 1 << " is " << sum << endl; } //Add Diagonal 1 for (int row = 0 ; row < 4 ; row ++) { sum += magic [row ] [];; } cout << " The sum for D1 is " << sum << endl; return sum; }``````

I am not sure how to go about getting the sum of the diagonals. Any ideas?
 ``1234`` ``````for (int rc = 0; rc < 4; rc++) { sum+= magic[rc][rc]; }``````

So it will add the diagonal in the middle:
0,0
1,1
2,2
3,3

* 0 0 0
0 * 0 0
0 0 * 0
0 0 0 *

 ``1234`` ``````for (int rc =1; rc < 4; rc++) { sum += magic[rc-1][rc]; }``````

0,1
1,2
2,3

0 * 0 0
0 0 * 0
0 0 0 *
0 0 0 0

I'm sure you can figure it out from here.
Wow! Thanks. So if I wanted to find the middle of the opposite side, it would look like this:

0,3
1, 2
2,1
3, 0

therefore making the code:
 ``1234`` ``````for (int rc = 0; rc < 4; rc --) { sum+= magic[rc][rc]; }``````

Actually nevermind. I'm guessing the change would have an addition interation and subtraction?

ps: Thanks Anthony
I was thinking about it and I'm sure you do the same thing as

 ``1234`` ``````for (int rc =1; rc < 4; rc++) { sum += magic[rc-1][rc]; }``````

except you flip the sum part to this `magic[rc][rc-1]` for finding the diagonals below the middle one

And yes, three iterations in total since you need to solve above the middle, below the middle and the middle itself.
Topic archived. No new replies allowed.