CIS 220 - Program 8
Objectives: Using multi-dimensional arrays, write a program that displays a magic square of whatever size is specified by the user.
Description: Magic squares are squares that contain numbers arranged in such a way
that all the rows, columns, and diagonals add up to the same number.
An example of a magic square of size n=3 is shown below.
algorithm exists to determine where numbers should be placed so that rows and
columns will add up to the same number.
The algorithm works for
odd n only, and is as follows (for a square of size
Add 1 to
Move left one square and up one square and insert the number
This algorithm assumes that rows and columns wrap-around—if a move is
off the top of the square, start again at the bottom; similarly if a move is off
the left of the square, start again at the right.
Write a program to implement this magic square algorithm. Your program should allow the user to enter the required size (n) in a text field, and then after verifying that n is valid, it should print out a magic square of that size by displaying the n x n array in a text box.