W
winnerpl
Hey guys I'm trying to get a magic square but I'm stuck with it printing
out only 1's and 0's in random places. Hope you experts can can provide
some input on what I'm doing wrong.
#include <stdio.h>
#define MAX 20
int x[MAX][MAX];
int size_request(void); //asks the user to enter the size x size square.
void fill_square(int[][], int); //fills up the magic square with the
appropriate numbers.
void print_square(int[], int); //prints out the magic square.
int main(void)
{
int roww;
int size = size_request();
x[size][size];
fill_square(x, size);
for(roww = 0; roww < size; roww++)
print_square(x[roww], size);
return 0;
}
int size_request(void)
{
int size;
do
{
printf("Enter a positive, odd integer in the range from 1 to 15:
");
scanf("%d", &size);
} while((size % 2 != 1) ||(size > 15) || (size < 1));
printf("\n");
return size;
}
void fill_square(int x[][MAX], int size)
{
int count;
int row = size - 1;
int col = row / 2;
int maxnum = size * size;
for(count = 1; count < maxnum; count++)
{
x [row][col] = 1;
if(x [row-2][col+1] == 0)
{
row = row - 2;
col = col + 1;
if (col == size)
col = col - size;
else if (row < 0)
row = row + size;
}
else
{
row --;
if (row < 0)
row = row + size;
}
}
return;
}
void print_square(int x[], int size)
{
int colw;
for(colw = 0; colw < size; colw++)
printf("%4d", x[colw] * x[colw]);
printf("\n");
return;
}
Thanks in advance guys.
out only 1's and 0's in random places. Hope you experts can can provide
some input on what I'm doing wrong.
#include <stdio.h>
#define MAX 20
int x[MAX][MAX];
int size_request(void); //asks the user to enter the size x size square.
void fill_square(int[][], int); //fills up the magic square with the
appropriate numbers.
void print_square(int[], int); //prints out the magic square.
int main(void)
{
int roww;
int size = size_request();
x[size][size];
fill_square(x, size);
for(roww = 0; roww < size; roww++)
print_square(x[roww], size);
return 0;
}
int size_request(void)
{
int size;
do
{
printf("Enter a positive, odd integer in the range from 1 to 15:
");
scanf("%d", &size);
} while((size % 2 != 1) ||(size > 15) || (size < 1));
printf("\n");
return size;
}
void fill_square(int x[][MAX], int size)
{
int count;
int row = size - 1;
int col = row / 2;
int maxnum = size * size;
for(count = 1; count < maxnum; count++)
{
x [row][col] = 1;
if(x [row-2][col+1] == 0)
{
row = row - 2;
col = col + 1;
if (col == size)
col = col - size;
else if (row < 0)
row = row + size;
}
else
{
row --;
if (row < 0)
row = row + size;
}
}
return;
}
void print_square(int x[], int size)
{
int colw;
for(colw = 0; colw < size; colw++)
printf("%4d", x[colw] * x[colw]);
printf("\n");
return;
}
Thanks in advance guys.