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
using namespace std;
string inputFileName = "magic.txt";
class MagicSquareClass {
public:
uint16_t inputSquareSize,
magicSum;
vector > inputSquareVector;
void displayErrorAndExit(string inputFileName);
bool readSquareFile(vector
bool isSquareMagic(vector
void displaySquare(vector
bool checkSquareCriteria(vector
};
void MagicSquareClass::displayErrorAndExit(string errorString) {
cerr
cerr
getchar();
exit(EXIT_FAILURE);
}
bool MagicSquareClass::readSquareFile(vector
if (!(fileStreamObj >> squareSize)) {
cout
return true;
}
inputSquareVector.clear();
inputSquareVector.resize(squareSize, vector
for (auto &row : inputSquareVector)
for (auto &col : row)
fileStreamObj >> col;
return(false);
}
void MagicSquareClass::displaySquare(vector
for (auto &row : inputSquareVector) {
for (auto &col : row)
cout
cout
}
cout
}
bool MagicSquareClass::isSquareMagic(vector
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
uint16_t squareSizeSquared = squareSize * squareSize;
vector
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; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started