I need help on writing this code using Language C A Magic Sq
I need help on writing this code using Language \"C\"- A Magic Square.
I need to simulate this using 2-D Array -> Write a function called \'isMagicSqure\' that takes 2-D array of int and determine wheter the array is magic square or not. It will return true if its magix square, and false otherwise.
I must use \'size_t\' for the array indices, must use prototypes for all functions, and must implement the following functions:
bool isValidGrid (int values[ ][N_COLS];
bool allSumsAreEqual (int values[ ][N_COLS];
bool isMagicSquare (int values[ ][N_COLS];
Use the preprocessor directives #define N_COLS 3, and #define N_ROWS 3, for the number of columns and number of rows respectively and intialize the arrays as follows:
int array1[ ][N_COLS] = { { 4, 9, 2 }, { 3, 5, 7 }, { 8, 1, 6 } }; // This is a magic square.
int array1[ ][N_COLS] = { { 2, 2, 3 }, { 4, 1, 6 }, { 7, 8 ,5 } }; // This is a non-magic square.
int array1[ ][N_COLS] = { { 4, 3, 8 }, { 9, 5, 1 }, { 2, 7, 6 } }; // This is a magic square.
int array1[ ][N_COLS] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // This is a non-magic square.
This is in Language \"C\".
15 4 2 15 3 715 8 6 H15 15 15 15 15Solution
code snipplet :
******************************************************************************************************************************
#include<stdio.h>
#include<stdbool.h>
#define N_COLS 3
#define N_ROWS 3
bool isValidGrid(int values[][N_COLS])
{
if(N_ROWS==N_COLS)
return true;
else
return false;
}
bool allSumsAreEqual(int values[][N_COLS])
{
if(isValidGrid(values))
{
int size_t = 3;
int i, j = 0;
int sum, sum1, sum2;
int flag = 0;
//For diagonal elements
sum = 0;
for (i=0; i < size_t; i++)
{
for (j = 0; j < size_t; j++) {
if (i == j)
sum = sum + values[i][j];
}
}
//For Rows
for (i=0; i< size_t; i++)
{
sum1 = 0;
for (j=0; j<size_t; j++) {
sum1 = sum1 + values[i][j];
}
if (sum == sum1)
flag = 1;
else
{
flag = 0;
break;
}
}
if(flag!=0)
{
//For Columns
for (i=0;i<size_t;i++) {
sum2 = 0;
for (j = 0; j < size_t; j++) {
sum2 = sum2 + values[i][j];
}
if (sum == sum2)
flag = 1;
else {
flag = 0;
break;
}
}
}
if (flag == 1)
return true;
else
return false;
}
return false;
}
bool isMagicSquare (int values[][N_COLS])
{
if(allSumsAreEqual(values))
return true;
else
return false;
}
int main()
{
int array1[ ][N_COLS] = { { 4, 9, 2 }, { 3, 5, 7 }, { 8, 1, 6 } }; // This is a magic square.
int array2[ ][N_COLS] = { { 2, 2, 3 }, { 4, 1, 6 }, { 7, 8 ,5 } }; // This is a non-magic square.
int array3[ ][N_COLS] = { { 4, 3, 8 }, { 9, 5, 1 }, { 2, 7, 6 } }; // This is a magic square.
int array4[ ][N_COLS] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // This is a non-magic square.
bool t = isMagicSquare(array1);
if(t)
printf(\"This is a magic square\");
else
printf(\"This is not a magic square\");
return 0;
}
***************************************************************************************************************************
Important points :
1. As per your requirement we are not taking any input from user.
2. only array1 is executed in the program, you can also execute array2, array3 and array4 by changing argument name in isMagicSquare() function call.


