-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathspiral_matrix.cpp
More file actions
70 lines (51 loc) · 967 Bytes
/
spiral_matrix.cpp
File metadata and controls
70 lines (51 loc) · 967 Bytes
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
63
64
65
66
67
68
69
70
#include<iostream>
using namespace std;
void spiral(int a[][3]);
/* --> t 123
456
--> b 789<-- r
^
l */
void spiral(int a[][3])
{
int l,r,dir,t,b;
l=0, r=2,dir=0,t=0,b=2;
while(l<=r&&t<=b)
{
//for printing upper rows
if(dir==0)
{
for(int i=l;i<=r;i++)
cout<<a[t][i];
t++;
}
//printing column from top to bottom
else if(dir==1)
{
for(int i=t;i<=b;i++)
cout<<a[i][r];
r--;
}
//printing rows from right to left
else if(dir==2)
{
for(int i=r;i>=l;i--)
cout<<a[b][i];
b--;
}
//printing column from bottom to top
else if(dir==3)
{
for(int i=b;i>=t;i--)
cout<<a[i][l];
l++;
}
dir=(dir+1)%4;
}
}
int main()
{
int a[3][3]= { {1,2,3},{4,5,6},{7,8,9}};
spiral(a);
return 0;
}