##### Question

# C++ Magic squares

Objective: Develop a C++ program which determines if a square matrix is a magic square.

Discussion: A magic square is an nxn (i.e, square) matrix of numbers containing all numbers from 1 to n2 such that all the numbers in any given row, column, or maindiagonal sum to the same number. The number n is called the order of the magic square. For an example, see the square below (of order 4):

Your task is to write a program that reads the order of a magic square, and then reads n2 additional numbers (in row major order) into a two-dimensional nxn array. Youprogram will then test the square and report whether or not it is a magic square. If the matrix is not a magic square, your program needs to identify which row, columnor diagonal caused it to fail.

Example: Two examples of the program being tested are shown on the next page. In the first case a 4×4 array is read into the program after which the program determinesthe array is not a magic square because the diagonal sum from top left to bottom right is not correct. In the second case the program finds that the 3×3 array is amagic square.

Additional Details:

a. Note: None of the test cases for the program will be larger than a 5×5 square matrix. Therefore, it is sufficient for you to allocate your two-dimensional array asfollows:

const int MAX_ARRAY_SIZE = 5;

int squareArray[MAX_SIZE][MAX_SIZE] = {0};

b. The ‘<’ character is used for input redirection. Input redirection allows your program to take the input from a file, rather than typing it from the keyboard.This simplifies testing, because you can put the keystrokes you want (e.g., the numbers you want to test) into a file and re-use them repeatedly. In this homework, youdo not have to input from file.

john >magic_square < test_case_1

What is the order of the square to be tested: Please enter the 16 values for the magic square in row major order:

Number Check: All numbers 1 to 16 are present.

Row Check:

Row 0 sum: 34

Row 1 sum: 34

Row 2 sum: 34

Row 3 sum: 34

Column Check:

Column 0 sum: 34

Column 1 sum: 34

Column 2 sum: 34

Column 3 sum: 34

Diagonal Check:

Diagonal #0 sum: 52

The square:

16 3 2 13

5 10 11 8

4 15 14 1

9 6 7 12

is not a magic square.

john >magic_square < test_case_2

What is the order of the square to be tested: Please enter the 9 values for the magic square in row major order:

All numbers 1 to 9 are present.

Row Check:

Row 0 sum: 15

Row 1 sum: 15

Row 2 sum: 15

Column Check:

Column 0 sum: 15

Column 1 sum: 15

Column 2 sum: 15

Diagonal Check:

Diagonal #0 sum: 15

Diagonal #1 sum: 15

The square:

8 1 6

3 5 7

4 9 2

is a magic square.