
Untitled
By: a guest on
May 2nd, 2012 | syntax:
C | size: 1.54 KB | hits: 18 | expires: Never
#include <stdio.h>
const int UP = 0;
const int RIGHT = 1;
const int DOWN = 2;
const int LEFT = 3;
int main()
{
int dir; //direction
int x,y; //posiotion
int u,r,d,l; //limits: up, right, down, left
int count; //just counts the cells that printed
char arr[4][3]=
{ {'1','2','3'} , {'4','5','6'} , {'7','8','9'} , {'a','b','c'}};
//at first, direction set to be right
dir=RIGHT;
//we start at this corner
x=0;
y=0;
//at first, limits are edges of array
u=1;
r=3-1;
d=4-1;
l=0;
for(count=0;count<3*4;count++)
{
printf("%c ", arr[x][y]);
if(dir == UP)
{
//move to direction
x--;
//if we are on the limit: move limit one step to center & change direction
if(x==u)
{
u++;
dir=(dir+1)%4;
}
}
else if (dir == RIGHT)
{
//move to direction
y++;
//if we are on the limit: move limit one step to center & change direction
if(y==r)
{
r--;
dir=(dir+1)%4;
}
}
else if(dir == DOWN)
{
//move to direction
x++;
//if we are on the limit: move limit one step to center & change direction
if(x==d)
{
d--;
dir=(dir+1)%4;
}
}
else if(dir == LEFT)
{
//move to direction
y--;
//if we are on the limit: move limit one step to center & change direction
if(y==l)
{
l++;
dir=(dir+1)%4;
}
}
}
return 0;
}