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

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

ID: 3630773 • Letter: O

Question

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 main diagonal 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. You program 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, column or diagonal caused it to fail.

Example: Two examples of the program being tested are shown on the next page. In the first case a 4x4 array is read into the program after which the program determines the 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 3x3 array is a magic square.

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

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, you do 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.

Explanation / Answer

please rate - thanks

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{const int MAX_SIZE = 5;
int squareArray[MAX_SIZE][MAX_SIZE] = {0};
   int n,i,j,total,tot;
    bool sum=true, seq=true;
    bool num [MAX_SIZE*MAX_SIZE];
    cout<<"What is the order of the square to be tested: ";
    cin>>n;
    for(i=0;i<n*n;i++)
         num[i]=false;
cout<<"Please enter the 9 values for the magic square in row major order: ";
   for(i=0;i<n;i++)
       for(j=0;j<n;j++)
         {cout<<"enter element["<<i<<"]["<<j<<"]: ";
         cin>>squareArray[i][j];
            num[squareArray[i][j]-1]=true;
            }
    cout<<"The square ";
    for(i=0;i<n;i++)
    {for(j=0;j<n;j++)
      cout<<setw(4)<<squareArray[i][j];
     cout<<endl;
    }
for(i=0;i<n*n;i++)
    if(!num[i])
         seq=false;
if(!seq)
    cout<<"all numbers 1 to "<<n*n<<" are not present ";
else
    cout<<"all numbers 1 to "<<n*n<<" are present ";   
cout<<"Row Totals: ";
for(i=0;i<n;i++)
   {tot=0;
    for(j=0;j<n;j++)
       tot+=squareArray[i][j];
    cout<<"Row "<<i<<" sum: "<<tot<<endl;
    }
total=tot;   
cout<<"Column Totals: ";
for(j=0;j<n;j++)
   {tot=0;
    for(i=0;i<n;i++)
       tot+=squareArray[i][j];
    if(tot!=total)
         sum=false;
    cout<<"Column "<<j<<" sum: "<<tot<<endl;
    }
cout<<"Diagonal #0 sum: ";
tot=0;
for(i=0;i<n;i++)
    tot+=squareArray[i][i];
cout<<tot<<endl;
if(tot!=total)
         sum=false;   
cout<<"Diagonal #1 sum: ";
tot=0;
for(i=0;i<n;i++)
    tot+=squareArray[i][n-i-1];
if(tot!=total)
         sum=false;
   
cout<<tot<<endl;


if(!sum)
    cout<<"Not Magic square due to sum error ";

if(sum&&seq)
     cout<<"Magic square ";
system("pause");
return 0;
}