-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiralArray.c
164 lines (135 loc) · 3.32 KB
/
SpiralArray.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned int UINT4;
typedef struct {
UINT4 x;
UINT4 y;
} CELL;
typedef struct {
UINT4 width;
UINT4 height;
UINT4 cellCount;
UINT4 leftFilled;
UINT4 rightFilled;
UINT4 topFilled;
UINT4 bottomFilled;
} BOARD;
void moveRight(UINT4***, BOARD*, CELL, CELL, UINT4);
void printSpiral(UINT4 **spiral, const UINT4 width, const UINT4 height)
{
UINT4 row;
UINT4 column;
for (row = 0; row < height; row++) {
for (column = 0; column < width; column++) {
printf("%3u ", spiral[row][column]);
}
printf("\n");
}
}
void moveUp(UINT4 ***spiral, BOARD *board, CELL start, CELL end, UINT4 value) {
int index;
for (index = start.y; index >= (int)end.y; index--) {
(*spiral)[index][start.x] = value++;
}
board->leftFilled++;
if (value != board->cellCount) {
CELL s;
CELL e;
s.x = end.x + 1;
s.y = end.y;
e.x = (board->width - 1) - board->rightFilled;
e.y = s.y;
moveRight(spiral, board, s, e, value);
}
}
void moveLeft(UINT4 ***spiral, BOARD *board, CELL start, CELL end, UINT4 value) {
int index;
for (index = start.x; index >= (int)end.x; index--) {
(*spiral)[start.y][index] = value++;
}
board->bottomFilled++;
if (value != board->cellCount) {
CELL s;
CELL e;
s.x = end.x;
s.y = end.y - 1;
e.x = s.x;
e.y = board->topFilled;
moveUp(spiral, board, s, e, value);
}
}
void moveDown (UINT4 ***spiral, BOARD *board, CELL start, CELL end, UINT4 value) {
int index;
for (index = start.y; index <= end.y; index++) {
(*spiral)[index][start.x] = value++;
}
board->rightFilled++;
if (value != board->cellCount) {
CELL s;
CELL e;
s.x = end.x - 1;
s.y = end.y;
e.x = board->leftFilled;
e.y = s.y;
moveLeft(spiral, board, s, e, value);
}
}
void moveRight(UINT4 ***spiral, BOARD *board, CELL start, CELL end, UINT4 value) {
int index;
for (index = start.x; index <= end.x; index++) {
(*spiral)[start.y][index] = value++;
}
board->topFilled++;
if (value != board->cellCount) {
CELL s;
CELL e;
s.x = end.x;
s.y = end.y + 1;
e.x = s.x;
e.y = (board->height - 1) - board->bottomFilled;
moveDown(spiral, board, s, e, value);
}
}
void whirlAround(UINT4 ***spiral, const UINT4 width, const UINT4 height) {
CELL start;
CELL end;
BOARD board;
start.x = 0;
start.y = 0;
end.x = width - 1;
end.y = 0;
board.width = width;
board.height = height;
board.cellCount = width * height;
board.leftFilled = 0;
board.rightFilled = 0;
board.topFilled = 0;
board.bottomFilled = 0;
moveRight(spiral, &board, start, end, 0);
}
void makeSpiral(const UINT4 width, const UINT4 height) {
UINT4 **spiral;
UINT4 index;
spiral = malloc(sizeof(*spiral) * height);
for (index = 0; index < height; index++) {
spiral[index] = malloc(sizeof(UINT4) * width);
}
whirlAround(&spiral, width, height);
printSpiral(spiral, width, height);
for (index = 0; index < height; index++) {
free(spiral[index]);
}
free(spiral);
}
int main(int argc, char** argv) {
UINT4 width;
UINT4 height;
printf("Spiral Array Width: ");
scanf("%u", &width);
printf("Spiral Array height: ");
scanf("%u", &height);
printf("%u x %u\n", width, height);
makeSpiral(width, height);
return 0;
}