0

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++;

        }
    }
}
underscore_d
  • 6,309
  • 3
  • 38
  • 64
  • 3
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Jun 28 '16 at 21:38
  • 4
    Please use comments, don't split the code. – Karoly Horvath Jun 28 '16 at 21:41
  • It would be helpful to show a suitable sample (non-square) matrix as input and the expected output (see how to create an MCVE ([MCVE])). Are you spiralling clockwise or anticlockwise? Are you starting at top-left, bottom-right, or one of the other corners? (Where is element `m[0][0]` — bottom-left, top-left, somewhere else?) It looks as though you are using `m[0][0]` as top left corner and you are starting at top-left and spiralling clockwise — judging from the comments. But clarifying such details help people help you get the answer you want. – Jonathan Leffler Jun 28 '16 at 22:44
  • thank you for the advice..Will keep it in mind next time.. – Saumittra Saxena Jun 29 '16 at 08:44
  • Does this answer your question? [Traverse 2D Array in Spiral pattern using recursion](https://stackoverflow.com/questions/1655685/traverse-2d-array-in-spiral-pattern-using-recursion) – RaptorRV Oct 21 '22 at 08:57

1 Answers1

0

To accomplish the task there's no need of using variable dir and checking its value as all the steps are repeated in the same order.

There was also an error in the last loop's condition: k <= t should be k >= t.

Here is a possible working implementation:

void spiral(int n,int m,int arr[][m])
{
    int top = 0,            
        right = m - 1,      
        bottom = n - 1,     
        left = 0, 
        k;  

    while( top <= bottom  &&  left <= right )
    {
        //print top row
        for ( k = left; k <= right; k++ )
        {
            printf("%d  ",arr[top][k]);
        }
        ++top;

        //print right column
        for( k = top; k <= bottom; k++ )
        {
            printf("%d   ",arr[k][right]);
        }
        --right;

        //print bottom row
        for( k = right; k >= left; k-- )
        {
            printf("%d   ",arr[bottom][k]);
        }
        --bottom;

        //print left column
        for( k = bottom; k >= top; k-- )
        // this was wrong ^^^^^^ in OP's code
        {
            printf("%d   ",arr[k][left]);
        }
        ++left;
    }
}

Live example HERE

Bob__
  • 12,361
  • 3
  • 28
  • 42