CODE IN C Using template below complete the following write
*****CODE IN C++ Using template below, complete the following:
-write the introductory comments and code to output the user’s input
-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;
}
Solution
/* Program name : magicsquare.cpp
* Programmer Name:
* Date :
* Description: This program takes 2-d array square matrix and
* checks weather given matrix is Lo Shu Magic Square or not
*/
#include <iostream>
using namespace std;
int main() { int magicsquare[20][20];
int rowsandcolumns;
int sum[20]={0},i=0;
cout << \"What size of a magic square would you like to test?\";
cout<< \"\ (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
for (int r = 0; r < rowsandcolumns; r++) {
for (int c = 0; c < rowsandcolumns; c++) {
cout<<magicsquare[r][c]<<\" \";
sum[i]+=magicsquare[r][c];
}
i++;
cout<<endl;
}
//test if it is a Lo Shu Magic Square, Magic Square (not Lo Shu), // or not a magic square and output the result
int sum1 = sum[0],cnt = 0;
for (int r = 0; r < rowsandcolumns; r++)
{
if(sum1 == sum[r] )
{
cnt++;
}
else
{
cout<<\"Not magic square...\"<<endl;
}
}
if(cnt == rowsandcolumns )
{
cout<<\"Lo Shu Magic Square...\"<<endl;
}
return 0;
}
