Magic Sqaure -- sum of diagonals help

Here's part of my code 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
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
67
68
69
70
71
72
73
74
75
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?
1
2
3
4
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 *

1
2
3
4
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:
1
2
3
4
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

1
2
3
4
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.