# include <stdio.h>
# include <stdlib.h>
int main(void)
{
int s;
int row;
int column;
int k;
int array[99][99] ;
printf("Enter the dimension of the square : ") ;
scanf("%d", &s) ;
if (s % 2 == 0)
{
printf("Please enter an even number") ;
goto last;
}
column = (s + 1) / 2 ;
row = 1 ;
int sqr1 = s*s;
for(k = 1 ; k <= sqr1 ; k++)
{
array[row][column] = k ;
if(k % s == 0)
{
row = (row + 1);
goto loop ;
}
if(row == 1)
row = s ;
else
row = row - 1 ;
if(column == s)
column = 1;
else
column = column + 1 ;
loop : ;
}
for (row = 1 ; row <= s ; row++)
{
for (column = 1 ; column <= s ; column++)
{
printf("%d\t", array[row][column]) ;
}
printf("\n\n") ;
}
last : ;
return 0;
}
I was wondering if anyone could tell me where the code puts the number down. Say I wanted a 3x3 magic square. The output would be:
https://i.stack.imgur.com/BYTSn.png
I was wondering where in the code it would move the 4 down because the 1 is already there. Same thing with the 7 moving down. The principle is you go 1 up and 1 to the right everytime and if there is something there you move down and keep going.