Spiral Scanning for matrix using recursion I have written code for scanning an N x M matrix in spiral order using a recursive function.

It uses 5 parameters - including 2 extra variables:


call to keep track of the no. of current call
counter to keep track of no. of elements already printed


Example:

For matrix:

1 2 3
4 5 6
7 8 9


For call = 0:
Output: 1 2 3 6 9 8 7 4 counter = 8

For call = 1:
Output: 5 counter = 9

Can the code be improved so that it uses fewer no. of variables and is more efficient?

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
void spiralscan(int a[][20],int n,int m,int call,int counter)
{
    int i,j;
    if(counter&gt;=n*m)
        return;
    //scan top
    //if(m&gt;call)
    {   printf("\ntop\n");
        for(j=call;j&lt;m-call;j++)
        {printf(" %d ",a[call][j]);
        counter++;
        }
    }
        if(counter&gt;=n*m)
        return;
    //scan right
   // if(n-call&gt;0)
    {   printf("\nright\n");
        for(i=call+1;i&lt;n-call;i++)
        {printf(" %d ",a[i][m-call-1]);
        counter++;
        }
    }
        if(counter&gt;=n*m)
        return;

    //scan bottom
 //   if(m-call&gt;0)
    {   printf("\nbottom\n");
        for(j=m-2-call;j&gt;=call;j--)
        {printf(" %d ",a[n-1-call][j]);
        counter++;
        }
    }
        if(counter&gt;=n*m)
        return;
    //scan left
   // if(call&lt;n/2)
       {   printf("\nleft\n");
        for(i=n-2-call;i&gt;call;i--)
        {printf(" %d ",a[i][call]);
         counter++;
        }
    }
        if(counter&gt;=n*m)
        return;

    spiralscan(a,n,m,call+1,counter);
}
int main()
{
    int n,m,i,j,a[20][20];
    printf("\t\t\tSpiral Scanning\n\n");
    printf("no. of rows : ");
    scanf("%d",&amp;n);
    printf("\nno. of columns : ");
    scanf("%d",&amp;m);
    printf("\nEnter matrix elements : \n");
    for(i=0;i&lt;n;i++)
    {
        for(j=0;j&lt;m;j++)
             scanf("%d",&amp;a[i][j]);
    }
    printf("\n\n\t\tScanning Spirally\n\n");
    spiralscan(a,n,m,0,0);
    return 0;
}
It is very important you indent your code properly as it will make your code easier to read and less prone to errors.  This statement in particular is problematic:


if(counter&gt;=n*m)
return;



You should use returns around your if blocks and loops even when they are single-statement bodies, you should indent your code so it is clear the return is part of the if block, and you should use spaces around your operators to make it easier to read:

if(counter &gt;= n * m) {
    return;
}


Right here, you should remove the if, braces, and indentation:

    //scan bottom
 //   if(m-call&gt;0)
    {   printf("\nbottom\n");
        for(j=m-2-call;j&gt;=call;j--)
        {printf(" %d ",a[n-1-call][j]);
        counter++;
        }
    }


It doesn't make sense to have messy, hard-to-read code like this lying around.  Also, the final code shouldn't show fixed bugs and commented (dead) code - it should just be the working code and comments stating what it does as needed (I am not an advocate of a comment per line).

You should name your variables better:

void spiralscan(int a[][20],int n,int m,int call,int counter)


What are n and m anyway?  Also, you should put spaces after your commas.

It will probably be easier to read your code if you use PascalCase, snake_case, or camelCase to name your variables and functions becausevariableandfunctionnameswithnowordseparatorsarehardtoreadwhenverylong.  It seems snake_case is common among C programmers.