Skip to content

Instantly share code, notes, and snippets.

@corehello
Created February 7, 2017 05:44
Show Gist options
  • Save corehello/96a9d7485aa0dcc7eb19d8c0683c0bf7 to your computer and use it in GitHub Desktop.
Save corehello/96a9d7485aa0dcc7eb19d8c0683c0bf7 to your computer and use it in GitHub Desktop.
snake array
#include <stdio.h>
#include <stdlib.h>
int main (int ac, char* av[])
{
int number = atoi(av[1]);
//number = 5;
int i=0,j=0;
int direction=0;
/*
* define array to make border
*/
int lt[2] = {0,1},
rt[2] = {number, 0},
lb[2] = {0, number-1},
rb[2] = {number-1, number-1};
int k;
int array[number][number];
for(k=0; k<number*number; k++)
{
printf("i = %d, j = %d, k = %d\n", i, j, k);
array[i][j] = k + 1;
switch (direction%4) {
case 0:
if (i == rt[0]-1)
{
j++;
rt[0]--;
rt[1]++;
direction++;
}
else
{
i++;
}
break;
case 1:
if (j == rb[1])
{
i--;
rb[0]--;
rb[1]--;
direction++;
}
else
{
j++;
}
break;
case 2:
if (i == lb[0])
{
j--;
lb[0]++;
lb[1]--;
direction++;
}
else
{
i--;
}
break;
case 3:
if (j == lt[1] && k != 0 )
{
i++;
lt[0]--;
lt[1]++;
direction++;
}
else
{
j--;
}
break;
}
}
for(int m = 0; m< number; m++)
{
for(int n = 0; n <number; n++)
{
printf("%d\t",array[m][n]);
}
printf("\n");
}
}
@corehello
Copy link
Author

corehello commented Feb 7, 2017

# gcc snake_array.c -o snake_array
# ./snake_array 5
i = 0, j = 0, k = 0
i = 1, j = 0, k = 1
i = 2, j = 0, k = 2
i = 3, j = 0, k = 3
i = 4, j = 0, k = 4
i = 4, j = 1, k = 5
i = 4, j = 2, k = 6
i = 4, j = 3, k = 7
i = 4, j = 4, k = 8
i = 3, j = 4, k = 9
i = 2, j = 4, k = 10
i = 1, j = 4, k = 11
i = 0, j = 4, k = 12
i = 0, j = 3, k = 13
i = 0, j = 2, k = 14
i = 0, j = 1, k = 15
i = 1, j = 1, k = 16
i = 2, j = 1, k = 17
i = 3, j = 1, k = 18
i = 3, j = 2, k = 19
i = 3, j = 3, k = 20
i = 2, j = 3, k = 21
i = 1, j = 3, k = 22
i = 1, j = 2, k = 23
i = 2, j = 2, k = 24
1	16	15	14	13
2	17	24	23	12
3	18	25	22	11
4	19	20	21	10
5	6	7	8	9
# ./snake_array 10
i = 0, j = 0, k = 0
i = 1, j = 0, k = 1
i = 2, j = 0, k = 2
i = 3, j = 0, k = 3
i = 4, j = 0, k = 4
i = 5, j = 0, k = 5
i = 6, j = 0, k = 6
i = 7, j = 0, k = 7
i = 8, j = 0, k = 8
i = 9, j = 0, k = 9
i = 9, j = 1, k = 10
i = 9, j = 2, k = 11
i = 9, j = 3, k = 12
i = 9, j = 4, k = 13
i = 9, j = 5, k = 14
i = 9, j = 6, k = 15
i = 9, j = 7, k = 16
i = 9, j = 8, k = 17
i = 9, j = 9, k = 18
i = 8, j = 9, k = 19
i = 7, j = 9, k = 20
i = 6, j = 9, k = 21
i = 5, j = 9, k = 22
i = 4, j = 9, k = 23
i = 3, j = 9, k = 24
i = 2, j = 9, k = 25
i = 1, j = 9, k = 26
i = 0, j = 9, k = 27
i = 0, j = 8, k = 28
i = 0, j = 7, k = 29
i = 0, j = 6, k = 30
i = 0, j = 5, k = 31
i = 0, j = 4, k = 32
i = 0, j = 3, k = 33
i = 0, j = 2, k = 34
i = 0, j = 1, k = 35
i = 1, j = 1, k = 36
i = 2, j = 1, k = 37
i = 3, j = 1, k = 38
i = 4, j = 1, k = 39
i = 5, j = 1, k = 40
i = 6, j = 1, k = 41
i = 7, j = 1, k = 42
i = 8, j = 1, k = 43
i = 8, j = 2, k = 44
i = 8, j = 3, k = 45
i = 8, j = 4, k = 46
i = 8, j = 5, k = 47
i = 8, j = 6, k = 48
i = 8, j = 7, k = 49
i = 8, j = 8, k = 50
i = 7, j = 8, k = 51
i = 6, j = 8, k = 52
i = 5, j = 8, k = 53
i = 4, j = 8, k = 54
i = 3, j = 8, k = 55
i = 2, j = 8, k = 56
i = 1, j = 8, k = 57
i = 1, j = 7, k = 58
i = 1, j = 6, k = 59
i = 1, j = 5, k = 60
i = 1, j = 4, k = 61
i = 1, j = 3, k = 62
i = 1, j = 2, k = 63
i = 2, j = 2, k = 64
i = 3, j = 2, k = 65
i = 4, j = 2, k = 66
i = 5, j = 2, k = 67
i = 6, j = 2, k = 68
i = 7, j = 2, k = 69
i = 7, j = 3, k = 70
i = 7, j = 4, k = 71
i = 7, j = 5, k = 72
i = 7, j = 6, k = 73
i = 7, j = 7, k = 74
i = 6, j = 7, k = 75
i = 5, j = 7, k = 76
i = 4, j = 7, k = 77
i = 3, j = 7, k = 78
i = 2, j = 7, k = 79
i = 2, j = 6, k = 80
i = 2, j = 5, k = 81
i = 2, j = 4, k = 82
i = 2, j = 3, k = 83
i = 3, j = 3, k = 84
i = 4, j = 3, k = 85
i = 5, j = 3, k = 86
i = 6, j = 3, k = 87
i = 6, j = 4, k = 88
i = 6, j = 5, k = 89
i = 6, j = 6, k = 90
i = 5, j = 6, k = 91
i = 4, j = 6, k = 92
i = 3, j = 6, k = 93
i = 3, j = 5, k = 94
i = 3, j = 4, k = 95
i = 4, j = 4, k = 96
i = 5, j = 4, k = 97
i = 5, j = 5, k = 98
i = 4, j = 5, k = 99
1	36	35	34	33	32	31	30	29	28
2	37	64	63	62	61	60	59	58	27
3	38	65	84	83	82	81	80	57	26
4	39	66	85	96	95	94	79	56	25
5	40	67	86	97	100	93	78	55	24
6	41	68	87	98	99	92	77	54	23
7	42	69	88	89	90	91	76	53	22
8	43	70	71	72	73	74	75	52	21
9	44	45	46	47	48	49	50	51	20
10	11	12	13	14	15	16	17	18	19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment