Magic squares are a square grid of consecutive numbers with the property that every row, column and diagonal sums to the same number.
It should look like this:
Welcome to the magic square checker. please enter your magic square
If you only need to read squares of predetermined size (i.e. always 4*4) then it's dead easy, just use a 2D array such as:
int square[4][4];
If the squares can vary in size, it gets a bit harder and I suggest you read the data from a text file, and instead of 2D arrays you should use one of the STL containers:
1 2 3 4 5
#include <vector> // this should be the best choice for you
// ...
std::vector<std::vector<int> > square;
It appears uglier this way, but at least you don't have to manage the size anymore: you'll just read data and the vectors will resize accordingly.
I worked on it some more.
This is a four by four row magic square, the user must input the numbers and this program will check if every row, column and diagonal sums to the same number. or not.
You are doing all the sums manually... which is why I suggested a 2D array (or matrix if you will) in the first place. With a matrix all you need to do is play around with indices in a couple of for() loops.
int square[4][4];
// reading data from keyboard
for (int i=0; i<4; ++i)
for (int j=0; j<4; ++j)
{
cout << "square[" << i << "][" << j << "] = ";
cin >> square[i][j];
}
// ...
// magic testing function
// will return true if square is magic, false otherwise
bool is_magic(constint square[4][4])
{
// all other sums need to be equal to this
int expected_sum = 0;
// compute the expected sum
for (int j=0; j<4; ++j)
expected_sum += square[0][j];
int temp_sum = 0;
// check the lines
// (start from the second line because expected_sum already sums up the first)
for (int i=1; i<4; ++i)
{
for (int j=0; j<4; ++j)
temp_sum += square[i][j];
if (temp_sum != expected_sum)
returnfalse; // quit on the first bad sum
temp_sum = 0; // clear the temporary sum for next line
}
// check the columns
for (int j=0; j<4; ++j)
{
for (int i=0; i<4; ++j)
temp_sum += square[i][j];
if (temp_sum != expected_sum)
returnfalse;
temp_sum = 0;
}
// check the diagonals
// diagonal one, formula square[k][k]
// square[0][0] + square[1][1] + square[2][2] + ... + square[n-1][n-1]
for (int k=0; k<4; ++k)
temp_sum += square[k][k];
if (temp_sum != expected_sum)
returnfalse;
temp_sum = 0;
// diagonal two, formula square[k][n-1-k]
// square[0][n-1] + square[1][n-2] + square[2][n-3] + ... + square[n-1][0]
for (int k=0; k<4; ++k)
temp_sum += square[k][4-1-k];
if (temp_sum != expected_sum)
returnfalse;
returntrue; // if we got here, it can only be that the square is magic...
// either that, or I messed up some checks
}
// ...
if (is_magic(square))
cout << "Yes this is a magic square.\n";
else
cout << "No this is not a magic square.\n";
The problem with the above code (which wasn't tested) is that the size of the square is hardcoded as 4. The good thing is that, unlike in your solution, if you figure out how to remove the 4 and use a variable n instead, the program will work for squares of any size n.