F
Felipe Ribeiro
Hi everybody.
So, I'm reading KN King's book to learn C by myself and came across a
problem in the chapter about arrays that asks me to write a program
that prints a "size x size" magic square. The user specifies the value
of "size". The algorithm is the following:
Start placing the number 1 in the middle of row 0. Place each of the
remaing numbers 2, 3, ..., n2 by moving up one row and over one
column. In case a number attempts to go outside the array the program
should "wrap around" to the opposite side. If a number attempts to be
stored in an element already occupied, the number goes directly below
the previously stored number.
Here's what I wrote:
----------------------------------------------------------------------------------------------------------
#include <stdio.h>
#define true 1
#define false 0
#define ARRAY_SIZE 99
typedef int bool;
int main(void)
{
int i, j, size, n = 1, magic_sqr[ARRAY_SIZE][ARRAY_SIZE];
bool vacant[ARRAY_SIZE][ARRAY_SIZE];
printf("This program creates a magic square of a specified size.\n");
printf("The size must be an odd number between 1 and 99.\n");
printf("Enter size of magic square: ");
scanf("%d", &size);
for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
vacant[j] = true;
for (i = 0, j = size / 2; n <= size * size; n++) {
magic_sqr[j] = n;
vacant[j] = false;
if ((i - 1 >= 0) && (j + 1 < size)) {
if (vacant[i - 1][j + 1]) {
i--;
j++;
} else {
i++;
}
} else {
if ((i - 1) == -1 && (j + 1) == size) {
if (vacant[size - 1][0]) {
i = size - 1;
j = 0;
} else {
i++;
}
} else {
if ((j + 1) == size) {
if (vacant[i - 1][0]) {
j = 0;
i--;
} else {
i++;
}
}
if ((i - 1) == -1) {
if (vacant[size - 1][j + 1]) {
i = size - 1;
j++;
} else {
i++;
}
}
}
}
}
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++)
printf("%4d", magic_sqr[j]);
printf("\n");
}
return 0;
}
----------------------------------------------------------------------------------------------------------
When the user enters 5, the program should print
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
but it doesn't. It prints
0 23 1 8 15
22 5 7 14 16
4 6 13 19 21
10 12 18 20 3
11 17 24 2 9.
I've noticed that whenever the program has to go to element [0][0] it
fails. I've been looking into the code for some good time but coudn't
understand what's been happening. The condition that treats the 17th
number is the same that treats the 10th, for example, and it works
wonders for the latter.
If somebody could give me a hand I'd be really thankful.
So, I'm reading KN King's book to learn C by myself and came across a
problem in the chapter about arrays that asks me to write a program
that prints a "size x size" magic square. The user specifies the value
of "size". The algorithm is the following:
Start placing the number 1 in the middle of row 0. Place each of the
remaing numbers 2, 3, ..., n2 by moving up one row and over one
column. In case a number attempts to go outside the array the program
should "wrap around" to the opposite side. If a number attempts to be
stored in an element already occupied, the number goes directly below
the previously stored number.
Here's what I wrote:
----------------------------------------------------------------------------------------------------------
#include <stdio.h>
#define true 1
#define false 0
#define ARRAY_SIZE 99
typedef int bool;
int main(void)
{
int i, j, size, n = 1, magic_sqr[ARRAY_SIZE][ARRAY_SIZE];
bool vacant[ARRAY_SIZE][ARRAY_SIZE];
printf("This program creates a magic square of a specified size.\n");
printf("The size must be an odd number between 1 and 99.\n");
printf("Enter size of magic square: ");
scanf("%d", &size);
for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
vacant[j] = true;
for (i = 0, j = size / 2; n <= size * size; n++) {
magic_sqr[j] = n;
vacant[j] = false;
if ((i - 1 >= 0) && (j + 1 < size)) {
if (vacant[i - 1][j + 1]) {
i--;
j++;
} else {
i++;
}
} else {
if ((i - 1) == -1 && (j + 1) == size) {
if (vacant[size - 1][0]) {
i = size - 1;
j = 0;
} else {
i++;
}
} else {
if ((j + 1) == size) {
if (vacant[i - 1][0]) {
j = 0;
i--;
} else {
i++;
}
}
if ((i - 1) == -1) {
if (vacant[size - 1][j + 1]) {
i = size - 1;
j++;
} else {
i++;
}
}
}
}
}
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++)
printf("%4d", magic_sqr[j]);
printf("\n");
}
return 0;
}
----------------------------------------------------------------------------------------------------------
When the user enters 5, the program should print
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
but it doesn't. It prints
0 23 1 8 15
22 5 7 14 16
4 6 13 19 21
10 12 18 20 3
11 17 24 2 9.
I've noticed that whenever the program has to go to element [0][0] it
fails. I've been looking into the code for some good time but coudn't
understand what's been happening. The condition that treats the 17th
number is the same that treats the 10th, for example, and it works
wonders for the latter.
If somebody could give me a hand I'd be really thankful.