Question: LANGUAGE IS C++ I'm writing code that is supposed to display and solve MagicSquares from a text file, but I'm struggling with the solvePuzzle function.

LANGUAGE IS C++

I'm writing code that is supposed to display and solve MagicSquares from a text file, but I'm struggling with the solvePuzzle function. I think he wants us to utilize backtracking, but I don't know how. I'm not exactly sure how to implement solvePuzzle. If you could help me with my code and explain what I may be doing wrong I would really appreciate it. Please send a picture of the code working. I have had many experts submit code that doesn't work.

MY CODE:

#include #include #include #include using namespace std;

const int MAX_SIZE = 100;

char puzzle[MAX_SIZE][MAX_SIZE]; int puzzle_int[MAX_SIZE][MAX_SIZE];

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++; } //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 findCandidate(int** puzzle, int n) { for (int r = 0; r

bool solvePuzzle(int** puzzle, int n) { if (!isValidPuzzle(puzzle, n)) { 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); if (isCompletePuzzle(magicSquare, n)){ cout :[" :/"

cout

THE ASSIGNMENT (TO BETTER HELP YOU UNDERSTAND WHAT I AM TRYING):

LANGUAGE IS C++ I'm writing code that is supposed to display and

solve MagicSquares from a text file, but I'm struggling with the solvePuzzle

function. I think he wants us to utilize backtracking, but I don't

MAGIC SQUARES Backtracking and Recursion Pretty much any numeric puzzle can be solved with a handful of Boolean methods: isCompletePuzzle () // returns true if every spot in the puzzle has a value, false otherwise isValidPuzzle() // returns true if the puzzle contains no counterexamples, false otherwise The solution strategy is implemented in a third method solvePuzzle() which executes the following recursive algorithm: // base cases Is the puzzle valid? If not, return false. Is the puzzle complete? If so, return true (valid and complete!) // current candidate is valid and incomplete, so we have a recursive case Locate the first blank space in the puzzle. In a loop over all potential candidates, Drop in a candidate. Make a recursive call to solvePuzzle () with the updated puzzle. Got back a true? Return true. Otherwise, continue the loop (try the next candidate and make another recursive ca Completed the loop? None of them worked. Reset the candidate cell to blank and return fa A normal Magic Square of dimensionality n contains the numbers 1 through n2 ordered in such a way that the sum of each row, each column, and each diagonal is the same value. The "magic constant" for such a square is computed as M=1/2n(n2+1) For example, here is a magic square of order 3. The constant is 1/2(3)(9+1)=15. HOMEWORK: Magic Square Solver Write a C++ program that performs the following tasks: - Display a friendly greeting to the user - Prompt the user for a filename - Accept that filename - Attempt to open the file - If the file fails to open, provide an error message and exit - Read the numbers in that file - Determine the number of entries in the file (should be a perfect square) If that number isn't a perfect square, provide an error message and exit - Create a square two-dimensional array containing these values - Create the corresponding Magic Square using the recursive algorithm given - Display the solved puzzle, or an appropriate message if no solution is found The entries in the file will be base-10 integers. Display the result using only base-10 integers. EXTRA CREDIT: Modify your program to search for all possible Magic Squares (based on the given input file) and display the number of solutions found. DELIVERABLES: Your C++ source code and a screen shot of your code in action, using as input the 55 input file given below. For this 33 input file (an empty Magic Square) 000000000 Your program should find the first solution 294753618 in less than a second. The extra credit "count" code should find eight solutions. They are all reflections and rotations of the pattern above. For the 33 input file 800000000 Your program should find the first solution (which is also given on the previous page) 816 357 492 in less than a second. If you run the extra credit "count" code on this input file, the answer is 2. Dnotha EuE innow tin Your program should find the first solution in a couple of seconds

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!