Example:
Input array: 1 2 3 4 5 6 7 8 9 10 11 12 13
Matrix size: 4 4
Output:
1 2 3 4
12 13 0 5
11 0 0 6
10 9 8 7
Note: remaining elements in the matrix should be filled with zeroes
....
What i have tried is:
I just used four loops for each direction and then assigned to specific element in a 2d array. i could not find easy logic. please any one tell any logic to fill the elements in spiral direction in simple way. i tried to post this question before. someone removed my post. kindly dont remove my question so that others may help me.,
awaiting for answer.
..
dont see my solution.. its complex . its better give your own logic. thanks in advance.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m,q1,n,p,k=0,a[100],b[10][10],q,x=0,y=0;
clrscr();
printf("\n enter the size of the array:");
scanf("%d",&p);
printf("\n enter the array");
for(i=0;i<p;i++)>
{
scanf("%d",&a[i]);
}
printf("\n enter the size of the 2d array");
scanf("%d%d",&m,&n);
q=n;q1=m;
for(i=0;i<m;i++)>
{
for(j=0;j<n;j++)>
{
b[i][j]=0;
}
}
while(x<m&&y><n)>
{
for(j=y;((j<n)&&(k><p));j++)>
{
b[x][j]=a[k];
k++;
}
x++;
for(j=x;((j<m)&&(k><p));j++)>
{
b[j][n-1]=a[k];
k++;
}
n--;
if(x<m)>
{
for(j=n-1;((j>=y)&&(k<p));j--)>
{
b[m-1][j]=a[k];
k++;
}
m--;
}
if(y<n)>
{
for(j=m-1;((j>x-1)&&(k<p));j--)>
{
b[j][y]=a[k];
k++;
}
y++;
}
}for(i=0;i<q1;i++)>
{
for(j=0;j<q;j++)>
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
getch();
}