Question: I have written this code on C++ but am not sure why when I enter the correct magic square numbers it says they are not
I have written this code on C++ but am not sure why when I enter the correct magic square numbers it says they are not magic square even though it is. Can someone please fix this code to where I can run it properly.
#include "magicsquare.h"
MagicSquare::MagicSquare()
{
for(int i = 0; i < MAX_SIZE; i++)
{
for(int j = 0; j < MAX_SIZE; j++)
{
magic[i][j] = 0; // Initialize array to zero, or empty.
}
}
magic_constant = 0; // Initialize magic_constant to zero.
size = 0; // Initialize size to zero.
}
//--------------------------------------------------------------------------
MagicSquare::MagicSquare(int square_size)
{
size = square_size; // Set size;
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
magic[i][j] = 0; // Initialize array to zero, or empty.
}
}
magic_constant = 0; // Initialize magic_constant to zero.
}
//--------------------------------------------------------------------------
bool MagicSquare::analise_square()
{
}
//---------------------------------------------------------------------------
void MagicSquare::is_magic()
{
magic_constant = size*(size*size+1)/2; // Compute magic_constant
if( analise_square() ) // If analise_square returns true, print this.
{
printf(" This is a Magic Square of sum %d. ", magic_constant);
}
else // Else print message and call generate_square
{
printf(" This is NOT a magic square. It should be: ");
generate_square();
print_square(); // Print square.
}
}
//-----------------------------------------------------------------------------
void MagicSquare::generate_square()
{
int num = 1; // Number element in the square.
int nn= (size*3) / 2; // This formula gets how many possible positions a
// magic square can be started according to its
// dimensions. Ex: if size = 3, then you have 4
// positions to start the square with number 1.
for(int i = 0; i < size; i++) // Starting row 0
for(int j = 0; j < size; j++) // Starting col 0
{
magic[(2*i-j+size)%size][(j-i+nn)%size]=num++;
// Row position is computed by:
// (2 * row_count - col_count + square_dimension) % square_dimension
// Column position is computer by:
// (row_count - column_count + possible_starts) % square_dimension
// Then,
// increment num.
}
}
//----------------------------------------------------------------------------
void MagicSquare::read_data()
{
do
{
printf("Enter size of square: ");
scanf("%d", &size); // Enter size of square.
}while( size%2 == 0 || size < 3 || size > 9);
// Odd number, 3 <= size <= 9.
printf("Enter square by row and 1 space ");
int i, j;
for(i = 0; i < size; i++)
{
printf("-> ");
for(j = 0; j < size; j++)
{
scanf("%d", &magic[i][j]); // Enter row by row.
}
}
}
//----------------------------------------------------------------------------
void MagicSquare::print_square()
{
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
printf("%3d", magic[i][j]); // Print square.
}
printf(" ");
}
}
//---------------------------------------------------------------------------
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
