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;
}
|