forked from Mosina916/CPP-LV-VMC-DEC-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiralprint.cpp
74 lines (47 loc) · 1011 Bytes
/
spiralprint.cpp
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
71
72
73
74
#include <iostream>
using namespace std;
void spiralprint(int arr[100][100],int rows,int cols){
int sr=0,sc=0,er=rows-1,ec=cols-1;
while(sc<=ec&&sr<=er){
for(int i=sc;i<=ec;i++){
cout<<arr[sr][i]<<" ";
}
sr++;
for(int j=sr;j<=er;j++){
cout<<arr[j][ec]<<" ";
}
ec--;
if(sr<er){
for(int i=ec;i>=sc;i--){
cout<<arr[er][i]<<" ";
}
er--;
}
if(sc<ec){
for(int j=er;j>=sr;j--){
cout<<arr[j][sc]<<" ";
}
sc++;
}
}
}
int main() {
int arr[100][100];
int rows,cols;
cin>>rows>>cols;
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cin>>arr[i][j];
}
// cout<<endl;
}
// PRINTING ARRAY
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
spiralprint(arr,rows,cols);
return 0;
}