-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelixArray.c
64 lines (60 loc) · 1.48 KB
/
helixArray.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
#include <stdio.h>
void printHelixArray(int dimension);
int main(){
int d;
scanf("%d", &d);
printHelixArray(d);
return 0;
}
void printHelixArray(int dimension){
int numbers[100][100];
for(int count = 0; count < dimension; count++){
for(int in = 0; in < dimension; in++){
numbers[count][in] = 0;
}
}
int ima = 0;
int currentx = -1;
int currenty = 0;
int currentlen = 0;
int orientation = 0;
int fold = 1;
int layer = fold / 2;
for(int count = 0; count < (dimension * dimension); count++){
if(currentlen == (dimension - layer)){
if(orientation != 3){
orientation++;
} else {
orientation = 0;
}
fold++;
layer = fold / 2;
currentlen = 0;
}
switch(orientation){
case 0:
currentx++;
break;
case 1:
currenty++;
break;
case 2:
currentx--;
break;
case 3:
currenty--;
break;
}
numbers[currenty][currentx] = ++ima;
currentlen++;
}
for(int y = 0; y < dimension; y++){
for(int x = 0; x < dimension; x++){
if(x != (dimension - 1)){
printf("%d ", numbers[y][x]);
} else {
printf("%d\n", numbers[y][x]);
}
}
}
}