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

C Programming: A magic square (example shown in Figure 7-19) is a square grid wi

ID: 3602640 • Letter: C

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:

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 }};

4 2 3 7 Fig 7-19

Explanation / Answer

#include <stdio.h>

void magicsquare(int, int);

#define N_COLS 3

#define N_ROWS 3

int isValidGrid(int array1[N_ROWS][N_COLS]);

int main()

{

unsigned nRows;

unsigned nCols;

int array1[N_ROWS][N_COLS] = { { 4, 2, 2 }, { 3, 5, 7 }, { 8, 1, 6 }};

printf(" array 1 ");

int o=isValidGrid(array1);

if(o==0)

{

printf("valid");

}

}

int isValidGrid(int array1[N_ROWS][N_COLS])

{

  

for (int i = 0; i < 3-1; i++)

for (int j = i+1; j < 3; j++)

if(array1[i]==array1[j])

{

return 1;

}

else

{

return 0;

}

}

#include <stdio.h>

void magicsquare(int, int);

int main()

{

unsigned nRows;

unsigned nCols;

printf("Enter number of Rows: ");

scanf("%i",&nRows);

printf("Enter number of Columns: ");

scanf("%i",&nCols);

magicsquare(nRows,nCols);

}

void magicsquare(int nRows, int nCols){

int k;

int p;

int sumR;

int sumC;

int sumD = 0;

int sumD2 = 0;

int isMagic = 1;

int matrix[nRows][nCols];

//Enterring Coefficients of the Matrix

for(k = 0; k < nRows; ++k){

for(p = 0; p < nCols; ++p){

printf("Enter value for Matrix[%i][%i]: ",k,p);

scanf("%i",&matrix[k][p]);

}

}

//Adding diagonal elements

for(k = 0; k < nRows ; ++k){

sumD += matrix[k][k];

sumD2 += matrix[k][nCols - k - 1];

}

printf("Sum of right Diagonal = %i ",sumD);

printf("Sum of left Diagonal = %i ",sumD2);

//Adding the rows

for(k = 0; k < nRows; ++k){

for(p = 0; p < nCols; ++p){

sumR += matrix[k][p];

}

printf("Sum of the %i row is = %i ",k,sumR);

if(sumR != sumD && sumD2)

isMagic = 0;

sumR = 0;

}

//Adding the columns

for(p = 0; p < nRows; ++p){

for(k = 0; k < nCols; ++k){

sumC += matrix[k][p];

}

printf("Sum of the %i column is = %i ",p,sumC);

if(sumC != sumD && sumD2)

isMagic = 0;

sumC = 0;

}

//Displaying matrix

for(k = 0; k < nRows; ++k){

for(p = 0; p < nCols; ++p){

printf("%i ",matrix[k][p]);

}

printf(" ");

}

if(isMagic){

printf(" Magic Square! ");

}

else{

printf(" Not a Magic Square! ");

}

}

input :

3

3

2 7 6 9 5 1 4 3 8

output:

Enter number of Rows:

Enter number of Columns:

Enter value for Matrix[0][0]: Enter value for Matrix[0][1]: Enter value for Matrix[0][2]: Enter value for Matrix[1][0]: Enter value for Matrix[1][1]: Enter value for Matrix[1][2]: Enter value for Matrix[2][0]: Enter value for Matrix[2][1]: Enter value for Matrix[2][2]: Sum of right Diagonal = 15

Sum of left Diagonal = 15

Sum of the 0 row is = 15

Sum of the 1 row is = 15

Sum of the 2 row is = 15

Sum of the 0 column is = 15

Sum of the 1 column is = 15

Sum of the 2 column is = 15

2 7 6

9 5 1

4 3 8

Magic Square!