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

I need help fixing just part of my code and I can\'t figure out. The code I have

ID: 3603382 • Letter: I

Question

I need help fixing just part of my code and I can't figure out. The code I have fails before printing the outcome. Your help is greatly appreciated. I just nedd help fixing bool allSumsAre Equal. PLEASE KEEP IN MIND the We can't use malloc or any high level coding.

the requirment is as follows:

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

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.

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

I will post my code below and the outcome below Thank you.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define N_COLS 3
#define N_ROWS 3

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


int main()
{

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

bool signal = true;

displayArry(array1);
signal = isMagicSquare(array1);

if(signal == true)
{
printf(" This is a magic square. ");
}
else
{
printf(" This is not a magic square. ");
}

signal = false;

displayArry(array2);
signal = isMagicSquare(array1);

if(signal == true)
{
printf(" This is a magic square. ");
}
else
{
printf(" This is not a magic square. ");
}

signal = false;

displayArry(array3);
signal = isMagicSquare(array3);

if(signal == true)
{
printf(" This is a magic square. ");
}
else
{
printf(" This is not a magic square. ");
}

signal = false;

displayArry(array4);
signal = isMagicSquare(array4);

if(signal == true)
{
printf(" This is a magic square. ");
}
else
{
printf(" This is not a magic square. ");
}

return 0;
}
void displayArry(int values[][N_COLS])
{
printf("==================================== ");

for (int i = 0; i < N_ROWS; i++)
{
printf(" ");
for (int j = 0; j < N_COLS; j++)
{
printf(" %d", values[i][j]);
}
}
}

bool isValidGrid(int values[][N_COLS])
{
int *count;
count = 0;
bool signal = true;
for (int i = 1; i <= N_ROWS * N_COLS; i++)
count[i] = 0;
for (int i = 0; i < N_ROWS; i++)
{
for (int j = 0; j < N_COLS; j++)
{
count[values[i][j]] = count[values[i][j]] + 1;
}
}

for(int k=1; k<= N_ROWS*N_COLS; k++)
{
if((count[k]) != 1)
{
signal = false;
break;
}
}
free(count);return signal;
}

bool allSumsAreEqual(int values[][N_COLS])
{
int sum = 0;
bool signal = true;

for (int i = 0; i < N_ROWS; i++)
{
for (int j = 0; j < N_COLS; j++)
{
if (i == j)
sum = sum + values[i][j];
}
}

for (int i = 0; i < N_ROWS; i++)
{
int sumOfRow = 0;
for (int j = 0; j < N_COLS; j++)
{
sumOfRow = sumOfRow + values[i][j];
}

if (sum == sumOfRow)
signal = true;
else
{
signal = false;
break;
}
}

for (int i = 0; i < N_ROWS; i++)
{
int SumOfCol = 0;
for (int j = 0; j < N_COLS; j++)
{
SumOfCol = SumOfCol + values[j][i];
}
if (sum == SumOfCol)
signal = true;
else
{
signal = false;
break;
}
}
return signal;
}

bool isMagicSquare(int values[][N_COLS])
{

bool Signal = true;

bool ValidGrid = isValidGrid(values);

bool equalSums = allSumsAreEqual(values);

if(ValidGrid != true || equalSums != true)
Signal = false;

return Signal;
}

OUTCOME

9 2 3 5 7 6 8 This is a magic square. 2 2 3 8 5 This is not a magic square 8 1 6 4 2 7 This is a magic square 7 8 9 This is not a magic square. Process returned 0 (0x0) execution time: 0.017 s Press any key to cont inue.

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#define N_COLS 3

#define N_ROWS 3

void displayArry(int values[][N_COLS]);

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

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

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

void displayArry(int values[][N_COLS])

{

int i,j;

printf("==================================== ");

for ( i = 0; i < N_ROWS; i++)

{

printf(" ");

for ( j = 0; j < N_COLS; j++)

{

printf(" %d", values[i][j]);

}

}

}

bool isValidGrid(int values[][N_COLS])

{

int count[100],i,j,k;

count[0] = 0;

bool signal = true;

for ( i = 1; i <= N_ROWS * N_COLS; i++)

count[i] = 0;

for ( i = 0; i < N_ROWS; i++)

{

for ( j = 0; j < N_COLS; j++)

{

count[values[i][j]] = count[values[i][j]] + 1;

}

}

for( k=1; k<= N_ROWS*N_COLS; k++)

{

if((count[k]) != 1)

{

signal = false;

break;

}

}

//return signal;

}

bool allSumsAreEqual(int values[][N_COLS])

{

int sum = 0,i,j;

bool signal = true;

for ( i = 0; i < N_ROWS; i++)

{

for ( j = 0; j < N_COLS; j++)

{

if (i == j)

sum = sum + values[i][j];

}

}

for ( i = 0; i < N_ROWS; i++)

{

int sumOfRow = 0;

for ( j = 0; j < N_COLS; j++)

{

sumOfRow = sumOfRow + values[i][j];

}

if (sum == sumOfRow)

signal = true;

else

{

signal = false;

break;

}

}

for ( i = 0; i < N_ROWS; i++)

{

int SumOfCol = 0;

for ( j = 0; j < N_COLS; j++)

{

SumOfCol = SumOfCol + values[j][i];

}

if (sum == SumOfCol)

signal = true;

else

{

signal = false;

break;

}

}

return signal;

}

bool isMagicSquare(int values[][N_COLS])

{

bool Signal = true;

bool ValidGrid = isValidGrid(values);

bool equalSums = allSumsAreEqual(values);

if(ValidGrid != true || equalSums != true)

Signal = false;

return Signal;

}

int main()

{

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

bool signal = true;

displayArry(array1);

signal = isMagicSquare(array1);

if(signal == true)

{

printf(" This is a magic square. ");

}

else

{

printf(" This is not a magic square. ");

}

signal = false;

displayArry(array2);

signal = isMagicSquare(array1);

if(signal == true)

{

printf(" This is a magic square. ");

}

else

{

printf(" This is not a magic square. ");

}

signal = false;

displayArry(array3);

signal = isMagicSquare(array3);

if(signal == true)

{

printf(" This is a magic square. ");

}

else

{

printf(" This is not a magic square. ");

}

signal = false;

displayArry(array4);

signal = isMagicSquare(array4);

if(signal == true)

{

printf(" This is a magic square. ");

}

else

{

printf(" This is not a magic square. ");

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote