patterncMinor
Spiral Scanning for matrix using recursion
Viewed 0 times
scanningrecursionusingforspiralmatrix
Problem
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:
Example:
For matrix:
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?
It uses 5 parameters - including 2 extra variables:
callto keep track of the no. of current call
counterto keep track of no. of elements already printed
Example:
For matrix:
1 2 3
4 5 6
7 8 9For 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
#include
void spiralscan(int a[][20],int n,int m,int call,int counter)
{
int i,j;
if(counter>=n*m)
return;
//scan top
//if(m>call)
{ printf("\ntop\n");
for(j=call;j=n*m)
return;
//scan right
// if(n-call>0)
{ printf("\nright\n");
for(i=call+1;i=n*m)
return;
//scan bottom
// if(m-call>0)
{ printf("\nbottom\n");
for(j=m-2-call;j>=call;j--)
{printf(" %d ",a[n-1-call][j]);
counter++;
}
}
if(counter>=n*m)
return;
//scan left
// if(callcall;i--)
{printf(" %d ",a[i][call]);
counter++;
}
}
if(counter>=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",&n);
printf("\nno. of columns : ");
scanf("%d",&m);
printf("\nEnter matrix elements : \n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
}
printf("\n\n\t\tScanning Spirally\n\n");
spiralscan(a,n,m,0,0);
return 0;
}Solution
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:
You should use returns around your
Right here, you should remove the
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:
What are
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.
if(counter>=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 >= n * m) {
return;
}Right here, you should remove the
if, braces, and indentation://scan bottom
// if(m-call>0)
{ printf("\nbottom\n");
for(j=m-2-call;j>=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.
Code Snippets
if(counter>=n*m)
return;if(counter >= n * m) {
return;
}//scan bottom
// if(m-call>0)
{ printf("\nbottom\n");
for(j=m-2-call;j>=call;j--)
{printf(" %d ",a[n-1-call][j]);
counter++;
}
}void spiralscan(int a[][20],int n,int m,int call,int counter)Context
StackExchange Code Review Q#77478, answer score: 3
Revisions (0)
No revisions yet.