-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspiral_print.cpp
51 lines (40 loc) · 927 Bytes
/
spiral_print.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
// Code to print an array spirally
// errors in exception cases.
#include <iostream>
using namespace std;
void spiralPrint(int arr[][5],int R,int C)
{
int startR=0 ,startC=0 ,endR=R-1 ,endC=C-1 ;
while(endR-startR >=0 && endC-startC>=0 )
{
for(int i=startC;i<=endC;i++)
{
cout<<arr[startR][i]<<" ";
}
for(int i=startR+1;i<=endR;i++)
{
cout<<arr[i][endC]<<" ";
}
for(int i=endC-1;i>=startC;i--)
{
cout<<arr[endR][i]<<" ";
}
for(int i=endR-1;i>=startR+1;i--)
{
cout<<arr[i][startC]<<" ";
}
startR++ ;
startC++ ;
endR--;
endC-- ;
}
}
int main() {
int arr[][5]={
{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
};
spiralPrint(arr,3,5);
return 0;
}