Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #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 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:

image text in transcribed

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

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

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students also viewed these Databases questions