A Magic Square is a 2dimensional array with the same number

A Magic Square is a 2-dimensional array with the same number of rows and columns (hence the term square). What makes it “magic” it that is filled with the whole numbers 1 to the number of rows and columns squared in an arrangement that the sum of each column and each row is the same value. A Magic Square is considered to be a Lo Shu Magic Square when the sum of the diagonals is the same number.

Review the 3 tables below. Each is a square because the number of rows is the same as the number of columns (3). Each has been filled with the numbers 1 through 9. Both A and B are Magic Squares because the sum of each row and column is the same value,15 in this example. B is a Lo Shu Magic Square because the sum of the diagonals (4 + 5 + 6 and 2 + 5 + 8) is also 15.

A template is below and has code to enable a user to input numbers into a square array. You may assume the user has entered the correct whole numbers. You need to write the introductory comments and code to output the user’s input. Next you need to write the code that determines if the user’s input creates a Lo Shu Magic Square, Magic Square, or not a magic square. The code has been written that the user can create up to a 20 by 20 square. You may assume the user does not enter a size greater than 20.

/* Add your introductory comments here.*/
#include <iostream>using namespace std;
int main(){int magicsquare[20][20];

int rowsandcolumns;cout << \"What size of a magic square would you like to test?\"

<< \"\ (Because it is a square the number of rows equals the number

of columns.) \";

cin >> rowsandcolumns;

                 //assume the user enters a value <= 20

cout << \"Enter the values for the square one value at a time.\";//assume the user enters correct values
for (int r = 0; r < rowsandcolumns; r++){

for (int c = 0; c < rowsandcolumns; c++){

cout << \"Enter the value for row \" << r + 1 << \" column \" << c

+ 1 << \". \";

cin >> magicsquare[r][c];

}

}//Print out their input array here as a table
//test if it is a Lo Shu Magic Square, Magic Square (not Lo Shu),// or not a magic square and output the result
return 0;

}

4 92) 3 5 7 A 4 9 2 ; 1 2 3 8 1 6

Solution

/* CPP program to find square magic or not*/
#include <iostream>
using namespace std;
int main()
{
int magicsquare[20][20];
int rowsandcolumns;
cout << \"What size of a magic square would you like to test?\"<< \"\ (Because it is a square the number of rows equals the number of columns.) \";
cin >> rowsandcolumns;                                        //assume the user enters a value <= 20
cout << \"Enter the values for the square one value at a time.\"<<endl; //assume the user enters correct values
for (int r = 0; r < rowsandcolumns; r++){
for (int c = 0; c < rowsandcolumns; c++){
cout << \"Enter the value for row \" << r + 1 << \" column \" << c
+ 1 << \". \";
cin >> magicsquare[r][c];
}
}

//Printing given input array here as a table
cout << \"Given Input Matrix is : \"<<endl;
for (int r = 0; r < rowsandcolumns; r++){
for (int c = 0; c < rowsandcolumns; c++){

cout << magicsquare[r][c]<<\"\\t\";
}
cout << \"\ \";
}
// Required varibles to find square magic
int rsum=0,rs[rowsandcolumns];       //rsum used for row sum and rs is used for store each row sum
int csum=0,cs[rowsandcolumns];       //csum used for column sum and cs is used for store each column sum
int dsum=0,d2sum=0;                   //dsum for store diagonal 1 sum and d2sum for diagonal d2 sum
int flag1=1,flag2=1;


//loops to find row sums   
for (int r = 0; r < rowsandcolumns; r++){
   rsum=0;
for (int c = 0; c < rowsandcolumns; c++){
   rsum=rsum+magicsquare[r][c];

}
rs[r]=rsum;
//cout << rs[r];
}


//loops to find column sums
for (int r = 0; r < rowsandcolumns; r++){
       csum=0;
for (int c = 0; c < rowsandcolumns; c++){
   csum=csum+magicsquare[c][r];

}
cs[r]=csum;
cout << cs[r];
}
//loops to find diagonal sums
for (int i = 0; i < rowsandcolumns; ++i)
{
dsum = dsum + magicsquare[i][i];
d2sum = d2sum + magicsquare[i][rowsandcolumns - i - 1];
}

//cout <<dsum <<d2sum;


//loop find row sums are equal ,if all are not equal flag1 set to 0
int temp=rs[0];
for (int r = 0; r < rowsandcolumns; r++){
  

   if(temp==rs[r])
   continue;
   else
   {
       flag1=0;
      
   }
  
  

}

//loop find columns sums are equal and equal to row sums,if all are not equal flag1 set to 0

for (int r = 0; r < rowsandcolumns; r++){
  

   if(temp==cs[r])
   continue;
   else
   {
       flag1=0;
      
   }
  

}


//finding diagonal sums are equal ,if not equal set to flag2 as 0
if(dsum != d2sum)
flag2=0;

if(flag1==1 && flag2==1)       //flag1 and flag2 are equal to 1 ,its Lo Shu Magic Square
cout << \"\ Lo Shu Magic Square\ \";
else if (flag1==1)                   //flag1 only equal to 1 ,its Magic Square
{
   cout << \"\ Square Magic\ \";
}
else
cout << \"\ Not a Magic square\ \";       //flags are set to 0 then it is not square magic


return 0;
}

Output :

What size of a magic square would you like to test?
(Because it is a square the number of rows equals the number of columns.) 3
Enter the values for the square one value at a time.
Enter the value for row 1 column 1. 4
Enter the value for row 1 column 2. 9
Enter the value for row 1 column 3. 2
Enter the value for row 2 column 1. 3
Enter the value for row 2 column 2. 5
Enter the value for row 2 column 3. 7
Enter the value for row 3 column 1. 8
Enter the value for row 3 column 2. 1
Enter the value for row 3 column 3. 6
Given Input Matrix is :
4 9 2
3 5 7
8 1 6
Lo Shu Magic Square

A Magic Square is a 2-dimensional array with the same number of rows and columns (hence the term square). What makes it “magic” it that is filled with the whole
A Magic Square is a 2-dimensional array with the same number of rows and columns (hence the term square). What makes it “magic” it that is filled with the whole
A Magic Square is a 2-dimensional array with the same number of rows and columns (hence the term square). What makes it “magic” it that is filled with the whole
A Magic Square is a 2-dimensional array with the same number of rows and columns (hence the term square). What makes it “magic” it that is filled with the whole

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site