1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| #include <limits.h>
#include <stdio.h>
#include <stdlib.h>
/******************************** CONSTANTS ********************************/
#define G_NUMBER 76
/* (46340 * 46340 < INT_MAX). And this way, INT_MAX is a sentinel. */
#define G_SQUARE_MAX 46340
/********************************** MACRO **********************************/
#define SPIRAL_MOVE \
if (nb_cases < tmp_length) { \
++nb_cases; \
} else { \
++nb_changes; \
nb_cases = 0; \
\
switch (nb_changes) { \
case 1: --tmp_length; break; \
case 2: direction = ((direction > 0)? -1: 1); nb_changes = 0; break; \
default: break; \
} \
} \
\
switch (nb_changes) { \
case 0: col = (col + direction); break; \
case 1: row = (row + direction); break; \
default: break; \
}
/********************************* PROGRAM *********************************/
/* XXX Warning : nb_digits should be <= 9 */
void print_array(size_t* array, size_t length) {
if ((array != NULL) && (length > 0)) {
char format[6];
size_t row, col, number, nb_digits;
/* Find the max of number of digits */
number = *(array); /* top-left corner value is the max */
nb_digits = 0;
while (number > 9) {
number /= 10;
++nb_digits;
}
++nb_digits;
/* printf("print_array - debug : nb digits %lu\n", nb_digits); */
/* Create the printf format with the max of number of digits */
format[0] = '%';
format[1] = (nb_digits + '0');
format[2] = 'l';
format[3] = 'u';
format[4] = ' ';
format[5] = '\0';
/* Print the array */
for(row=0; row < length; ++row) {
for(col=0; col < length; ++col) {
number = *(array + (row * length) + col);
if (number != INT_MAX) {
printf(format, number);
} else {
printf(format, 0);
}
}
printf("\n");
}
}
}
void print_array01(size_t* array, size_t length) {
if ((array != NULL) && (length > 0)) {
size_t row, col, number;
int nb_digits;
/* Find the max of number of digits */
number = *(array); /* top-left corner value is the max */
nb_digits = 0;
while (number > 9) {
number /= 10;
++nb_digits;
}
++nb_digits;
/* printf("print_array - debug : nb digits %lu\n", nb_digits); */
/* Print the array */
for(row=0; row < length; ++row) {
for(col=0; col < length; ++col) {
number = *(array + (row * length) + col);
if (number != INT_MAX) {
printf("%*lu ", nb_digits, number);
} else {
printf("%*u ", nb_digits, 0);
}
}
printf("\n");
}
}
}
int main(int argc, char** argv)
{
if ((G_NUMBER > 4) && (G_NUMBER <= G_SQUARE_MAX)) {
size_t* array;
size_t length;
/* Find length */
length = 1;
while ((length * length) < G_NUMBER) {
++length;
}
/* Create the array */
array = malloc(length * length * sizeof(size_t));
if (array != NULL) {
size_t tmp, tmp_length, col, row, nb_cases, nb_changes;
int direction;
for(tmp=G_NUMBER, tmp_length=(length - 1), row=0, col=0, nb_cases=0, direction=1, nb_changes=0; tmp > 0; --tmp) {
*(array + (row * length) + col) = tmp;
/* printf("main - debug : case %lu %lu - val : %lu\n", row, col, tmp); */
SPIRAL_MOVE
}
for(tmp=((length * length) - G_NUMBER); tmp > 0; --tmp) {
*(array + (row * length) + col) = INT_MAX; /* sentinel */
/* printf("main - debug : case %lu %lu - sentinel\n", row, col); */
SPIRAL_MOVE
}
/* print_array(array, length); */
print_array01(array, length);
/* Free the array */
free(array);
/* array = NULL; */
} else {
printf("main - error : malloc\n");
}
} else {
printf("main - debug : nothing to do\n");
}
return EXIT_SUCCESS;
} |
Partager