Sale!

CSCI 240 Program 10 Classes solution

$30.00 $25.50

Category:

Description

5/5 - (5 votes)

Overview
The Lo Shu Magic Square is an arrangement of the digits 1 through 9 in a 3×3 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

is a valid Lo Shu Magic Square because it uses all 9 digits one time, and all of the rows, columns, and diagonals add up to the same value (15).

uses all 9 digits one time, but the rows, columns, and diagonals do not add up to the same value. Therefore, it is not a valid Lo Shu Magic Square.

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.

LoShuMagicSquare class

Data Members

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:

4 9 2
3 5 7
8 1 6

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.

 4 9 2
 3 5 7
 8 1 6

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.

main()

A driver program called pgm10fall16driver.cpp has been written to test the LoShuMagicSquare class. It can be copied from Blackboard or the course website.

Add the class definition and the methods to the driver program in the appropriate locations.

The C++ code that is in main() may not be altered. However, as the program is being developed, the code can be commented out so that it is not all running at one time. A good way to proceed for this assignment is to first write the constructor and printSquare method. All of the puzzles except for the first one should be commented out. With the first puzzle, comment out the calling statement for the fillSquare method and the cout statement that contains the calling statement for the isMagic method. Compile and Run the program. If the code was written correctly, the output should be 3 rows with 3 columns of zeros. If the output doesn’t match, double check the constructor and printSquare methods for mistakes.

Once all of the zeros are correctly displayed, write the fillSquare method and un-comment the calling statement in main(). Compile and Run the program. The output should match what is shown below if the fillSquare method was written correctly. If the output doesn’t match, double check the fillSquare and printSquare methods.

Once the puzzle displays correctly, write the isMagic method and un-comment the cout statement in main that calls the method. Compile and Run the program. Again, the output should match what is show below. If it does not, check the isMagic method for mistakes.

Once the first puzzle is correctly displayed and verified, un-comment the code for puzzle 2 and verify that it works. Continue in this manner until all 4 puzzles are running correctly.

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

As with previous assignments, the 4 files should be downloaded from Blackboard or the course website and saved in the same directory as the CPP file. (For Mac users, you will probably have to put in the set of double quotes(“”), find where the files have been saved on your computer, and then drag and drop the files in between the quotes. It should place the name of the files, including the complete path of where it is located on your computer, between the quotes.)

Programming Notes

  1. Each method must have a documentation box like a function.
  2. Hand in a copy of your source code using Blackboard.

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

Extra Credit

For up to 5 points of extra credit, add code to the printSquare method so that the puzzles display in something that resembles a grid. For example:

-------------
| 4 | 9 | 2 |
-------------
| 3 | 5 | 7 |
-------------
| 8 | 1 | 6 |
-------------

Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly. In other words, don’t take short cuts in the rest of the program because it is assumed that 5 extra points will be awarded.

CSCI 240 Program 10