-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19.SpiralPrint.cpp
More file actions
50 lines (43 loc) · 973 Bytes
/
19.SpiralPrint.cpp
File metadata and controls
50 lines (43 loc) · 973 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
#include<iostream>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
int arr[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>arr[i][j];
}
}
int row=n;
int col=m;
int count=0;
int total=n*m;
//index initialization
int startingRow=0;
int startingCol=0;
int endingRow=row-1;
int endingCol=col-1;
while(count<total){
//print starting row
for(int index=startingCol;index<=endingCol;index++){
cout<<arr[startingRow][index]<<" ";
}
startingRow++;
//printing ending column
for(int index=startingRow;index<=endingRow;index++){
cout<<arr[index][endingCol]<<" ";
}
endingCol--;
//printing ending row
for(int index=endingCol;index>=startingCol;index--){
cout<<arr[endingRow][index]<<" ";
}
endingRow--;
//printing starting column
for(int index=endingRow;index>=startingRow;index--){
cout<<arr[index][startingCol]<<" ";
}
startingCol++;
}
}