Created
February 7, 2017 05:44
-
-
Save corehello/96a9d7485aa0dcc7eb19d8c0683c0bf7 to your computer and use it in GitHub Desktop.
snake array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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"); | |
} | |
} |
Author
corehello
commented
Feb 7, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment