# How to print element of 2d matrix in spiral?

602 views
How to print element of 2d matrix in spiral?
posted Oct 23, 2013

+1 vote

See the following code

``````#define ROWS 5
#define COLS 5

int main()
{
int arr[ROWS][COLS] =  {{ 1, 2, 3, 4, 17},
{ 5, 6, 7, 8, 18},
{ 9,10,11,12, 19},
{13,14,15,16, 20},
{21,22,23,24, 25}};

// Four direction counters of current movement
// Horizontal right, vertical bottom, horizontal left and vertical top respectively
int hr, vb, hl, vt;

// levl indicates current depth of our imaginary rectangle into array. Starting value is zero
// since we are looping on the boundaries and ending value is the inner most rectangle
int levl;
for (levl=0; levl < COLS-levl; levl++)
{
for(hr=levl; hr < COLS-levl; hr++)   // go right
printf("%d ,", arr[levl][hr]);

for(vb=levl+1; vb < COLS-levl; vb++) // go down
printf("%d ,", arr[vb][hr-1]);

for(hl=vb-1; hl-1 >= levl; hl--)  // go left
printf("%d ,", arr[vb-1][hl-1]);

for(vt=vb-1; vt-1 > levl; vt--)  // go up
printf("%d ,", arr[vt-1][hl]);
}

return 0;
}
``````

Output

``````1 ,2 ,3 ,4 ,17 ,18 ,19 ,20 ,25 ,24 ,23 ,22 ,21 ,13 ,9 ,5 ,6 ,7 ,8 ,12 ,16 ,15 ,14 ,10 ,11 ,
``````
What is the complexity of this code ??
+1 vote
``````void main()
{
int r;      // no of row
int c;      //no of column
int x;
int z;
int l=0,n=0;
int a[20][20];

printf("Enter the row => ");
scanf("%d",&r);
printf("Enter the column => ");
scanf("%d",&c);

mat1(a,r,c,l,n,c*r);        //  This function will print matrix

printf("\n MATRIX => \n");
for(x=0;x<r;x++)
{
printf("\n\n\n");
for(z=0;z<c;z++)
printf("    %d",a[x][z]);
}
getch();
}

int mat1(int a[][20],int r,int c,int l,int n,int m)
{
if(n<m)
{
int x,z;
for(x=l;x<c;x++)
a[l][x]=++n;
for(z=l+1;z<r;z++)
a[z][c-1]=++n;
c--;
mat2(a,r,c,l,n,m);
}
else
return 1;
}

int mat2(int a[][20],int r,int c,int l,int n,int m)
{
int x,z;
if(n<m)
{
for(x=c-1;x>=l;x--)
a[r-1][x]=++n;
for(z=r-2;z>=l+1;z--)
a[z][l]=++n;
r--;
l++;
mat1(a,r,c,l,n,m);
}
else
return 1;
}
``````

complexity O(no of element in matrix).

Thanks.

Assume 4*4

You have to walk as this, Row-Column[Forward] then Row-Column[Reverse]

1. Ro ==> C0 to C2
C3 ==> R0 to R2

2. R3 ==> C3 to C1
C0 ==> R3 to R1

With above you will be able to print outer layer. You have to increment all the value[ie index] by one and try for first inner later.

Hope this 2 steps will be unique and will work for N*N matrix also.

I let yo to write code for whole. Let me know if you find difficulties and let others also comment. with their ideas..

Good Idea :)
Can you please write code too ??
Similar Questions
``````The input tree is as shown below
40
/ \
20      60
/ \       \
10      30      80
\            /  \
15        70    90

Output: 15 30 60 80 90
``````
+1 vote

C: Print the elements of a matrix in anti spiral order?

Input Matrix

``````1 2 3
4 5 6
7 8 9
``````

Output

``````5 6 9 8 7 4 1 2 3
``````

1,1,2,2,2,6,6,6,7,7,7,7,7,7,7,8,8,9,9,9

Example:
Input = 1 Output=0 (First index of 1).
Input = 2 Output=2 (First index of 2).
Input = 6 Output= 5 (First index of 6).
Input = 7 Output= 8 (First index of 7).
Input = 8 Output=15 (First index of 8).
Input = 9 Output=17 (First index of 9).