Overview The Lo Shu Magic Square is an arrangement of the digits l through 9 in
ID: 3778135 • Letter: O
Question
Overview The Lo Shu Magic Square is an arrangement of the digits l through 9 in a 3x3 grid such that: each digit is only used one time the sum of the each row, column, and diagonal all add up to the same value 4 9 2 15 3 5 7 8 1 6 ms is a valid Lo Shu Magic Square because it uses all 9 digits one time, and all of the rows, columns, and diagonals add up to the same value (15) 2 9 1 12 3 5 8 7 4 6 n uses all 9 digits one time, but the rows. columns, and diagonals do not add up to the same value. Therefore, it is not a valid Lo Shu Magic Square. For this assignment, implement and use the methods for a class called LoShuMagicSquare that will be used to verify solutions for Lo Shu Magic Squares. LoShuMagicSquare class This class will represent a single student in the CSC1240 class. Data Members The data members for the class are: an integer symbolic constant that holds the maximum number ofrows in atwo dimensional array. Use a value of 3. an integer symbolic constant that holds the maximum number of columns in a two dimensional array. Use a value of3. a two-dimensional array of integers with 3 rows and 3 columns that will hold the possible solution Constructor This class has one constructor that should initialize the two-dimensional array of integers. It takes no arguments. The array should be initialized to hold 0s in all 9 elements.Explanation / Answer
Here is the code for you:
#include <iostream>
#include <fstream>
using namespace std;
class MagicSquare
{
static const int MAXROWS = 3;
static const int MAXCOLS = 3;
int square[MAXROWS][MAXCOLS];
public:
MagicSquare()
{
for(int i = 0; i < MAXROWS; i++)
for(int j = 0; j < MAXCOLS; j++)
square[i][j] = 0;
}
void fillSquare(const char fileName[])
{
ifstream fin;
fin.open(fileName);
if(fin.is_open())
{
for(int i = 0; i < MAXROWS; i++)
for(int j = 0; j < MAXCOLS; j++)
fin>>square[i][j];
}
fin.close();
}
void printSquare()
{
for(int i = 0; i < MAXROWS; i++)
{
for(int j = 0; j < MAXCOLS; j++)
cout<<square[i][j]<<" ";
cout<<endl;
}
}
bool isMagic()
{
int r1, r2, r3, c1, c2, c3, d1, d2;
r1 = square[0][0] + square[0][1] + square[0][2];
r2 = square[1][0] + square[1][1] + square[1][2];
r3 = square[2][0] + square[2][1] + square[2][2];
c1 = square[0][0] + square[1][0] + square[2][0];
c2 = square[0][1] + square[1][1] + square[2][1];
c3 = square[2][0] + square[2][1] + square[2][2];
d1 = square[0][0] + square[1][1] + square[2][2];
d2 = square[0][2] + square[1][1] + square[2][0];
if(r1 == 15 && r2 == 15 && r3 == 15 && c1 == 15 && c2 == 15 && c3 == 15 && d1 == 15 && d2 == 15)
return true;
else
return false;
}
};
int main()
{
MagicSquare mSquare;
char fileName[50];
cout<<"Enter the name of the file to read: ";
cin>>fileName;
mSquare.fillSquare(fileName);
mSquare.printSquare();
cout<<"Is it magic? ";
if(mSquare.isMagic())
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.