-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP74MATRIX.cpp
More file actions
66 lines (47 loc) · 1.34 KB
/
P74MATRIX.cpp
File metadata and controls
66 lines (47 loc) · 1.34 KB
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
// Spiral Order Matrix Traversal
#include<iostream>
using namespace std;
int main(){
int n,m;
cout<<"give the order of matrix you want";
cin>>n>>m;
int arr[n][m];
for (int i = 0; i < n; i++) // taking input of matrix
{
for (int j = 0; j < m; j++)
{
cin>>arr[i][j];
}
}
// spiral order print
int row_start=0, row_end=n-1, column_start=0, column_end=m-1;
while (row_start <= row_end && column_start <= column_end)
{
// for row_start
//In 1st row we r printing all the elements from starting to end column and put + for starting ROW
for (int col = column_start; col <= column_end; col++)
{
cout<< arr[row_start][col]<<" ";
}
row_start++;
// for column_end
for (int row = row_start; row <= row_end; row++)
{
cout<< arr[row][column_end]<<" ";
}
column_end--;
// for row_end
for (int col = column_end; col >= column_start; col--)
{
cout<< arr[row_end][col]<<" ";
}
row_end--;
// for column_start
for (int row = row_end; row >= row_start; row--)
{
cout<< arr[row][column_start]<<" ";
}
column_start++;
}
return 0;
}