Fall 2019 CMSC 140 Programming Project 5: Magic Square
Chapter(s) Covered:
· Chapter 1-8
Concepts tested by the program:
· Working with one dimensional parallel a
ays
· Use of functions
· Use of loops and conditional statements
Project Description
The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below.
The Lo Shu Magic Square has the following properties:
· The grid contains the numbers 1 – 9 exactly
· The sum of each row, each column and each diagonal all add up to the same number.
This is shown below:
Write a program that simulates a magic square using 3 one dimensional parallel a
ays of integer type.
Each one the a
ays co
esponds to a row of the magic square.
The program asks the user to enter the values of the magic square row by row and informs the user if the grid is a magic square or not.
See the sample outputs for more clarification.
Project Specifications
Input for this project:
· Values of the grid (row by row)
Output for this project:
· Whether or not the grid is magic square
· Programmer’s full name
· Project numbe
· Project due date
Processing Requirements
Use the following template to start your project:
#include
using namespace std;
Global constants
const int ROWS = 3;
The number of rows in the a
ay
const int COLS = 3;
The number of columns in the a
ay
const int MIN = 1;
The value of the smallest numbe
const int MAX = 9;
The value of the largest numbe
Function prototypes
ool isMagicSquare(int a
ayRow1[], int a
ayRow2[], int a
ayRow3[], int size);
ool checkRange(int a
ayRow1[], int a
ayRow2[], int a
ayRow3[], int size, int min, int max);
ool checkUnique(int a
ayRow1[], int a
ayRow2[], int a
ayRow3[], int size);
ool checkRowSum(int a
ayrow1[], int a
ayrow2[], int a
ayrow3[], int size);
ool checkColSum(int a
ayrow1[], int a
ayrow2[], int a
ayrow3[], int size);
ool checkDiagSum(int a
ayrow1[], int a
ayrow2[], int a
ayrow3[], int size);
void fillA
ay(int a
ayRow1[], int a
ayRow2[], int a
ayRow3[], int size);
void showA
ay(int a
ayrow1[], int a
ayrow2[], int a
ayrow3[], int size);
int main()
{
/* Define a Lo Shu Magic Square using 3 parallel a
ays co
esponding XXXXXXXXXXto each row of the grid *
int magicA
ayRow1[COLS], magicA
ayRow2[COLS], magicA
ayRow3[COLS];
Your code goes here
return 0;
}
Function definitions go here
Create and use following functions:
· void fillA
ay(int a
ayrow1[], int a
ayrow2[], int a
ayrow3[], int size) - Accepts 3 int a
ays and a size as arguments, and fills the a
ays out with values entered by the user. First argument co
esponds to the first row of the magic square, second argument to the second row and the third argument to the third row of the magic square
· void showA
ay(int a
ayRow1[], int a
ayRow2[], int a
ayRow3[], int size) - accepts 3 int a
ays and a size as arguments and displays their content.
Example:
XXXXXXXXXXa
ayRow1)
XXXXXXXXXXa
ayRow2)
XXXXXXXXXXa
ayRow3)
· bool isMagicSquare(int a
ayRow1[], int a
ayRow2[], int a
ayRow3[], int size) - accepts 3 int a
ays and a size as arguments and returns true if all the requirements of a magic square are met. Otherwise, it returns false. First argument co
esponds to the first row of the magic square, second argument to the second row and the third argument to the third row of the magic square.
· bool checkRange(int a
ayRow1[], int a
ayRow2[], int a
ayRow3[], int size, int min, int max) - accepts 3 int a
ays, a size and a min and max value as arguments and returns true if the values in the a
ays are within the specified range min and max. Otherwise, it returns false. First argument co
esponds to the first row of the magic square, second argument to the second row and the third argument to the third row of the magic square.
· bool checkUnique(int a
ayRow1[], int a
ayRow2[], int a
ayRow3[], int size) - accepts 3 int a
ays and a size as arguments, and returns true if the values in the a
ays are unique (only one occu
ence of numbers between 1-9). Otherwise, it returns false. First argument co
esponds to the first row of the magic square, second argument to the second row and the third argument to the third row of the magic square.
· bool checkRowSum(int a
ayRow1[], int a
ayRow2[], int a
ayRow3[], int size) - accepts 3 int a
ays and a size as arguments and returns true if the sum of the values in each of the rows are equal. Otherwise, it returns false. First argument co
esponds to the first row of the magic square, second argument to the second row and the third argument to the third row of the magic square.
· bool checkColSum(int a
ayRow1[], int a
ayRow2[], int a
ayRow3[], int size) - accepts 3 int a
ays and a size as arguments and returns true if the sum of the values in each of the columns are equal. Otherwise, it returns false. First argument co
esponds to the first row of the magic square, second argument to the second row and the third argument to the third row of the magic square.
· bool checkDiagSum(int a
ayrow1[], int a
ayrow2[], int a
ayrow3[], int size) - accepts 3 int a
ays and a size as arguments and returns true if the sum of the values in each of the a
ay's diagonals are equal. Otherwise, it returns false. First argument co
esponds to the first row of the magic square, second argument to the second row and the third argument to the third row of the magic square.
[NOTE: You can create and use more functions as needed.]
Sample Output
NOTE: Be sure to check also
1. CMSC140 Common Project Submission Requirements (.docx)
2. CMSC140 Grading Ru
ic_CheckList-Project 5 (.xlsx)
Test Plan Template
Test your program with at least 3 more test cases. Use the given data as an example. Record your data for input and output in the following table. Make sure your tests cover all the possible scenarios.
Test Case #
Input
Actual Input
Expected Output
Is Magic?
Actual Output
Is Magic?
Did the test pass?
1
1 3 2
5 4 9
6 7 8
No
2
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
No
3
4 9 2
3 5 7
8 1 6
Yes
4
2