Magic Square ( Due 27 Jan 2018 )

A n x n matrix that is filled with the numbers 1, 2, 3, ..., 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.

Implement the following algorithm to construct the magic n-by-n squares. This algorithm works only if n is odd.

The skeleton of your program will be as follows:


# Populate a 2-D list with numbers from 1 to n2
def make_square ( n ):

# Print the magic square in a neat format where the numbers
# are right justified
def print_square ( magic_square ):

# Check that the 2-D list generated is indeed a magic square
def check_square ( magic_square ):

def main():
  # Prompt the user to enter an odd number 3 or greater

  # Check the user input

  # Create the magic square

  # Print the magic square

  # Verify that it is a magic square

main()

In your function main() you will prompt the user to enter an odd number. You must check that the input is a positive odd number greater than or equal to 3. If it is not, you will prompt the user to re-enter the number and check again and again.

Then you will create a 2-D list representing the Magic Square. You will then print out the magic square in a neat format by calling the function print_square(). In the function print_square() you MUST use print with formatting.

You will then call the function check_square(). This function checks that the sum of all the rows have the same value and prints out that sum. It checks that the sum of all the columns have the same value and prints out that sum. It sums the two main diagonals and prints out the sum. For a magic square of size n, the sum is n * (n2 + 1) / 2.

This is a sample of what the program will output:

Please enter an odd number: 5

Here is a 5 x 5 magic square:

11  18  25   2   9
10  12  19  21   3
 4   6  13  20  22
23   5   7  14  16
17  24   1   8  15

Sum of row = 65
Sum of column = 65
Sum diagonal (UL to LR) = 65
Sum diagonal (UR to LL) = 65

The file that you will be turning in will be called MagicSquare.py. We will be looking for good documentation, descriptive variable names, clean logical structure, and adherence to the coding conventions discussed in class. You may work with a partner on this assignment. Both of you must read the paper on Pair Programming and abide by the ground rules as stated in that paper. The file will have a header of the following form:

'''
  File: MagicSquare.py

  Description:

  Student's Name:

  Student's UT EID:
 
  Partner's Name:

  Partner's UT EID:

  Course Name: CS 313E 

  Unique Number: 

  Date Created:

  Date Last Modified:
'''

There will be only one copy of the file being submitted if you are doing pair programming.

Use the Canvas system to submit your MagicSquare.py file. We should receive your work by 11 PM on Saturday, 27 Jan 2018. There will be substantial penalties if you do not adhere to the guidelines. Remember Python is case sensitive. The name of your file must match exactly what we have specified.

References

  1. Magic Square Article in Wolfram MathWorld
  2. Article on Magic Square in Wikipedia