#include <iostream> using namespace std; int main() { int magicsquare[20][20]; i
ID: 3937986 • Letter: #
Question
#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;
}
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int magicsquare[20][20];
int rows;
int columns;
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 >> rows;
cin>>columns;
//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 < rows; r++)
{
for (int c = 0; c < columns; 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
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
cout<<magicsquare[i][j];
}
cout<<" ";
return 0;
}
void checkType(int mat[][],int r,int c)
{
int rsc,csc;
int dc;
rsc=csc=dc=0;
int sum=mat[0][0]+mat[0][1]+mat[0][2];
for(int i=0;i<r;i++)
{
int sum1=0;
for(int j=0;j<c;j++)
{
sum1=sum1+mat[i][j];
}
if(sum1==sum)
rsc++;
sum1=0;
}
for(int i=0;i<c;i++)
{
int sum2=0;
for(int j=0;j<r;j++)
{
sum2=sum2+mat[i][j];
}
if(sum2==sum)csc++;
sum2=0;
}
sum1=0;
sum2=0;
int a=0;
int b=c;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
sum1=sum1+mat[i][a];
sum2=sum2+mat[i][b];
a++;
b--;
}
}
if(sum1==sum2&&sum2==sum)dc=2;
if(rsc==r&&csc==c&&dc==2)
cout<<" Lo Shu magic square";
else if(rsc==r&&csc==c&&dc!=2)
cout<<" magic square";
else cout<<" not a magic square";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.