-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSpiral_Order_Matrix.cpp
53 lines (49 loc) · 1.19 KB
/
Spiral_Order_Matrix.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
// WAP To Print Spiral Order Matrix Traversal For Given Two Dimensional Array.
// -------- >
// ----> -
// - -
// - -
// <--------\/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n,m;
cout<<"Enter Row & Collumn For Your Matrix:-"<<endl;
cin>>n>>m;
int Arr[n][m];
cout<< "Enter Your Integer Elements:"<<endl;
for (int i=0; i<n; i++)
{ for (int j=0; j<m; j++)
{
cin>>Arr[i][j];
}
}
int row_start=0,row_end=n-1;
int column_start=0,column_end=m-1;
while (row_start <= row_end && column_start <= column_end)
{
for(int row=column_start; row<= column_end; row++)
{
cout<< Arr[row_start][row] <<" ";
}
row_start++;
for(int col=row_start; col<= row_end; col++)
{
cout<< Arr[col][column_end] <<" ";
}
column_end--;
for(int row=column_end; row>= column_start; row--)
{
cout<< Arr[row_end][row] <<" ";
}
row_end--;
for(int col=row_end; col>= row_start; col--)
{
cout<< Arr[col][column_start] <<" ";
}
column_start++;
}
return 0;
}