Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment, implement and use the methods for a class called LoShuMagic

ID: 3777911 • Letter: F

Question

For this assignment, implement and use the methods for a class called LoShuMagicSquare that will be used to verify solutions for Lo Shu Magic Squares, write the complete program in C++.

The data members for the class are:

an integer symbolic constant that holds the maximum number of rows in a two dimensional array. Use a value of 3.

an integer symbolic constant that holds the maximum number of columns in a two dimensional array. Use a value of 3.

a two-dimensional array of integers with 3 rows and 3 columns that will hold the possible solution

Constructor:

This class has one constructor that should initialize the two-dimensional array of integers. It takes no arguments.

The array should be initialized to hold 0s in all 9 elements.

Methods:

void fillSquare( const char [] )

This is a public method that will fill the two-dimensional array data member with information that is read from a file. It takes 1 argument: an array of constant characters that represents the name of a file that holds the information to place into the array. It returns nothing.

This method should open the file that's name is passed in via the argument and verify that it opened correctly. Once the file has been correctly opened, a nested loop should be used to read the values into the array. After all of the values have been placed in the array, the file should be closed.

The input file contains exactly 9 numbers, arranged in 3 rows of 3 columns each, and separated by whitespace. For example:

void printSquare()

This is a public method that will display the two-dimensional array to the screen as 3 rows or 3 columns (the same way the numbers appear in the input file). It takes no arguments and returns nothing.

bool isMagic()

This is a public method that will determine if the two-dimensional array contains a valid Lo Shu Magic Square solution. It takes no arguments and returns a boolean: true if the solution is valid or false if the solution is not valid.

As mentioned earlier, a valid solution is one where all 9 digits are used one time and all of the rows, columns, and diagonals add up to the same value.

For main() use:

There are 4 input files that are being referenced in main(). They are:

loshu_puzzle1.txt

---------

loshu_puzzle2.txt

---------

loshu_puzzle3.txt

---------

loshu_puzzle4.txt

---------

Helpful info:

The Lo Shu Magic Square is an arrangement of the digits 1 through 9 in a 3x3 grid such that:

each digit is only used one time

the sum of the each row, column, and diagonal all add up to the same value.

Explanation / Answer

// C++ code

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>

using namespace std;

//Place the class definition after this line

class LoShuMagicSquare

{
public:
   int MAX_ROW = 3;
   int MAX_COL = 3;
   int square[3][3];


void fillSquare(const char filename[] )

{
    ifstream inputFile (filename);
    if (inputFile.is_open())
  
   {
   for (int i = 0; i < MAX_ROW; ++i)
  
   {
       for (int j = 0; j < MAX_COL; ++j)
      
       {
           inputFile >> square[i][j];
       }
   }
   inputFile.close();
   }

   else cout << "Unable to open file";

}


void printSquare()

{
        for (int i = 0; i < MAX_ROW; ++i)
  
   {
       for (int j = 0; j < MAX_COL; ++j)
      
       {
           cout << square[i][j] << " ";
       }

       cout << endl;
   }
}

bool isMagic()
{
        //For diagonal elements
        int ismagicSquare = 1;
   int sum = 0;
   for (int i = 0; i < MAX_ROW; i++)
   {
   for (int j = 0; j < MAX_COL; j++)
   {
   if (i == j)
   sum = sum + square[i][j];
   }
   }
     
   //For Rows
   for (int i = 0; i < MAX_ROW; i++)
   {
   int sum1 = 0;
   for (int j = 0; j < MAX_COL; j++)
   {
   sum1 = sum1 + square[i][j];
   }
   if (sum == sum1)
   ismagicSquare = 1;
   else
   {
   ismagicSquare = 0;
   break;
   }
   }
     
   //For columns
   for (int i = 0; i < MAX_ROW; i++)
   {
   int sum2 = 0;
   for (int j = 0; j < MAX_COL; j++)
   {
   sum2 = sum2 + square[j][i];
   }
   if (sum == sum2)
   ismagicSquare = 1;
   else
   {
   ismagicSquare = 0;
   break;
   }
   }
     
   int count[9] = {0};
   for (int i = 0; i < MAX_ROW; ++i)
   {
      for (int j = 0; j < MAX_COL; ++j)
      {
         count[square[i][j]]++;
      }
   }

   for (int i = 0; i < 9; ++i)
   {
          if(count[i] > 1)
              ismagicSquare = 0;
   }

   if (ismagicSquare == 1)
   return true;
   else
   return false;
}
};


int main()

{
   //Create a LoShuMagicSquare object that will be used to test the 4 puzzles
   LoShuMagicSquare puzzle;


   //Puzzle 1 using loshu_puzzle1.txt. The object will be filled, displayed,
   //and then tested to see if it is a valid solution

   cout << "Puzzle 1:" << endl << endl;
   puzzle.fillSquare( "loshu_puzzle1.txt");

   puzzle.printSquare();

   cout << endl << "Is it magic? " << ( puzzle.isMagic() ? "Yes": "No" ) << endl << endl << endl;


   //Puzzle 2 using loshu_puzzle2.txt. The object will be filled, displayed,
   //and then tested to see if it is a valid solution
   cout << "Puzzle 2:" << endl << endl;

   puzzle.fillSquare( "loshu_puzzle2.txt");

   puzzle.printSquare();

   cout << endl << "Is it magic? " << ( puzzle.isMagic() ? "Yes": "No" ) << endl << endl << endl;


   //Puzzle 3 using loshu_puzzle3.txt. The object will be filled, displayed,
   //and then tested to see if it is a valid solution
   cout << "Puzzle 3:" << endl << endl;

   puzzle.fillSquare( "loshu_puzzle3.txt");

   puzzle.printSquare();

   cout << endl << "Is it magic? " << ( puzzle.isMagic() ? "Yes": "No" ) << endl << endl << endl;


   //Puzzle 4 using loshu_puzzle4.txt. The object will be filled, displayed,
   //and then tested to see if it is a valid solution
   cout << "Puzzle 4:" << endl << endl;

   puzzle.fillSquare( "loshu_puzzle4.txt");

   puzzle.printSquare();

   cout << endl << "Is it magic? " << ( puzzle.isMagic() ? "Yes": "No" ) << endl << endl << endl;


return 0;
}

/*
output:

Puzzle 1:

4 9 2
3 5 7
8 1 6

Is it magic? Yes


Puzzle 2:

2 9 1
3 5 8
7 4 6

Is it magic? No


Puzzle 3:

5 5 5
5 5 5
5 5 5

Is it magic? No


Puzzle 4:

2 7 6
9 5 1
4 3 8

Is it magic? Yes

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote