-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSpiral_Matrix.c
62 lines (61 loc) · 1.14 KB
/
Spiral_Matrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <stdlib.h>
void Spiral_Order(int n,int arr[n][n])
{
int dir,L,R,T,B;
dir=L=T=0;
R=B=(n-1);
while(L<=R && T<=B)
{
if(dir==0)
{
for(int i=L;i<=R;i++)
{
printf("%d ",arr[T][i]);
}
T++;
}
else if(dir==1)
{
for(int i=T;i<=B;i++)
{
printf("%d ",arr[i][R]);
}
R--;
}
else if(dir==2)
{
for(int i=R;i>=L;i--)
{
printf("%d ",arr[B][i]);
}
B--;
}
else if(dir==3)
{
for(int i=B;i>=T;i--)
{
printf("%d ",arr[i][L]);
}
L++;
}
dir=(dir+1)%4;
}
}
int main()
{
int size;
printf("Enter Size of array : ");
scanf("%d",&size);
int arr[size][size];
printf("Enter elements of array : ");
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
scanf("%d",&arr[i][j]);
}
}
Spiral_Order(size,arr);
return 0;
}