-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathspiralPrintArray.cpp
More file actions
66 lines (54 loc) · 1005 Bytes
/
spiralPrintArray.cpp
File metadata and controls
66 lines (54 loc) · 1005 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
#include <iostream>
using namespace std;
//spiral print
void spiralPrint(int arr[][170],int row,int col){
int sr=0,er=row-1;
int startCol=0,endCol=col-1;
while(sr<=er && startCol<=endCol){
//print start row
for(int i=startCol;i<=endCol;i++){
cout<<arr[sr][i]<<" ";
}
sr++;
//print end column
for(int j=sr;j<=er;j++){
cout<<arr[j][endCol]<<" ";
}
endCol--;
if(sr<er){
//print end row
for(int i=endCol;i>=startCol;i--){
cout<<arr[er][i]<<" ";
}
er--;
}
if(startCol<endCol){
//print start column
for(int j=er;j>=sr;j--){
cout<<arr[j][startCol]<<" ";
}
startCol++;
}}
}
int main(){
int arr[170][170];
int row,col;
cout<<"Enter Rows and Columns";
cin>>row;
cin>>col;
int value=1;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
arr[i][j]=value;
value++;
}}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
spiralPrint(arr,row,col);
cout<<endl;
return 0;
}