Help with magic square program
Hi All,
I am writing a magic square code. I am doing a mistake somewhere because the only value that I can get in the square is the first number. Here is my code and my output. Any help and pointers would be greatly appreciated.Thanks
#include<stdio.h>
#define SIZE 7
#define RANGE SIZE*SIZE
void main(void)
{
int square[SIZE][SIZE]={0};
int count=1;
int col=SIZE/2;
int row=0;
square[row][col]=count;
//build the magic square
do{
count++;
row--;
col++;
if(row == -1 && col == SIZE)
{
row+=2;
col--;
}
else if (row == -1)
row=SIZE-1;
else if(col == SIZE)
col = 0;
else if (square[row][col] != 0)
{
row+=2;
col--;
}
}while (count < RANGE);
//print magic square
for(row=0;row<SIZE;row++){
for(col=0;col<SIZE;col++)
printf("%6d",square[row][col]);
printf("\n");
}
}
output
0 0 0 1 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0