i am trying to print elements of an array in a spiral manner..cant figure out the logical error in my code..
void spiral(int n,int m,int arr[][m])
{
int t=0,r=m-1,b=n-1,l=0,dir=0; /* t->top b->bottom l->left r->right */
int k,j;
// above parameters to manage the matrix.
// exit condition from loop
while (t<=b && l<=r)
{
// print top row
if (dir==0)
{
for (k=l;k<=r;k++)
{
printf("%d ",arr[t][k]);
}
dir=1;
t++;
}
// print right column
else if (dir==1)
{
for (k=t;k<=b;k++)
{
printf("%d ",arr[k][r]);
}
dir=2;
r--;
}
// print bottom row
else if (dir==2)
{
for (k=r;k>=l;k--)
{
printf("%d ",arr[b][k]);
}
dir=3;
b--;
}
// print left column
else if (dir==3)
{
for (k=b;k<=t;k--)
{
printf("%d ",arr[k][l]);
}
dir=0;
l++;
}
}
}