-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiralorderprinting.cpp
47 lines (37 loc) · 1.03 KB
/
spiralorderprinting.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
#include<iostream>
using namespace std;
int main(){
int r,c;
cin>>r>>c;
int a[r][c];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cin>>a[i][j];
}
}
//spiral order print
int row_start=0, row_end=r-1, column_start=0, column_end=c-1;
while(row_start <= row_end && column_start <= column_end){
//for row_start
for(int col = column_start; col<=column_end; col++){
cout<<a[row_start][col]<<" ";
}
row_start++;
//for column_end
for(int row=row_start;row<=row_end; row++){
cout<<a[row][column_end]<<" ";
}
column_end--;
//for row_end
for(int col=column_end; col >=column_start; col--){
cout<<a[row_end][col]<<" ";
}
row_end--;
//for column_start
for(int row=row_end; row>=row_start; row--){
cout<<a[row][column_start]<<" ";
}
column_start++;
}
return 0;
}