# Magic Squares!

• 04-28-2004
ZAM777
Magic Squares!
Hi
Ive got this homework to do concerning magic squares. Could anyone help me out on the C++ code for this problem? :confused:

An n by n matrix that is filled with the numbers 1, 2, 3, ... n*n is a Magic Square if the sum of the elements in each row, in each column and in the two diagonals is the same value. For example

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

is a Magic Square. Write a program that reads n from the user and then prompts for n*n values, ensuring that each is between 1 and n*n.
:Check that each number occurs only once
:Check if the sums of the rows, columns and diagonals are equal
:Display the square on the screen
Based on these facts, inform the user whether the numbers form a Magic Square or not.
You must use 2-Dimensional static arrays! Use a 4x4 array.
• 04-28-2004
jlou
Sure.

Step one. Write an empty program that does nothing but compile.
Step two. Create a 2-dimensional static array that is 4x4. Make sure it compiles.
Step three. Put code in to output the values in the square. Compile it and run it. Hard code your own values (like the example above) to test that this works. You can remove that later.
Step four. Add code to prompt the user for the n*n values. Put this code before the code you did in Step three. Remove the hardcoded values you used to test Step three. Compile it and run it. The values you input should then be printed back out in the form of a square.
Step five. Add the validation that ensures that each number is between 1 and n*n. Compile it, run it and test it.
Step six. Figure out how to test that every number appears only once. Decide what to do if this rule is violated (your assignment doesn't specify). Code it, compile it, run it, test it, until it works.
Step seven. Add code to validate whether the square is magic or not. Compile it, run it, test it, until it works.
Step eight. Smile, you are done.