Computer club Question

Go down

Computer club Question

Post  Ramprasadg on Sat Aug 16, 2008 7:19 am

last year in computer club this question was asked...i am pretty sure gaurav-ji knows the ans...
given a number n.print all the numbers from 1 till n^2 in a spiral.
for example given 3.
print the following
1 2 3
8 9 4
7 6 5

for 4
1 2 3 4
121314 5
111615 6
109 8 7

(i am not able to align for n=4 correctly,but i am sure everyone can get it??? : Question )

please give logic/code....but mainly tell me the class under which it comes if you know
avatar
Ramprasadg

Posts : 12
Join date : 2008-08-13

View user profile

Back to top Go down

Re: Computer club Question

Post  chunks on Sat Aug 16, 2008 11:14 pm

I started from the last element ie. n^2,then putting all other values from center to first position.
Check this out,if there is some problem in any case then let me know.




#include<iostream>
#include<string>
using namespace std;
#define F0(i,n) for(i=0;i<n;i++)
#define F1(i,n) for(i=2;i<=2*n-1;i++)
int main()
{
int n,ii,jj,put,i,j,k,k1;
cout<<"enter n:";
cin>>n;
put=n*n;
int arr[n][n];
if(n%2==1)
{
ii=1;i=n/2;
jj=-1;j=n/2;
}
else
{
ii=-1;i=n/2;
jj=1;j=n/2-1;
}
arr[i][j]=put;
put--;
F1(k,n)
{
if((k!=2 && k%2==0) || k==2*n-1)
k1=k/2;
else
k1=k/2 + 1;
if(k%2==0)
{
if(k==2)
j=j+jj;
if(k!=2)
i=i+ii;
while(k1--)
{ arr[i][j]=put;
put--;
i=i+ii;
}i=i-ii;
jj*=(-1);
}
else
{ j=j+jj;
while(k1--)
{ arr[i][j]=put;
j=j+jj;
put--;
}
j=j-jj;
ii*=(-1);
}
}
F0(i,n)
{ F0(j,n)
{cout<<arr[i][j]<<" ";}
cout<<"\n";
}
cin>>i;

}

cheers
avatar
chunks

Posts : 6
Join date : 2008-08-14

View user profile

Back to top Go down

Looks fine

Post  Ramprasadg on Sun Aug 17, 2008 5:34 am

yeah the solution looks fine to me.thanks
avatar
Ramprasadg

Posts : 12
Join date : 2008-08-13

View user profile

Back to top Go down

Re: Computer club Question

Post  ish_mnnit on Sun Aug 17, 2008 4:32 pm

i0 = j0 = 0; i1 = m - 1; j1 = n - 1;
for (;Wink {
if (i0 > i1) break;
for (i = i0++, j = j0; j <= j1; j++) a[i][j] = next_input();
if (j0 > j1) break;
for (i = i0, j = j1--; i <= i1; i++) a[i][j] = next_input();
if (i0 > i1) break;
for (i = i1--, j = j1; j >= j0; j--) a[i][j] = next_input();
if (j0 > j1) break;
for (i = i1, j = j0++; i >= i0; i--) a[i][j] = next_input();
}

ish_mnnit

Posts : 4
Join date : 2008-08-10

View user profile

Back to top Go down

Re: Computer club Question

Post  Ramprasadg on Mon Aug 18, 2008 12:05 pm

hey we need to enter the size of the array only and then we have to form the array from that....

it's not next_input() thing. pale


if u wanna reply ,pls reply with full running(really important) code


Last edited by Ramprasadg on Mon Aug 18, 2008 2:44 pm; edited 1 time in total
avatar
Ramprasadg

Posts : 12
Join date : 2008-08-13

View user profile

Back to top Go down

Re: Computer club Question

Post  sweetesh singh on Mon Aug 18, 2008 12:46 pm

int i,j,k,s=1,a[10][10],left,right,up,down,n;
scanf("%d",&n);
left=1;
right=n;
up=2;
down=n-1;
while(s<=n*n)
{for(j=left;j<=right;j++)
a[up-1][j]=s++;
for(i=up;i<=down;i++)
a[i][right]=s++;
for(j=right;j>=left;j--)
a[down+1][j]=s++;
for(i=down;i>=up;i--)
a[i][left=s++;
right=right-1;
left=left+1;
down=down-1;
up=up+1;
}
for(i=1;i<=n;i++)
{printf("\n");
for(j=1;j<=n;j++)
printf("%d",a[i][j]);
}

sweetesh singh

Posts : 8
Join date : 2008-08-12

View user profile

Back to top Go down

Re: Computer club Question

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum