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

As early as 650 BC, mathematicians had been composing magic squares, a sequence

ID: 3775132 • Letter: A

Question

As early as 650 BC, mathematicians had been composing magic squares, a sequence of n numbers arranged in a square such that all rows, columns, and diagonals sum to the same constant. Used in China, India, and Arab countries for centuries, artist Albrecht Dürer’s engraving Melencolia I (year: 1514) is considered the first time a magic square appears in European art. Each row, column, and diagonal of Dürer’s magic square sums to 34. In addition, each quadrant, the center four squares, and the corner squares all sum to 34. An example of a “magic square” is displayed below.
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Write a program to prove a series of numbers is indeed a 4 x 4 magic square. Your program should complete the following steps, in this order:
(a) Ask the user to enter their proposed magic square in a single input statement (e.g., [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]—note this example is a 4 x 4 matrix, but NOT a magic square). You may assume the user will enter whole numbers; they will not enter either decimal values or text.
(b) Check that all values are positive; ** for-loop or nested for-loop required in the solution. If one or more of the values in the matrix are negative or zero, issue a statement to the command window informing the user of the mistake and ask the user to enter another matrix. This check should be repeated until the user enters a matrix with positive values. This check should work even if the user does not enter a 4 x 4 matrix; it should work regardless of the size of matrix entered.
(c) Check for an arrangement of 4 x 4. If the matrix is not a 4 x 4, issue a statement to the command window informing the user of the mistake and ask the user to enter another matrix. This check should be repeated until the user enters a 4 x 4 matrix. You may assume the re-entered matrix contains only positive values; you do not need to re- check the new matrix for positive values, only for matrix dimensions.
(d) Determine if the matrix is a form of a magic square. The minimum requirement to be classified as a magic square is each row and column sums to the same value. ** for- loop or nested for-loop required in the solution. If this criteria is not met, issue a statement to the command window informing the user they have not entered a magic square and ask the user if they wish to try another magic square. This question can be posed using either a text answer entered by the user (yes, no) or by using a menu. If the user chooses to run the program again, the entire program starting with step (a) should begin again.
(e) Determine the classification of the magic square using the following requirements:
1. If each row and column sums to the same value, the magic square is classified as “semimagic”; the summation value is called the magic constant.
2. If, in addition to criterion 1, each diagonal sums to the same value as the rows and columns, the magic square is classified as “normal;” ** for-loop or nested for-loop required in the solution. The use of built-in functions such as diag, fliplr, rot90, trace or similar built-in functions is forbidden.
3. If, in addition to #1 and #2, the largest value in the magic square is equal to 16, the magic square is classified as “perfect;”

Format your magic square classification similar to the format shown below. You may choose to format your table differently, but each classification should contain a “yes” or “no” next to each magic square category. The magic constant for your magic square is 24. The classification for your magic square: Semi-Magic Normal Perfect yes yes yes After this table appears, ask the user if they wish to try another magic square. This question can be posed using either a text answer entered by the user (yes, no) or by using a menu. If the user chooses to run the program again, the entire program starting with step (a) should begin again. A few test cases for you to consider: Albrecht Dürer magic square: [16, 3, 2, 13; 5, 10, 11, 8; 9, 6, 7, 12; 4, 15, 14, 1]; Chautisa Yantra magic square: [7, 12, 1, 14; 2, 13, 8, 11; 16, 3, 10, 5; 9, 6, 15, 4]; Sangrada Familia church, Barcelona magic square: [1, 14, 14, 4; 11, 7, 6, 9; 8, 10, 10, 5; 13, 2, 3, 15]; Random magic square: [80, 15, 10, 65; 25, 50, 55, 40; 45, 30, 35, 60; 20, 75, 70, 5]; Steve Wozniak’s magic square: [8, 11, 22, 1; 21, 2, 7, 12; 3, 24, 9, 6; 10, 5, 4, 23].


Explanation / Answer

/* 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."<<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]<<" ";
}
cout << " ";
}

int rsum=0,rs[rowsandcolumns];
int csum=0,cs[rowsandcolumns];
int dsum=0,d2sum=0;
int flag1=1,flag2=1;
//Testing given input matrix

     
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];
}

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];
}

for (int i = 0; i < rowsandcolumns; ++i)
        {
            dsum = dsum + magicsquare[i][i];
            d2sum = d2sum + magicsquare[i][rowsandcolumns - i - 1];
        }

cout <<dsum <<d2sum;
int temp=rs[0];
for (int r = 0; r < rowsandcolumns; r++){
  

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

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

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

}

if(dsum != d2sum)
flag2=0;

if(flag1==1 && flag2==1)
cout << "Lo Shu Magic Square -All rows & columns & Diagonals have sam sum ";
else if (flag1==1)
{
   cout << "Square Magic ";
}
else
cout << "Not a Magic square ";


return 0;
}

Ouput :

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.) 4
Enter the values for the square one value at a time.
Enter the value for row 1 column 1. 1
Enter the value for row 1 column 2. 1
Enter the value for row 1 column 3. 1
Enter the value for row 1 column 4. 1
Enter the value for row 2 column 1. 1
Enter the value for row 2 column 2. 1
Enter the value for row 2 column 3. 1
Enter the value for row 2 column 4. 1
Enter the value for row 3 column 1. 1
Enter the value for row 3 column 2. 1
Enter the value for row 3 column 3. 1
Enter the value for row 3 column 4. 1
Enter the value for row 4 column 1. 1
Enter the value for row 4 column 2. 1
Enter the value for row 4 column 3. 1
Enter the value for row 4 column 4. 1
Given Input Matrix is :
1       1       1       1
1       1       1       1
1       1       1       1
1       1       1       1
Lo Shu Magic Square -All rows & columns & Diagonals have sam sum