I need the program to display This is a magic square or This

I need the program to display \"This is a magic square\" or \"This is not a magic square\" using boolean. I am having trouble so could someone please help me. I have the instructions as well as my code.

// ********************************************************

// 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]);


Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define N_COLS 3
#define N_ROWS 3
void printArray(short int[][N_COLS], int);
void printArray2(short int[][N_COLS], int);
void printArray3(short int[][N_COLS], int);
void printArray4(short int[][N_COLS], int);


int main()
{

short int array1[][N_COLS] = { { 4, 9, 2 }, { 3, 5, 7 }, { 8, 1, 6 }};
short int array2[][N_COLS] = { { 2, 2, 3 }, { 4, 1, 6 }, { 7, 8, 5 }};
short int array3[][N_COLS] = { { 4, 3, 8 }, { 9, 5, 1 }, { 2, 7, 6 }};
short int array4[][N_COLS] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }};

printf(\"==========================\ \");
printArray(array1, 3);
printf(\"==========================\ \");
printArray2(array2, 3);
printf(\"==========================\ \");
printArray3(array3, 3);
printf(\"==========================\ \");
printArray4(array4, 3);

puts(\"\");

bool isValidGrid(int values[][N_COLS]);
bool allSumsAreEqual(int values[][N_COLS]);
bool isMagicSquare(int values[][N_COLS]);

return 0;
}

void printArray(short int arr[][N_COLS], int size)

{
for (size_t row = 0; row < (size_t) size ; row++)
{
for (size_t col = 0; col < 3 ; col++)
printf(\"%5d\", arr[row][col]);
puts(\"\");
}

}
void printArray2(short int arr2[][N_COLS], int size)

{
for (size_t row = 0; row < (size_t) size ; row++)
{
for (size_t col = 0; col < 3 ; col++)
printf(\"%5d\", arr2[row][col]);
puts(\"\");
}

}
void printArray3(short int arr3[][N_COLS], int size)

{
for (size_t row = 0; row < (size_t) size ; row++)
{
for (size_t col = 0; col < 3 ; col++)
printf(\"%5d\", arr3[row][col]);
puts(\"\");
}

}
void printArray4(short int arr4[][N_COLS], int size)

{
for (size_t row = 0; row < (size_t) size ; row++)
{
for (size_t col = 0; col < 3 ; col++)
printf(\"%5d\", arr4[row][col]);
puts(\"\");
}

}

Solution

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define N_COLS 3
#define N_ROWS 3
void printArray(short int[][N_COLS], int);
void printArray2(short int[][N_COLS], int);
void printArray3(short int[][N_COLS], int);
void printArray4(short int[][N_COLS], int);


void printArray(short int values[][N_COLS], int size)
{
size_t row=0;
size_t col = 0;
for (row = 0; row < (size_t) size ; row++)
{
for (col = 0; col < 3 ; col++)
printf(\"%5d\", values[row][col]);
puts(\"\");
}

}
bool isValidGridFunction(short int values[][N_COLS])
{

int array[9];
   int i,j,c=0;
   for(i=0;i<N_ROWS;i++)
   {
       for(j=0;j<N_COLS;j++)
       {
           array[c]=values[i][j];
           //printf(\"%d\ \",values[i][j]);
           c++;
       }
   }
   for (i = 0; i < c-1; i++) { // read comment by @nbro
for (j = i + 1; j < c; j++) {
if (array[i] == array[j]) {
return false;
}
}
}
return true;

}

bool isSumEqualFunction(short int values[][N_COLS])
{
int sum, sum1, sum2;
int flag = 0;
int row,col,size=3;
sum = 0;
for (row = 0; row < size; row++) {
for (col = 0; col < size; col++) {
if (row == col)
sum = sum + values[row][col];
}
}

//For Rows
for (row = 0; row < size; row++) {
sum1 = 0;
for (col = 0; col < size; col++) {
sum1 = sum1 + values[row][col];
}
if (sum == sum1)
flag = 1;
else {
flag = 0;
break;
}
}

//For cols
for (row = 0; row < size; row++) {
sum2 = 0;
for (col = 0; col < size; col++) {
sum2 = sum2 + values[col][row];
}
if (sum == sum2)
flag = 1;
else {
flag = 0;
break;
}
}

if (flag == 1)
return true;
else
return false;

}

void isMagicFunction(bool isGrid,bool isEqualSum)
{
if(isGrid==true && isEqualSum==true) printf(\"Is magic square\ \");
else printf(\"Not magic square\ \");;
}
int main()
{
short int array1[][N_COLS] = { { 4, 9, 2 }, { 3, 5, 7 }, { 8, 1, 6 }};
short int array2[][N_COLS] = { { 2, 2, 3 }, { 4, 1, 6 }, { 7, 8, 5 }};
short int array3[][N_COLS] = { { 4, 3, 8 }, { 9, 5, 1 }, { 2, 7, 6 }};
short int array4[][N_COLS] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }};
bool isGrid,isEqualSum;
printf(\"==========================\ \");
printArray(array1, 3);
isGrid=isValidGridFunction(array1);
    isEqualSum=isSumEqualFunction(array1);
    isMagicFunction(isGrid,isEqualSum);
printf(\"==========================\ \");
printArray(array2, 3);
isGrid=isValidGridFunction(array2);
    isEqualSum=isSumEqualFunction(array2);
    isMagicFunction(isGrid,isEqualSum);
printf(\"==========================\ \");
printArray(array3, 3);
isGrid=isValidGridFunction(array3);
    isEqualSum=isSumEqualFunction(array3);
    isMagicFunction(isGrid,isEqualSum);
printf(\"==========================\ \");
printArray(array4, 3);
isGrid=isValidGridFunction(array4);
    isEqualSum=isSumEqualFunction(array4);
    isMagicFunction(isGrid,isEqualSum);
puts(\"\");
   
    //printf(\"%d\ \",isGrid);
    //printf(\"%d\ \",isEqualSum);
    //printf(\"%d\ \",isMagic);
return 0;
}

I need the program to display \
I need the program to display \
I need the program to display \
I need the program to display \
I need the program to display \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site