Question: LANGUAGE IS C++ My program is supposed to read from a text file and solve a Magic Square. PLEASE FAMILIARIZE YOURSELF WITH MAGIC SQUARES BEFORE
LANGUAGE IS C++
My program is supposed to read from a text file and solve a Magic Square. PLEASE FAMILIARIZE YOURSELF WITH MAGIC SQUARES BEFORE TRYING TO HELP!
I am struggling with getting my program to solve the Magic Square and displaying the solution. Please help me with my solvePuzzle() function to make my program work properly. I think the only part of the code that needs modifying is the solvePuzzle() function. I think I need to utilize backtracking, but I do not know how. REMEMBER IT IS READING FROM A TEXT FILE! THE ONLY INPUT FROM THE USER IS ASKING FOR A FILE NAME!
PLEASE POST YOUR FULL CODE AND A SCREENSHOT OF IT WORKING! The last four times I have asked this question the experts have messed up the code or didn't post the fixed code. I will downvote any code that does not work, so please test it yourself with the provided text file. I have gotten very frustrated looking for help.
MY CODE:
#include
void testFile(string file) { ifstream infile; infile.open(file.c_str()); if (!infile) { cout
bool isCompletePuzzle(int** puzzle, int size) { for (int i = 0; i
int magicConstant(int n) { // Calculate the Magic Constant using formula M = 1/2 n (n^2 + 1) int M = 0; M = (n * n); M += 1; M *= n; M /= 2; return M; }
bool rowVal(int** puzzle, int row, int n) { int counter = 0; int val = magicConstant(n); for (int i = 0; i
bool colVal(int** puzzle, int col, int n) { int counter = 0; int val = magicConstant(n); for (int i = 0; i
bool leftDiagVal(int** puzzle, int n) { int counter = 0; int val = magicConstant(n); for (int i = 0; i
bool rightDiagVal(int** puzzle, int n) { int counter = 0; int val = magicConstant(n); for (int i = 0; i
bool isValidPuzzle(int** puzzle, int size) { //test each column, row, and diagonal to see if it adds up to the magic number for (int i = 0; i
int countEntries(string fileName) { int counter = 0; string entries;
ifstream fin; fin.open(fileName);
while (!fin.eof()) { fin >> entries; counter++; } return counter; }
int** magic(string file, int length) {
ifstream fin; fin.open(file);
int** result = new int* [length]; for (int i = 0; i > result[i][j]; } return result; }
void displaySquare(int** square, int n) { for (int r = 0; r
bool solvePuzzle(int** puzzle,int n) { if (isCompletePuzzle(puzzle, n) == true) { return true; } else { for (int r = 0; r
} } } } }
int main(int argc, char* argv[]) { string fileName = ""; int n = 0; int** magicSquare;
cout
if (argc == 2) { fileName = argv[1]; } else { cout
testFile(fileName); int root = sqrt(countEntries(fileName)); if (root * root != countEntries(fileName)) { cout
n = sqrt(countEntries(fileName));
magicSquare = magic(fileName, n);
cout
displaySquare(magicSquare, n);
solvePuzzle(magicSquare, n);
if (solvePuzzle(magicSquare, n) == true) { cout :/" :["
solvePuzzle(magicSquare, n); displaySquare(magicSquare, n); } else { cout
cout
}
INPUT TEXT FILE AND THE DESIRED OUTPUT:

Don't let me down
For the 55 input file Your program should find the first solution in a couple of seconds
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
