Magic Square Testing Program

Hello, I'm almost finished with a homework assignment requiring me to write a program that checks to see if a user inputted square with n(order) rows and n(order) columns is a magic square. The rules for a magic square are as follows:
must have all different numbers between 1-n*n, each row's sum must equal the others, and each column's sum must equal the others.


I am having a hard time figuring out why when the program checks the sums of the rows and columns, occasionally the sums work and occasionally emit garbage. If you could take a look at my code and give me a nudge in the right direction, that would be most appreciated. Thanks!

The code that applies to the situation is the following: \

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Check Row sums
cout << "Row Check:" << endl;
for (int i=0; i<order; i++) {
	for (int j=0; j<order; j++) {
		RowSums[i] += squareArray[i][j];
	}
cout << " Row " << i << " sum: " << RowSums[i];
cout << endl;
}

//(Have yet to check whether Rows are equal)


//Check Column sums
cout << "Column Check:" << endl;
for (int j=0; j<order; j++) {
	for (int i=0; i<order; i++) {
		ColumnSums[j] += squareArray[i][j];
	}
cout << " Column " << j << " sum: " << ColumnSums[j];
cout << endl;
}

//(Have yet to check whether Columns are equal) 


The program compiles without error, but the output I'm getting is the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
All numbers 1-9 are present.
Row Check:
 Row 0 sum: 6299816
 Row 1 sum: 15
 Row 2 sum: 4196966
Column Check:
 Column 1 sum: 18
 Column 2 sum: 15
 Column 3 sum: 1762966972
The square:
                9   8   7
                6   5   4
                3   2   1
is a magic square


Last edited on
Topic archived. No new replies allowed.