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

PLEASE USE EITHER CLION OR VISUAL STUIDOS TO DO THIS ASSIGNMENT.!!! Construct a

ID: 3827506 • Letter: P

Question

PLEASE USE EITHER CLION OR VISUAL STUIDOS TO DO THIS ASSIGNMENT.!!!

Construct a class named ThreeBy3 that manages a 3 by 3 array of integers. The class should have methods to display its array, and determine if the array fits the criteria for a magic square. In a 3 x 3 magic square, each of the eight rows, three horizontal, three vertical, and two diagonal, sum to the same number. Use the following template. Read 3 x 3 arrays from the attached file. For each one read, create an instance of ThreeBy3, then display the array, and follow that with a line of output that states whether the array is a magic square. Don't forget to delete the instance when it is no longer needed.

Attaches file msq2.txt

Please copy paste this data in a .txt document so you could stream it for the assignemt.

Explanation / Answer

Here is the modified version for you:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class ThreeBy3
{
static const int SQ = 3;
static const int rows[][3][2];
static const int rowCount;
int square[SQ][SQ];
int sum;
public:
ThreeBy3(int ms[][3]);
bool isMagicSquare();
void display(std::ostream& os);
};
const int ThreeBy3::rows[][3][2] = {
{{0,0}, {0,1}, {0,2}}, // x,y subscripts of the top, horizontal row
{{1,0}, {1,1}, {1,2}},
{{2,0}, {2,1}, {2,2}},
{{0,0}, {1,0}, {2,0}}, // x,y subscripts of the left, vertical column
{{0,1}, {1,1}, {2,1}},
{{0,2}, {1,2}, {2,2}},
{{0,0}, {1,1}, {2,2}}, // top,left to bottom,right diagonal
{{0,2}, {1,1}, {2,0}},
};
const int ThreeBy3::rowCount = (sizeof rows) / (sizeof rows[0]);
ThreeBy3::ThreeBy3(int ms[][3])
{
// copy the passed 3 x 3 array into the square member variable
for(int i = 0; i < 3; i++)
   for(int j = 0; j < 3; j++)
       square[i][j] = ms[i][j];
// set the sum member variable to the sum of the three elements in the top horizontal row
sum = ms[0][0] + ms[0][1] + ms[0][2];
}
bool ThreeBy3::isMagicSquare()
{
// return true if the sum of the top, horizontal row is equal to each of the sums of the other rows
for(int i = 0; i < 8; i++)
{
    int x = 0;
    for(int j = 0; j < 3; j++)
        x += square[rows[i][j][0]][rows[i][j][1]];
    if(x != sum)
        return false;  
}
// return false otherwise
return true;
}
void ThreeBy3::display(std::ostream& os)
{
const int W = 2;
os << std::setprecision(1) << std::fixed;
// display the 3 x 3 array on the passed output stream
for(int i = 0; i < 3; i++)
{
    for(int j = 0; j < 3; j++)
        cout << square[i][j] << " ";
    cout << endl;  
}
}
int assign_magic_squares()
{
static const int SQ = 3;
std::ifstream ifs;
std::string path = "path-to/msq2.txt";
int iv;
int ix, jx;
int ms[3][3] = {{0}, {0}, {0}};
int rcd = 0;
ThreeBy3 *msq;
ifs.open(path);
if (!ifs)
{
cout << "file not found:" << path << endl;
return -1;
}
while (!ifs.eof())
{
++rcd;
// read a 3 by 3 matrix into ms; goto the eof label on ifs.eof()
// create an instance of ThreeBy3
// display the array in the ThreeBy3 instance
//
if (msq->isMagicSquare())
{
cout << " is a magic square" << endl;
}
else
{
cout << " is NOT a magic square" << endl;
}
}
eof:
ifs.close();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote