Hi! I have this kind of project and im really needing help for it. The compiling and print have to be a spiral matrix. it must start in the center and then move to the right and then up and then to the left, making a spiral. I have to introduce an initial and final value. the initial value must be in the center and the final value would be the end of the spiral.
Like this
initial Value = 4
final Value = 18
4 5 6
14 15 16 7
13 18 17 8
12 11 10 9
Code:
#include < stdio.h >
#include < conio.h >
void main()
{
int m[20][20],i,j;
int lc,hc,lr,hr,r,c,cnt;
clrscr();
printf("\nEnter r & c :");
scanf("%d %d",&r,&c);
cnt = 1;
lr = 0; lc = 0;
hr = r - 1;
hc = c - 1;
while ( lr <=hr && lc <= hc )
{
i = lr;
for(j=lc;j <= hc;j++)
{
m[i][j] = cnt++;
j = hc;
for(i=lr+1;i<=hr;i++)
{
m[i][j] = cnt++;
if( lr != hr )
{
i = hr;
for(j=hc-1;j>=lc;j--)
m[i][j] = cnt++;
}
if ( lc != hc )
{
j = lc;
for(i=hr-1;i>lr;i--)
m[i][j] = cnt++;
}
}
}
lr++;lc++;
hr--;hc--;
}//while
printf("\nSpirally filled matrix is\n");
for(i=0;i < r;i++)
{
for(j=0;j < c;j++)
{
printf("%4d",m[i][j]);
printf("\n");
}
}
} // main
I have this program but isnt runnig well. Please please help