Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

An n x n matrix that is filled with the numbers 1, 2, 3, ..., n2 is a magic squa

ID: 3647437 • Letter: A

Question

An n x n matrix that is filled with the numbers 1, 2, 3, ..., n2 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. I have provided a few data files for you to use to test your program. Note: your program should produce the correct result for any file in this format, not only for these files.

validex.txt - a valid magic square
missing.txt - a magic square that is missing one of the numbers in the range 1, 2, ..., 16.
sumfail.txt - a magic square that is fails the summing tests.

16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

Write a c++ program that reads in 16 values from a file and tests whether they form a magic square when put into a 4 x 4 array. You need to test two features:

Does each of the the numbers 1, 2, 3, ... , 16 occur in the array?
When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to each other?

Explanation / Answer

# Populate a 2-D list with numbers from 1 to n2 def makeSquare ( n ): # Print the magic square in a neat format where the numbers # are right justified def printSquare ( magicSquare ): # Check that the 2-D list generated is indeed a magic square def checkSquare ( magicSquare ): 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()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote