- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathspiralprint.cpp
59 lines (47 loc) · 1.23 KB
/
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
#include<bits/stdc++.h>
usingnamespacestd;
#definelllonglong
#definepb push_back
#definemod1000000007
#defineendl'\n'
voidspiral(int arr[][10],int n,int m){
int startRow=0;
int endRow=n-1;
int startCol=0;
int endCol=m-1;
//outer loop
while(startCol<=endCol and startRow<=endRow){
//start row
for(int col=startCol;col<=endCol;col++){
cout<<arr[startRow][col]<<"";
}
//end col
for(int row=startRow+1;row<=endRow;row++){
cout<<arr[row][endCol]<<"";
}
//end row
for (int col=endCol-1;col>=startCol;col--) {
if(startRow==endRow){
break;
}
cout<<arr[endRow][col]<<"";
}
for(int row=endRow-1;row>=startRow+1;row--){
if(startCol==endCol){
break;
}
cout<<arr[row][startCol]<<"";
}
startRow++;
startCol++;
endRow--;
endCol--;
}
}
intmain(){
int n=4;
int m=4;
int arr[][10] = { {1, 2, 3, 4},{12, 13, 14, 5},{11, 16, 15, 6},{10, 9, 8, 7}};
spiral(arr,n,m);
return0;
}