-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiral_print.txt
63 lines (46 loc) · 1.23 KB
/
spiral_print.txt
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
spiral print
#include <iostream>
using namespace std;
void spiralprint(int arr[][10],int n,int m){
int startrow =0;
int endrow = n-1;
int startcol = 0;
int endcol = m-1;
while(startrow <= endrow && startcol <= endcol){
for(int i = startcol; i<= endcol;i++){
cout << arr[startrow][i]<<" ";
}
for(int i = startrow+1;i<=endrow;i++){
cout << arr[i][endcol]<<" ";
}
for(int i = endcol-1;i>= startcol;i--){
if(startrow==endrow)
break;
cout<<arr[endrow][i]<<" ";
}
for(int i = endrow -1;i>= startrow+1;i--){
if(startcol == endcol)
break;
cout<<arr[i][startcol]<<" ";
}
startrow++;
endrow--;
startcol++;
endcol--;
}
}
int main(){
int arr[][10] = {{1,2,3,4},
{12,13,14,5},
{11,16,15,6},
{10,9,8,7}};
int n =4,m=4;
for(int i =0;i<n;i++){
for(int j =0;j<m;j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
spiralprint(arr,n,m);
return 0;
}