Use C++ You will determine if a magic square is valid or not. The size of the magic square will be some number between 3 and 21 inclusive. This program will make use of a two dimension array. A Lo Shu Magic Square is a grid with 3 rows and 3 columns as shown here: +---+---+---+ |  4  |  9  |  2  | +---+---+---+ |  3  |  5  |  7  | +---+---+---+ |  8  |  1  |  6  | +---+---+---+ The 3x3 Lo Shu Magic Square has the following properties: The grid contains all of the numbers 1 through 9. Each digit 1 though 9 is in the puzzle and is in the puzzle just once. The range of values will be different for puzzles of sizes other than 3. The sum of each row, each column, and each diagonal all add up to the same number - 15.  Your program will simulate a magic square using a two-dimensional 21 x 21 array of int values. Your program needs to be able to support input files of 3x3 up to 21x21. For a 3x3 magic square the code for testing if a row or column adds up to the requirement sum is fairly straight forward. The sum is 15. The code for testing the diagonal values and for testing that the square contains all of the numbers 1 through n is trickier (n is size * size). You need to sit down with a paper and a pencil and work out the strategy.  For the diagonal going from the top left to the bottom right of the magic square you need to add up square[n][n] where n goes from 0 to 2. The diagonal going from the lower left to the upper right will require that you add up square[2][0], square[1][1], and square[[0][2]. Note that the first array subscript is going down (2, then 1, then 0) while the second array subscript is going up (0, 1 and then 2). For both of these you need to use a loop. For the testing of all values you need to check to see if any element has a value of 1, If it does you then check to see if any array element has a 2, and so on to check for all of the values 1 through n. If any of the elements 1 through n are not found you can stop testing and declare that it isn't a valid magic square. For other sizes other than 3 the value that the rows, columns and diagonals are calculated as: size * (size * size + 1) / 2 The numbers you need to check will not be 1 through 9 but rather 1 through (size * size). A 3x3 magic square would contain exactly 1 of each of the values 1 through 9 (size * size for size 3). A 5x5 magic square would contain the all of the numbers 1 through 25 (size * size where size is 5). In all cases he numbers in the magic square are 1 through size * size. Each number 1 through size * size must be in the magic square exactly one time. Validate function The validate function has the following signature: const int MAX_SIZE = 21; bool validateSquare(const int square[MAX_SIZE] [MAX_SIZE], int size); Note that MAX_SIZE is a global const int value. This is the only global variable you are allowed to have in the program. The validateSquare function will return true if the square array contains a valid magic square and false tests fail. Read function The read function will read the contents of a file into the passed in 21x21 array. If the file exists you can assume it contains valid date. Check to see if the file is opened properly or not. Here is the signature: int readSquare(int square[MAX_SIZE][MAX_SIZE], string inputFileName); The array you are reading into is passed as the first argument to the function. The second argument is the input file name. If the input file does not exist your function needs to return a value of 0. If the file is opened properly you need to read the contents of the file into the array and return the actual size of the magic square. Close once finished. The input file format for a 3x3 puzzle would be: 3 4 9 2 3 5 7 8 1 6 Note that the first value read in is the size of the puzzle. This is a 3x3 puzzle. For a 5x5 it would be: 5 9 2 25 18 11 3 21 19 12 10 22 20 13 6 4 16 14 7 5 23 15 8 1 24 17 Note that the first value read in (5 above) is the size of the puzzle. This is a 5x5 puzzle. The main function You will need your main function and you will need a function that displays the contents of the magic square, one row per line of output. The pseudocode for main is: Prompt file name. Read in the input file name. Call the readSquare function to read the magic square from input file. If the readSquare function worked you need to: 1. Call the display function and display magic square 2. Call the validateSquare funciton. 3. If magic square valid - Display "Valid magic square" 4. If magic square not valid - Display "Invalid magic square" else 1. Display file could not be opened  display magic square function   Sample run 1 (valid data) Contents of cin: square1.txt Contents of square1.txt: 3 4 9 2 3 5 7 8 1 6 Here is the output to cout: Enter input file name Magic square 4 9 2 3 5 7 8 1 6 Valid magic square Sample run 3 (invalid magic square) Contents of cin: square2.txt Contents of square2.txt: 3 2 9 4 3 5 7 8 1 6 Here is the output to cout: Enter input file name Magic square 2 9 4 3 5 7 8 1 6 Invalid magic square

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Use C++

You will determine if a magic square is valid or not. The size of the magic square will be some number between 3 and 21 inclusive.

This program will make use of a two dimension array.

A Lo Shu Magic Square is a grid with 3 rows and 3 columns as shown here:

+---+---+---+

|  4  |  9  |  2  |

+---+---+---+

|  3  |  5  |  7  |

+---+---+---+

|  8  |  1  |  6  |

+---+---+---+

The 3x3 Lo Shu Magic Square has the following properties:

  • The grid contains all of the numbers 1 through 9. Each digit 1 though 9 is in the puzzle and is in the puzzle just once. The range of values will be different for puzzles of sizes other than 3.
  • The sum of each row, each column, and each diagonal all add up to the same number - 15. 

Your program will simulate a magic square using a two-dimensional 21 x 21 array of int values. Your program needs to be able to support input files of 3x3 up to 21x21.

For a 3x3 magic square the code for testing if a row or column adds up to the requirement sum is fairly straight forward. The sum is 15.

The code for testing the diagonal values and for testing that the square contains all of the numbers 1 through n is trickier (n is size * size). You need to sit down with a paper and a pencil and work out the strategy. 

For the diagonal going from the top left to the bottom right of the magic square you need to add up square[n][n] where n goes from 0 to 2. The diagonal going from the lower left to the upper right will require that you add up square[2][0], square[1][1], and square[[0][2]. Note that the first array subscript is going down (2, then 1, then 0) while the second array subscript is going up (0, 1 and then 2). For both of these you need to use a loop.

For the testing of all values you need to check to see if any element has a value of 1, If it does you then check to see if any array element has a 2, and so on to check for all of the values 1 through n. If any of the elements 1 through n are not found you can stop testing and declare that it isn't a valid magic square.

For other sizes other than 3 the value that the rows, columns and diagonals are calculated as:

size * (size * size + 1) / 2

The numbers you need to check will not be 1 through 9 but rather 1 through (size * size). A 3x3 magic square would contain exactly 1 of each of the values 1 through 9 (size * size for size 3). A 5x5 magic square would contain the all of the numbers 1 through 25 (size * size where size is 5). In all cases he numbers in the magic square are 1 through size * size. Each number 1 through size * size must be in the magic square exactly one time.

Validate function

The validate function has the following signature:

const int MAX_SIZE = 21;

bool validateSquare(const int square[MAX_SIZE]

[MAX_SIZE], int size);

Note that MAX_SIZE is a global const int value. This is the only global variable you are allowed to have in the program.

The validateSquare function will return true if the square array contains a valid magic square and false tests fail.

Read function

The read function will read the contents of a file into the passed in 21x21 array. If the file exists you can assume it contains valid date. Check to see if the file is opened properly or not.

Here is the signature:

int readSquare(int square[MAX_SIZE][MAX_SIZE], string inputFileName);

The array you are reading into is passed as the first argument to the function. The second argument is the input file name.

If the input file does not exist your function needs to return a value of 0.

If the file is opened properly you need to read the contents of the file into the array and return the actual size of the magic square.

Close once finished.

The input file format for a 3x3 puzzle would be:

3

4 9 2

3 5 7

8 1 6

Note that the first value read in is the size of the puzzle. This is a 3x3 puzzle.

For a 5x5 it would be:

5

9 2 25 18 11

3 21 19 12 10

22 20 13 6 4

16 14 7 5 23

15 8 1 24 17

Note that the first value read in (5 above) is the size of the puzzle. This is a 5x5 puzzle.

The main function

You will need your main function and you will need a function that displays the contents of the magic square, one row per line of output.

The pseudocode for main is:

Prompt file name. Read in the input file name. Call the readSquare function to read the magic square from input file. If the readSquare function worked you need to:

1. Call the display function and display magic square

2. Call the validateSquare funciton.

3. If magic square valid - Display "Valid magic square"

4. If magic square not valid - Display "Invalid magic square"

else 1. Display file could not be opened 

display magic square function

 

Sample run 1 (valid data)

Contents of cin:

square1.txt

Contents of square1.txt:

3

4 9 2

3 5 7

8 1 6

Here is the output to cout:

Enter input file name

Magic square

4 9 2

3 5 7

8 1 6

Valid magic square

Sample run 3 (invalid magic square)

Contents of cin:

square2.txt

Contents of square2.txt:

3

2 9 4

3 5 7

8 1 6

Here is the output to cout:

Enter input file name

Magic square

2 9 4

3 5 7

8 1 6

Invalid magic square

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education