Question: Language is C++ I have to create a Magic Square Solver based on code I have already created and I'm not sure what to do
Language is C++
I have to create a Magic Square Solver based on code I have already created and I'm not sure what to do since my code is incomplete. Could you modify my code so that it works with the assignment and teach me what to do? I would appreciate it if you explained each step of the process and used comments to explain what is going on; so that I can learn what the code is doing. Please read the assignment and don't skip over anything. I will happily upvote if your expertise really helps me.
MY CODE:
#include
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 userFile) { int counter = 0; string entries;
ifstream fin; fin.open(userFile);
while (!fin.eof()) { fin >> entries; counter++; } counter--; return counter; }
bool solvePuzzle(int** puzzle, int size) {
}
ASSIGNMENT:



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
Get step-by-step solutions from verified subject matter experts
