Magic Square

Develop a C++ program which determines if a square matrix is a magic square.

A magic square is an nxn (i.e, square) matrix of numbers containing all numbers from 1 to n2 such that all the numbers in any given row, column, or main diagonal sum to the same number. The number n is called the order of the magic square.

Your task is to write a program that reads the order of a magic square, and then reads n2 additional numbers (in row major order) into a two-dimensional nxn array. You program will then test the square and report whether or not it is a magic square. If the matrix is not a magic square, your program needs to identify which row, column or diagonal caused it to fail.

Note: None of the test cases for the program will be larger than a 5x5 square matrix. Therefore, it is sufficient for you to allocate your two-dimensional array as follows:

const int MAX_ARRAY_SIZE = 5;
int squareArray[MAX_SIZE][MAX_SIZE] = {0};

THIS IS AN EXAMPLE:
john >magic_square < test_case_1
What is the order of the square to be tested: Please enter the 16 values for the magic square in row major order:

Number Check: All numbers 1 to 16 are present.
Row Check:
Row 0 sum: 34
Row 1 sum: 34
Row 2 sum: 34
Row 3 sum: 34
Column Check:
Column 0 sum: 34
Column 1 sum: 34
Column 2 sum: 34
Column 3 sum: 34
Diagonal Check:
Diagonal #0 sum: 52

The square:
16 3 2 13
5 10 11 8
4 15 14 1
9 6 7 12
is not a magic square.


This is the code I have written so far, this is due in 24 hours. Any help would be amazing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>
using namespace std;

const int MAX_SIZE = 5;
int squareArray[MAX_SIZE][MAX_SIZE] = {0};

int main (){

        int ROWS, COLUMNS;
        int a[ROWS][COLUMNS];
        string s;

        cout << "Enter numbers into the magic square "
             << "and push enter when you're done.\n";
        getline(cin, s);

        while(s >= "0"){
                getline(cin, s);
        }

        cout << s << endl;

}
Last edited on
- You've defined a 2 dimensional array named `squareArray´ but never uses it.
- `ROWS´ and `COLUMNS´ should be assigned any value.
- Why did you define the array `a´? You never use it.
- I didn't see any code
- reading in the matrix, nor
- interpreting the matrix.


Try it, so we could help you!
This is the code I have written so far, this is due in 24 hours. Any help would be amazing.


Good. Now I won't feel guilty when I give you the answer (seeing this was posted 6 days ago).


I think this should work...
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;

const int MAX_SIZE = 5;
int squareArray[MAX_SIZE][MAX_SIZE];

int main(){
	int dimension;
	bool isMagicSquare = true; // Is true for now...

	cout << "Enter numbers into the magic square "
		<< "and push enter when you're done.\n";	
	
	string input;
	
	getline(cin, input);

	int TotalNumberCount = 0;

	// I'm assuming all user input is correct???
	// I'm assuming you want to retrieve all the numbers in one line of input???
	// Parsing user input is kind of a pain
	for (int i = 0; i < input.length(); i++)
	{
		if (input[i] == ' ')
		{
			TotalNumberCount++;
		}
	}
	
	// Check if the user inputted enough values to form a square.
	switch (TotalNumberCount){
	case 0:
		dimension = 1;
		break;
	case 3:
		dimension = 2;
		break;
	case 8:
		dimension = 3;
		break;
	case 15:
		dimension = 4;
		break;
	case 24:
		dimension = 5;
		break;
	default:
		cout << "Not a square or too large\n";
		return 0;
	}

	// Store all the variables in SquareArray
	stringstream getInt(input);
	int thisNumber;
	for (int row = 0; row < dimension; row++)
	{
		for (int col = 0; col < dimension; col++)
		{
			getInt >> thisNumber;
			squareArray[row][col] = thisNumber;
		}
	}



	// Compare all results to the First sum.
	int firstSum = 0;
	for (int j = 0; j < dimension; j++)
	{
		firstSum += squareArray[0][j];
	}

	cout << "Row Check:\n";
	for (int i = 0; i < dimension; i++)
	{
		int sum = 0;
		for (int j = 0; j < dimension; j++)
		{
			sum += squareArray[i][j];
		}
		cout << "Row " << i << " sum: " << sum << endl;
		if (sum != firstSum)
		{
			isMagicSquare = false;
		}
	}

	cout << "Column Check:\n";
	for (int i = 0; i < dimension; i++)
	{
		int sum = 0;
		for (int j = 0; j < dimension; j++)
		{
			sum += squareArray[j][i];
		}
		cout << "Column " << i << " sum: " << sum << endl;
		if (sum != firstSum)
		{
			isMagicSquare = false;
		}
	}


	cout << "Diagonal Check:\n";

	cout << "Diagonal #0 sum: ";
	int Diag1Sum = 0;
	for (int i = 0; i < dimension; i++)
	{
		Diag1Sum += squareArray[i][i];
	}
	cout << Diag1Sum << endl;

	cout << "Diagonal #1 sum: ";
	int Diag2Sum = 0;
	for (int i = 0; i < dimension; i++)
	{
		Diag2Sum += squareArray[dimension - 1 - i][i];
	}
	cout << Diag2Sum << endl;
	if (Diag1Sum != firstSum || Diag2Sum != firstSum)
	{
		isMagicSquare = false;
	}
	
	// Display Square
	cout << "The square\n";
	for (int row = 0; row < dimension; row++)
	{
		for (int col = 0; col < dimension; col++)
		{
			cout << squareArray[row][col] << " ";
		}
		cout << endl;
	}
	
	if (isMagicSquare)
		cout << "is a magic square\n";
	else
		cout << "is not a magic square\n";

	return 0;
}
Can you please start a new thread rather than taking over someone else's thread.

You haven't asked a question. But we can take a look on your new thread, not on this one.
Topic archived. No new replies allowed.