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 and intMain() to make my program work. If you need to make small modifications to other functions, be sure to explain why. REMEMBER IT IS READING FROM A TEXT FILE! THE ONLY INPUT FROM THE USER IS ASKING FOR A FILE NAME!
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 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 solvePuzzle(int** puzzle,int n) { if (isCompletePuzzle(puzzle, n)) 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); if (isCompletePuzzle(magicSquare, n)){ cout :[" :/"
cout
if (solvePuzzle(magicSquare, n) == true) { cout
cout
}
TEXT FILE AND EXAMPLE OF OUTPUT:
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
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