-
Notifications
You must be signed in to change notification settings - Fork 1
/
2D-Array.cpp
73 lines (62 loc) · 1.21 KB
/
2D-Array.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
63
64
65
66
67
68
69
70
71
72
73
//Spiral print
#include<iostream>
//#include<array>
//#include<vector>
using namespace std;
int main()
{
// input of aaray
int n,m;
cout<<"\nENTER SIZE N & M:\t";
cin>>n>>m;
int a[n][m];
cout<<"\nEnter matrix:\n";
for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
{
cin>>a[i][j];
}
}
// here we go
int sr=0,sc=0,er=n-1,ec=m-1;
while(sr<=er&&sc<=ec)
{
for(int i=sc;i<=ec;i++)
{
cout<<a[i][sr]<<"\t";
}
sr++;
for(int i=sr;i<=er;i++)
{
cout<<a[ec][i]<<"\t";
}
ec--;
if(ec>sc)
{
for(int i=ec;i>=sc;i--)
{
cout<<a[i][er]<<"\t";
}
er--;
}
if(er>sr)
{
for(int i=er;i>=sr;i--)
{
cout<<a[sc][i]<<"\t";
}
sc++;
}
}
//// Output of array
// for (int i=0; i<n; i++)
// {
// for (int j=0; j<m; j++)
// {
// cout<<a[i][j]<<"\t";;
// }
// cout<<endl;
// }
return 0;
}