Magic Square Program

What am i doing wrong? Why is it not reading in correctly? I get a negative value.

Write a program to determine if a 3 x 3 array filled with integers is a Magic Square. A Magic Square is when you sum each row, each column, and the two diagonals. This means that all the sums (8 of them) are all the same value. That is the sum of the three values on row 0 equal the sum of row 1 equal to sum of row 2 equal to sum of col 0, 1, 2 and equal to the sum of the major and minor diagonals.


Output:
You are to print out the 2-D array by rows (one row per line), then print if the Square is Magic Square or not and if the Square is Magic, also print the sum value.


Input: Your input file is MagicSquare.txt.

The input file looks like this:

4 9 2 3 5 7 8 1 6
8 1 6 3 5 7 4 9 2
34 119 102 153 85 17 68 51 136
357 0 255 102 204 306 153 408 51
185 222 37 0 148 296 259 74 111
20 45 10 15 25 35 40 5 30
2 6 7 9 5 1 4 3 8
14 49 42 63 35 7 28 21 56

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int DIMENSION = 3;   // dimension for the array

int magicSquare[DIMENSION][DIMENSION];  // declare arrays

void readMagicSquare(int magicSquare[DIMENSION][DIMENSION]) // read data from file
{
	ifstream inFile("MagicSqaure.txt"); //open file for reading           
	
	for (int r = 0; r < DIMENSION; r++)   //row loop
	{
		for (int c = 0; c < DIMENSION; c++)  //column loop
		{
			inFile >> magicSquare[r][c]; //read data into 2D array
			cout << magicSquare[r][c] << endl;
			system("PAUSE");
		}
	}
			
	inFile.close(); // close the file                
}
void display_2D_Array(int magicSquare[DIMENSION][DIMENSION]) // display matrix
{
	cout << " Magic Square Program " << endl << endl;
	
	for (int r = 0; r < DIMENSION; r++)
	{
		for (int c = 0; c < DIMENSION; c++)
		{
			cout << setw(6);  //set output width to 6 characters
			cout << magicSquare[r][c] << "\t ";  // display numbers
		}
		cout << endl;
	}
}

bool determineMagicSquare(int magicSqaure[][DIMENSION], int square)
{
	int sum = magicSquare[0][0] + magicSquare[0][1] + magicSquare[0][2];
	int newSum = 0;
	
	// Verifies Rows
	for (int row = 0; row < DIMENSION; row++)
	{
		for (int column = 0; column < DIMENSION; column++)
		{
			newSum += magicSquare[row][column];
		}

		if (newSum != sum)
		{
			return false;
		}
		
		newSum = 0;
	}
	
	// Verifies Columns
	for (int column = 0; column < DIMENSION; column++)
	{
		for (int row = 0; row < DIMENSION; row++)
		{
			newSum += magicSquare[row][column];
		}

	}

	if (newSum != sum)
	{
		return false;
	}
	
	newSum = 0;

	// Verifies Diagonals
	if (magicSquare[0][0] + magicSquare[0][1] + magicSquare[0][2] != sum)
	{
		return false;
	}

	if (magicSquare[0][2] + magicSquare[1][1] + magicSquare[2][0] != sum)
	{
		return false;
	}

	return true;
}

int main()
{
	int square = 0;
	int magicSquare[DIMENSION][DIMENSION];

	readMagicSquare(magicSquare);  // read data from file
	display_2D_Array(magicSquare);  // display matrix
	determineMagicSquare(magicSquare, square);  //Verifies to see if its a Magic Square!

	system("Pause");
	return(0);
}  
Did you spell magicSquare.txt incorrectly?

ifstream inFile("MagicSqaure.txt"); //open file for reading
No that's how he has it.
So you have checked that the actual text file is spelled that way?

Well I saved as a text file the data I gave above as magicSquare.txt

With the incorrect name of magicSqaure.txt in the code for the file used I get with your code,

2293512
2293700
1988070613
1821512159
-2
1987973474
1988320196
4201744
2293652
Magic Square Program

2293512 2293700 1988070613
1821512159 -2 1987973474
1988320196 4201744 2293652

With the correct name I gave my file, magicSquare.txt, I get with your code,

4
9
2
3
5
7
8
1
6
Magic Square Program

4 9 2
3 5 7
8 1 6
Last edited on
Topic archived. No new replies allowed.