-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiralPrinting.cpp
62 lines (53 loc) · 1.29 KB
/
spiralPrinting.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
57
58
59
60
61
62
#include <iostream>
using namespace std;
int main()
{
int arr[5][6] = {{1, 2, 3, 4, 5, 6},
{9, 8, 7, 6, 5, 4},
{1, 2, 3, 4, 5, 6},
{9, 8, 7, 6, 5, 4},
{1, 2, 3, 4, 5, 6}};
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
// Spiral Printing Of 2D Array
cout << "Spiral Printing Of 2D Array" << endl;
int left = 0;
int right = 5;
int top = 0;
int bottom = 4;
while (top <= bottom && left <= right)
{
for (int i = left; i <= right; i++)
{
cout << arr[top][i] << " ";
}
top++;
// cout << endl;
for (int i = top; i <= bottom; i++)
{
cout << arr[i][right] << " ";
}
right--;
// cout << endl;
if (top <= bottom)
{
for (int i = right; i >= left; i--)
{
// cout<<"the bottom and i at "<<" "<<bottom<<i<<" "<<endl;
cout << arr[bottom][i] << " ";
}
bottom--;
}
for (int i = bottom; i >= top; i--)
{
cout << arr[i][left] << " ";
}
left++;
}
}