Question: In C++, I'm working on a magic square solver. I need help reading a .txt file of 7x7 digits. In order for me to pass,

In C++, I'm working on a magic square solver. I need help reading a .txt file of 7x7 digits. In order for me to pass, I have to enter the third line of the solved puzzle, with a space between each number, to complete the quiz. Below is the code I currently have.

More details of assignment - Backtracking and Recursion

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 = n ( n2 + 1)

For example, here is a magic square of order 3.

8

1

6

3

5

7

4

9

2

The constant is ( 3 ) ( 9 + 1 ) = 15.

  • Display a friendly greeting to the user
  • Prompt the user for a filename (if the file wasnt provided on the command line)
  • Accept that filename
  • Attempt to open the file
  • Read the numbers in that file
  • Determine the dimensionality of the puzzle (guaranteed to be a perfect square)
  • Create a square two-dimensional array containing these values
    • If there are multiple solutions, stop at the first one found
  • Create the corresponding Magic Square using the recursive algorithm given

magic.txt file

 0 0 20 37 12 29 4 0 27 0 0 0 11 0 0 0 26 43 18 0 0 9 33 1 25 49 17 41 0 0 32 7 24 0 0 0 39 0 31 0 23 0 0 21 38 13 0 0 0

My Current Code

#include #include #include

using namespace std; typedef vector > Matrix;

int readSquare(istream& is, Matrix& matrix) { int num; matrix.resize(1); matrix[0].clear(); cout << "Welcome to Magic Square Game." << endl; cout << "Enter integer from the file:" << endl;

string line; getline(is, line); istringstream iss(line); while (iss >> num) { matrix[0].push_back(num); }

int sz = matrix[0].size(); matrix.resize(sz); for (int row = 1; row < sz; ++row) { for (int col = 0; col < sz; ++col) { is >> num; matrix[row].push_back(num); } } getline(is, line);

return (is.good() ? sz : -1); }

void printSquare(ostream& os, const Matrix& matrix) { for (int row = 0; row < matrix.size(); ++row) { for (int col = 0; col < matrix[row].size(); ++col) { os << matrix[row][col] << ' '; } os << ' '; } }

int vectorSum(const vector& vec) { int sum = 0; for (int i = 0; i < vec.size(); ++i) { sum += vec[i]; } return sum; }

bool checkMagic(const Matrix& matrix) { bool result = false; int sz = matrix.size();

if (sz == 0) return false; if (matrix[0].size() != sz) return false;

int sum = vectorSum(matrix[0]);

for (int i = 1; i < sz; ++i) { if (matrix[i].size() != sz) return false; if (sum != vectorSum(matrix[i])) return false; }

for (int col = 0; col < sz; ++col) { int colSum = 0; for (int row = 0; row < sz; ++row) { colSum += matrix[row][col]; } if (sum != colSum) return false; }

int d1Sum = 0, d2Sum = 0; for (int i = 0; i < sz; ++i) { d1Sum += matrix[i][i]; d2Sum += matrix[i][sz - i - 1]; } if (d1Sum != sum || d2Sum != sum) return false; return true; }

int main(int argc, char** argv) { Matrix matrix; while (readSquare(cin, matrix) > 0) { printSquare(cout, matrix); cout << (checkMagic(matrix) ? "is" : "is not") << " magic ";

cin.ignore(1000, ' '); } }

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!