CS 210 - Fundamentals of Programming I

Spring 2006 - Programming Assignment 6

20 points

Out: March 30, 2006
Due: April 6, 2006


(6 points) Consider the following problem statement.

A magic square is an n by n grid in which each of the integers 1, 2, 3, ..., $n^2$ appears exactly once, and all column sums, row sums, and diagonal sums are equal. For example, the following is a 5 by 5 magic square in which all the rows, columns, and diagonals add up to 65:

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

The following is a procedure for constructing an n by n magic square for any odd integer n. Place 1 in the middle of the top row. Then after integer k has been placed, move up one row and one column to the right to place the next integer k + 1, unless one of the following occurs:
(i). If a move takes you above the top row in the jth column, move to the bottom of the jth column and place the integer k + 1 there.

(ii). If a move takes you outside to the right of the square in the ith row, place k + 1 in the ith row at the left side.

(iii). If a move takes you to an already filled square or if you move out of the square at the upper right-hand corner, place k + 1 immediately below k.


Write an analysis and design for a program to construct an n by n magic square for any odd value of n. The magic square should be represented using a 2-dimensional array. There must be at least two functions in addition to the main program: one to construct the magic square and one to display the magic square on the screen. The main program should repeatedly ask the user for n and construct and display an n by n magic square (by calling the two functions) until the user inputs 0 for n. The main program should also make sure that n is odd and print an error message if it is not. You may assume that the largest n entered will be 30. The 5 by 5 magic square given above was constructed using this procedure. Write the analysis and design of each function in as a comment block just above the function it pertains to, including main, in your implementation file.


(14 points) Implement the program you designed above. It must implement the functions specified for full credit. Also, the magic square should be displayed so that the columns line up. For example, the above 5 by 5 magic square might be display as follows:

17   24    1    8   15
23    5    7   14   16
 4    6   13   20   22
10   12   19   21    3
11   18   25    2    9
Name your source file magic.cpp. Note that you can check if the construction is correct by computing the row, column, and diagonal sums of the grid.


Follow the guidelines in the A C++ Programming Style Guideline handout. As stated in the syllabus, part of the grade on a programming assignment depends on how well you adhere to the guidelines. The grader will look at your code listing and grade it according to the guidelines.


Turn in a hardcopy printout of your program (that includes the analysis and design) to the instructor, and submit your source file (magic.cpp) by emailing it as an attachment to
cs210@csserver.evansville.edu by 5pm on the due date. (Make sure you send it to csserver. If you send to just evansville.edu, it will not be delivered.)


Converted using latex2html on Wed Mar 29 21:47:21 CST 2006