Question
C Programming: A magic square (example shown in Figure 7-19) is a square grid with 3 rows and 3 columns, which has the following properties:
C Programming:
A magic square (example shown in Figure 7-19) is a square grid with 3 rows and 3 columns, which has the following properties:
1. Each number between 1 and 9 inclusive appears once and only once in the grid
2. The sum of each row, each column and each diagonal all add up to the same number. This is shown in Figure 7-20. Fig 7-19 Fig 7-20
You can simulate a magic square using a 2-dimensional array. Write a function called isMagicSquare that takes a 2-dimensional array of int as an argument, and determines whether the array is a magic square. It then returns true if the array is a magic square, and false otherwise. Then demonstrate your function is working correctly by calling it in the main() function.
Additional requirements Make sure you meet all the requirements to avoid losing points
1. Follow the requirements in the Homework Notes.
2. Your function isMagicSquare must work correctly for any 2-dimensional array of int of size N_ROWS by N_COLS, and not just for the array shown in Fig 7-19. You are not allowed to hard code the values. You may assume the grid is square, that is, N_ROWS is equal to N_COLS
3. Must use size_t for the array indices
4. Must use prototypes for all the functions
5. Must implement the following functions (You are allowed to define additional functions to make your program more modular):
// ********************************************************
// The isValidGrid function accepts a two-dimensional int
// array as an argument, and returns true if each value
// in the range 1 through N_COL*N_ROWS appears once and only once.
// Otherwise, it returns false.
// ********************************************************
bool isValidGrid(int values[][N_COLS]);
// ********************************************************
// The allSumsAreEqual function accepts a two-dimensional int
// array as an argument, and returns true if the sum of
// each row, each column and each diagonal all add up
// to the same value // Otherwise, it returns false.
// ********************************************************
bool allSumsAreEqual(int values[][N_COLS]);
// ********************************************************
// The isMagicSquare function accepts a two-dimensional
// int array as an argument, and returns true if the
// array meets all the requirements of a magic square.
// Otherwise, it returns false.
// This function calls the isValidGrid() and
// allSumsAreEqual() functions
// ********************************************************
bool isMagicSquare(int values[][N_COLS]);
6. To demonstrate your functions are correct, implement this pseudocode to call the functions on four different arrays, array1, array2, array3 and array4 (You are allowed to define additional functions to make your program more modular) :
Initialize array1, array2, array3, array4
///////////////
Display array1
Call isMagicSquare for array1
Print the outcome
///////////////
Display array2
Call isMagicSquare for array2
Print the outcome
///////////////
Display array3
Call isMagicSquare for array3
Print the outcome
///////////////
Display array4
Call isMagicSquare for array4
Print the outcome
7. Use the preprocessor directives #define N_COLS 3 and #define N_ROWS 3 for the number of columns and number of rows respectively and initialize the arrays as follows:
// array1 is a magic square
int array1[][N_COLS] = { { 4, 9, 2 }, { 3, 5, 7 }, { 8, 1, 6 }};
// array2 is a non-magic square
int array2[][N_COLS] = { { 2, 2, 3 }, { 4, 1, 6 }, { 7, 8, 5 }};
// array3 is a magic square
int array3[][N_COLS] = { { 4, 3, 8 }, { 9, 5, 1 }, { 2, 7, 6 }};
// array4 is a non-magic square
int array4[][N_COLS] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }};
OUTPUT
4 2 3 7 Fig 7-19Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started