### user input for a sum check. any suggestions?

I need help writing this program

magic square checker

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:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

then replies if this is a magic square or not.

Last edited on
your gonna need to post what you've started with first. How much have you written? Which direction are you going in?
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:

 ``12345`` ``````#include // this should be the best choice for you // ... std::vector > 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.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include #include using namespace std; int main() { cout << "Welcome to the Magic Square Checker! Please enter your magic square: " //cin >> int a0, a1, a2, a3; int b0, b1, b2, b3; int c0, c1, c2, c3; int d0, d1, d2, d3; // Here we are setting variables a0 = 1; a1 = 2; a2 = 3; a3 = 4; b0 = 1; b1 = 2; b2 = 3; b3 = 4; c0 = 1; c1 = 2; c2 = 3; c3 = 4; d0 = 1; d1 = 2; d2 = 3; d3 = 4; // horizontal sum sum1 = a0 + a1 + a2 + a3; sum2 = b0 + b1 + b2 + b3; sum3 = c0 + c1 + c2 + c3; sum4 = d0 + d1 + d2 + d3; // vertical sum vsum1 = a0 + b0 + c0 + d0; vsum2 = a1 + b1 + c1 + d1; vsum3 = a2 + b2 + c2 + d2; vsum4 = a3 + b3 + c3 + d3; // diagonal sum dsum1 = a0 + b1 + c2 + d3; dsum2 = a3 + b2 + c2 + d3; // check if(sum1 == sum2 == sum3 == sum4 // if statement == vsum1 == vsum2 == vsum3 == vsum4 == dsum1 == dsum2) { cout << " Yes this is a magic square" ; //Than this is a magic square } { else cout << " No this is not a magic square" ; // If not } // It think there needs to be a loop? return0; }[code]``````
[/code]
Last edited on
Are the squares you read of a predetermined size, or can they be any size?

http://cplusplus.com/articles/jEywvCM9/
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.

(The code below was not tested.)

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980`` ``````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(const int 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) return false; // 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) return false; 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) return false; 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) return false; return true; // 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.
Topic archived. No new replies allowed.