#include <iostream> #include <cstdlib> using namespace std; int main() { int i,j
ID: 3620406 • Letter: #
Question
#include <iostream>#include <cstdlib>
using namespace std;
int main()
{
int i,j,k;
int num[3][3];
int sums[8];
int temp;
bool magic;
cout<<"-----Magic Square Checker-----"<<endl;
cout<<"Please enter the numbers 1 through 9 in the order you want them in the square: "<<endl;
for (i=0;i<3;i++){
for (j=1;j<=3;j++)
cin>>num[i][j];
}
k=0;
for (i=0;i<3;i++){
temp=0;
for (j=0;j<3;j++)
temp=temp+num[i][j];
sums[k]=temp;
k++;
}
//columns
for (i=0;i<3;i++){
temp=0;
for (j=0;j<3;j++)
temp=temp+num[i][j];
sums[k]=temp;
k++;
}
sums[k]=num[0][0]+num[1][1]+num[2][2];
cout<<endl;
for (i=0;i<3;i++){
cout<<" ";
for (j=0;j<3;j++)
cout<<num[i][j]<<" ";
cout<<endl;
}
temp=sums[0];
magic=false;
for (i=1;i<8;i++)
if (temp!=sums[i]){
magic=false;
}
if (magic=true)
cout<<"The square is magic!"<<endl;
else
cout<<"Sorry, that isn't a magic square."<<endl;
return 0;
}
Explanation / Answer
please rate - thanks #include <iostream>#include <cstdlib>
using namespace std;
int main()
{
int i,j,k;
int num[3][3];
int sums[8];
int temp;
bool magic;
cout<<"-----Magic Square Checker-----"<<endl;
cout<<"Please enter the numbers 1 through 9 in the order you want them in the square: "<<endl;
for (i=0;i<3;i++){
for (j=0;j<3;j++)
cin>>num[i][j];
}
k=0;
for (i=0;i<3;i++){
temp=0;
for (j=0;j<3;j++)
temp=temp+num[i][j];
sums[k]=temp;
k++;
}
//columns
for (i=0;i<3;i++){
temp=0;
for (j=0;j<3;j++)
temp=temp+num[j][i];
sums[k]=temp;
k++;
}
sums[k]=num[0][0]+num[1][1]+num[2][2];
k++;
sums[k]=num[0][2]+num[1][1]+num[2][0];
cout<<endl;
for (i=0;i<3;i++){
cout<<" ";
for (j=0;j<3;j++)
cout<<num[i][j]<<" ";
cout<<endl;
}
temp=sums[0];
magic=true;
for (i=0;i<8;i++)
if (temp!=sums[i]){
magic=false;
}
if (magic)
cout<<"The square is magic!"<<endl;
else
cout<<"Sorry, that isn't a magic square."<<endl;
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.