The assignment is to write a program that creates magic squares implemented as dynamic 2-dimensional arrays. The program prompts the user for the number of rows (number of columns = number of rows). This should be an odd number. If the number of rows is n, the program should fill in the values 1, 2, 3, ... n*n so that the sum of integers in each row, column, main diagonal, and anti-diagonal are all the same. This is the condition for a magic square.
Here is the 3 x 3 example magic square:
4 9 2 3 5 7 8 1 6
To construct the magic square:
- Place 1 in the middle position of the last row.
- After value x has been placed in a square, place x + 1 in the
square determined by:
- Go up (previous) 2 rows and right (next) 1 column. If the entry is not yet used, place x+1 in this position.
- Otherwise, go up (previous) 1 row and place x+1 there.
If any move goes out of the square, wrap around. For example if in the top row and trying to move up one row, move to the bottom row. Similarly if in the last column and trying to move right, move to the first column.