Question:
please help?
Sayom
2007-07-24 09:44:10 UTC
I canot write the proper program to print
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
in C language
when the dimenssions of the squre is a variable.
Four answers:
aspx
2007-07-24 13:14:29 UTC
Here is the program, with good display method of numbers:



# include

# include



int numlen(int m){

int len=0;

while (m>0){

len++;

m=m/10;

}

return len;



}//end numlen





main(){





int i=0;

int n;



printf("Enter the side length: ");

scanf("%i",&n);



int l,h;

int num=1;

int a[50][50];



l=0;

h=n-1;







while (i
for (int j=l;j<=h;j++)

a[l][j]=num++;

for (int j=l+1;j<=h;j++)

a[j][h]=num++;

for (int j=h-1;j>=l;j--)

a[h][j]=num++;

for (int j=h-1;j>=l+1;j--)

a[j][l]=num++;



i++;

l++;

h--;



}//end while





//for display purposes

int len=numlen(n*n);









for (int x=0;x
for (int y=0;y
int t=a[x][y];



int sp=len-numlen(t)+1;

while(sp>=0){

printf(" ");

sp--;

}

printf("%i",t);







}



printf("\n");

}















getch();

}//end main
R D
2007-07-25 16:51:46 UTC
N=5;M=5;r=1;c=1;

cl=1; cr=N;

rt=1; rb=M;

Num=0;Mode=0 ;

for n=1:N

for m=1:M

A(r,c)=Num+1;

Num=A(r,c);

if (r == rt && c==cr && Mode==0) Mode=1;rt=rt+1; end;

if (r == rb && c==cr && Mode==1) Mode=2;cr=cr-1; end;

if (r == rb && c==cl && Mode==2) Mode=3;rb=rb-1; end;

if (r == rt && c==cl && Mode==3) Mode=0;cl=cl+1; end;

if (Mode==0) c=c+1; end;

if (Mode==1) r=r+1; end;

if (Mode==2) c=c-1; end;

if (Mode==3) r=r-1; end;

end

end



Note: A is the array. This is MATLAB(easily available for me) code, u can change it to C easily.
jimmybond
2007-07-27 18:59:18 UTC
why not join the Solutions Slash for the answer?
§ïm¶Lə µë
2007-07-25 10:07:52 UTC
ask it to ur computer teacher


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...