Magic squares
 Due Apr 4, 2016 by 11:59pm
 Points 20
 Submitting a file upload
 File Types cpp
Magic squares (inspired by problem P6.19 from Horstmann)
(You may find the file we looked at last Wednesday to be helpful as well.)
An nbyn array filled with the numbers 1, 2, 3, ..., n^{2} is a magic square if the sum of the elements in each row, column, and 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 
You can check that all row sums, column sums, and diagonal sums are equal to 34. Check your understanding: what is the "magic number" for an nbyn magic square?
Write a program that reads in 16 int
values from a file and tests whether they form a magic square when put into a 4by4 array (sample file provided, keep reading). You need to test two properties:
 Does each of the numbers 1, 2, 3, 4, ..., 16 occur in the list of values?
 When the numbers are put into a square, are the sums of the rows, columns, and diagonals all equal to one another?
This is easiest if you use a 2dimensional table. Use a vector of vectors for this purpose: that is, store your values in a vector< vector<int> >
.
Use the provided code scaffold, and submit your code in a program called magic_squares.cpp
. Practice the technique of using functions to perform different parts of your procedure and calling those functions within the main
function.
Hints
Here are some hints to keep in mind while you work on this assignment.
 Remember our motto: "when in doubt,
cout
". Use trace statements invokingcout
to help you debug. For example, you could put them in your loops to show what is happening during each iteration of a loop. Of course you will remove all these after your program works correctly.  Use the algorithms described elsewhere in Chapter 6 (and in other parts of the book) to help you solve the subproblems. Have you reread Chapter 6? All of it? If you are stuck, now might be a good time to revisit it.
 You might need to think for a bit about how to check for the correct entries (that is, that the user enters each value from 1 through n^{2} exactly once). Use the techniques in Chapter 6.5 to help yourself think about it. There are a few different ways.
 Don't use "magic numbers": in particular, the size of the magic square. You should give it an ALL CAPS identifier and use that identifier exclusively in your code. Write all your functions to use this variable instead of the literal constant. Use the following rule: no integer literals allowed except 0, 1, and 2.
 Use the
main
function as a "driver" program to test your code. As you write the function bodies provided in the scaffold, add code tomain
that will make sure they are working properly. As you complete the program, you can update or replace the test code inmain
with its final form.
You can download two files here: the first one contains four magic squares, and the second one contains a few nonmagic squares. (The easiest way to use different squares is to edit these files and reorder the lines, since your program is going to be written just to use the first line of such a file.)
Rubric
Criteria  Ratings  Pts  

Program reads input from file and populates the square
threshold:
pts


pts



Row and column sums are correctly implemented
threshold:
pts


pts



Program checks that inputs are 1 through 16 exactly once
threshold:
pts


pts



Program checks that square is magic
threshold:
pts


pts



Main function delegates work to other functions
threshold:
pts


pts



Total Points:
20.0
out of 20.0
