Question: this my Answer but it has some issues #include #include #include #include stdafx.h #include #include #include #include #include #include #include #include #include using namespace std;

 this my Answer but it has some issues #include #include #include#include "stdafx.h" #include #include #include #include #include #include #include #include #include usingnamespace std; string inputFileName = "magic.txt"; class MagicSquareClass { public: uint16_t inputSquareSize,

this my Answer but it has some issues

#include

#include

#include

#include "stdafx.h"

#include

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

string inputFileName = "magic.txt";

class MagicSquareClass {

public:

uint16_t inputSquareSize,

magicSum;

vector > inputSquareVector;

void displayErrorAndExit(string inputFileName);

bool readSquareFile(vector>&, uint16_t&, ifstream&);

bool isSquareMagic(vector>&, uint16_t, uint16_t&);

void displaySquare(vector>&, uint16_t);

bool checkSquareCriteria(vector>&, uint16_t);

};

void MagicSquareClass::displayErrorAndExit(string errorString) {

cerr

cerr

getchar();

exit(EXIT_FAILURE);

}

bool MagicSquareClass::readSquareFile(vector>& inputSquareVector, uint16_t&squareSize, ifstream&fileStreamObj) {

if (!(fileStreamObj >> squareSize)) {

cout

return true;

}

inputSquareVector.clear();

inputSquareVector.resize(squareSize, vector(squareSize, 0));

for (auto &row : inputSquareVector)

for (auto &col : row)

fileStreamObj >> col;

return(false);

}

void MagicSquareClass::displaySquare(vector>& inputSquareVector, uint16_t squareSize) {

for (auto &row : inputSquareVector) {

for (auto &col : row)

cout

cout

}

cout

}

bool MagicSquareClass::isSquareMagic(vector>& square, uint16_t squareSize, uint16_t & magicSum) {

magicSum = (uint16_t)((squareSize* (pow(squareSize, 2) + 1)) / 2);

if (!(checkSquareCriteria(square, squareSize)))

return false;

uint16_t sumRow, sumCol;

for (uint16_t row = 0; row

sumRow = sumCol = 0;

for (uint16_t col = 0; col

sumRow += square[row][col];

sumCol += square[col][row];

}

if ((sumRow != magicSum) && (sumCol != magicSum))

return false;

}

uint16_t sumDiagRtoL = 0, sumDiagLtoR = 0;

for (uint16_t row = 0; row

sumDiagLtoR += square[row][row];

sumDiagRtoL += square[row][squareSize - row - 1];

}

if ((sumDiagRtoL != magicSum) && (sumDiagLtoR != magicSum))

return false;

}

bool MagicSquareClass::checkSquareCriteria(vector>& square, uint16_t squareSize) {

uint16_t squareSizeSquared = squareSize * squareSize;

vector nuberFoundCount((squareSize * squareSize) + 1, 0);

for (uint16_t row = 0; row

for (uint16_t col = 0; col

if ((square[row][col]) > (squareSizeSquared)) {

cout

return(false);

}

if (((++nuberFoundCount[square[row][col]]) == 2)) {

cout

return(false);

}

}

for (int numberSearch = 1; numberSearch

if (nuberFoundCount[numberSearch]==0) {

cout

return(false);

}

return true;

}

int main() {

MagicSquareClass magicSquareObj;

ifstream inputFileStreamObj;

inputFileStreamObj.open(inputFileName, ios::in);

if (inputFileStreamObj.fail())

magicSquareObj.displayErrorAndExit(inputFileName.append("file coud not be opened ! "));

bool doneBool;

do {

if (!(doneBool = magicSquareObj.readSquareFile(magicSquareObj.inputSquareVector, magicSquareObj.inputSquareSize, inputFileStreamObj))) {

magicSquareObj.displaySquare(magicSquareObj.inputSquareVector, magicSquareObj.inputSquareSize);

if (!(magicSquareObj.isSquareMagic(magicSquareObj.inputSquareVector, magicSquareObj.inputSquareSize, magicSquareObj.magicSum)))

cout

else

cout

}

system("pause");

system("cls");

} while (!doneBool);

inputFileStreamObj.close();

cout

system("pause");

return 0;

}

It's Magioc A friend tells you about a nifty type of square matrix called a "Magic Square" This friend bets you that you can'tcode up a C+ programthat can determine "Magic Squares" Matrixes A Matrixis a 2 dimensionalarray A squareMatrix has an equal amount of rows and columns An N Matrix is a squarematrix with N rows and N columns (N x N) Magic Square An N Magic Square is a N matrix (i.e... a square N x N matrix) with positive integer values for its elements (values in X's)) An N Magic Square has the following properties 1. 2. 3. It is an N Matrix The integer elements values range from 1 to N squared (N*N) i.e... 1.. N*N The rows, columns and diagonalsall have the same sum, called the Magic Square Sum: Magic Square Sum- [n (n2+1)]/2 4. All the integer elements in the magic squareare unique i.e... no two elements can have the same positiveinteger Example of a 5 Magic Square: 11 24 07 20 03 04 12 25 08 16 17 05 13 21 09 10 18 01 14 22 23 06 19 02 15 Criteria Check: It is a 5x 5 Matrix, so it's a 5 square. The integer elements values range from 1 to (5 x 5) .e... 1.. 25 All the integer element values aredistincti.e... different from each other, no two are equal The rows, columns anddiagonalsall have the same magic sum of 65, so the Magic Sum is 65 All the above criteria aretrue, so it's a 5 magic squarewith a magic sum of 65

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!