Click here to Skip to main content
15,921,646 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

C++
#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;
}
}
/*for(i=0;i<m;i++)>
{
for(j=0;j<n;j++)>
{
printf("%d\t",b[i][j]);
}
printf("\n");
}*/
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];
//printf("%d",b[j][n-1]);
k++;
}
n--;
if(x<m)>
{
for(j=n-1;((j>=y)&&(k<p));j--)>
{
b[m-1][j]=a[k];
//printf("\n %d",b[n][j]);
k++;
}
m--;
}
if(y<n)>
{
for(j=m-1;((j>x-1)&&(k<p));j--)>
{
b[j][y]=a[k];
k++;
}
y++;
}
//do
//{
//i=i+1;
/*for(i=i+1;i<m;i++)>
{
for(j=i;((j<n)&&(k><p));j++)>
{
if(b[i][j]==0)
{
b[i][j]=a[k];
k++;
}
}*/
}//while(b[i][j]==0);
for(i=0;i<q1;i++)>
{
for(j=0;j<q;j++)>
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
getch();
}
Posted
Updated 21-Jun-15 2:26am
v2

1 solution

I would just have a pair of indexes (x and y) and a pair of "adders" (dx and dy), possibly with a pair of "limit" indexes (lx and ly)

The adders can each have the value -1, 0, or 1 to control the direction.

Preset x = 0, y = 0, dx = 1, dy = 0, lx = 4, ly = 0
Loop through the 1D array, inserting the data at (x,y), then add the adders (dx, dy). If at the limit check the adders and "rotate 90 degrees" - so (dx = 1, dy = 0) becomes (dx = 0; dy = 1) and so on. Set up the next limits.

Try it by hand, and you'll see what I mean.
The advantage of this approach is that it's a single loop, and it works for any size of 2D array, provided you have the right number of input elements.
 
Share this answer
 
Comments
moyna coder 21-Jun-15 5:33am    
i could not get your idea fully..can u give me the code.
OriginalGriff 21-Jun-15 5:53am    
No! :laugh:
This is your homework, not mine!
moyna coder 21-Jun-15 5:57am    
okay fine thankz.. i am trying for a week to solve 15 tasks..this is a one i couldnt solve easily...any way thankz for your solution..
OriginalGriff 21-Jun-15 5:58am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900