# Untitled

By: a guest on May 2nd, 2012  |  syntax: C  |  size: 1.54 KB  |  hits: 18  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. #include <stdio.h>
2.
3. const int UP = 0;
4. const int RIGHT = 1;
5. const int DOWN = 2;
6. const int LEFT = 3;
7.
8. int main()
9. {
10.   int dir; //direction
11.   int x,y; //posiotion
12.   int u,r,d,l; //limits: up, right, down, left
13.   int count; //just counts the cells that printed
14.
15.   char arr[4][3]=
16.     { {'1','2','3'} , {'4','5','6'} , {'7','8','9'} , {'a','b','c'}};
17.
18.   //at first, direction set to be right
19.   dir=RIGHT;
20.
21.   //we start at this corner
22.   x=0;
23.   y=0;
24.
25.   //at first, limits are edges of array
26.   u=1;
27.   r=3-1;
28.   d=4-1;
29.   l=0;
30.
31.   for(count=0;count<3*4;count++)
32.   {
33.     printf("%c ", arr[x][y]);
34.     if(dir == UP)
35.     {
36.       //move to direction
37.       x--;
38.       //if we are on the limit: move limit one step to center & change direction
39.       if(x==u)
40.       {
41.         u++;
42.         dir=(dir+1)%4;
43.       }
44.     }
45.     else if (dir == RIGHT)
46.     {
47.       //move to direction
48.       y++;
49.       //if we are on the limit: move limit one step to center & change direction
50.       if(y==r)
51.       {
52.         r--;
53.         dir=(dir+1)%4;
54.       }
55.     }
56.     else if(dir == DOWN)
57.     {
58.       //move to direction
59.       x++;
60.       //if we are on the limit: move limit one step to center & change direction
61.       if(x==d)
62.       {
63.         d--;
64.         dir=(dir+1)%4;
65.       }
66.     }
67.     else if(dir == LEFT)
68.     {
69.       //move to direction
70.       y--;
71.       //if we are on the limit: move limit one step to center & change direction
72.       if(y==l)
73.       {
74.         l++;
75.         dir=(dir+1)%4;
76.       }
77.     }
78.   }
79.   return 0;
80. }