Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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;

image text in transcribedimage text in transcribedimage text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

Discuss career development and career development methods.

Answered: 1 week ago