-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiral_of_2d_array.cpp
More file actions
56 lines (51 loc) · 1.26 KB
/
spiral_of_2d_array.cpp
File metadata and controls
56 lines (51 loc) · 1.26 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
#include <iostream>
#include <cstdio>
using namespace std;
void print_spiral(int *arr,int m,int n){
int t=0,b=m-1,l=0,r=n-1,dir=0;
while(t<=b && l<=r){
if(dir==0){
for(int i=l;i<=r;++i){
//printf("%d ",arr[t][i]);
printf("%d ",*((arr+t*n)+i));
}
t++;
}
else if(dir==1){
for(int i=t;i<=b;++i){
//printf("%d ",arr[i][r]);
printf("%d ",*((arr+i*n)+r));
}
r--;
}
else if(dir==2){
for(int i=r;i>=l;--i){
//printf("%d ",arr[b][i]);
printf("%d ",*((arr+b*n)+i));
}
b--;
}
else if(dir==3){
for(int i=b;i>=t;--i){
//printf("%d ",arr[i][l]);
printf("%d ",*((arr+i*n)+l));
}
l++;
}
dir=(dir+1)%4;
}
}
int main(){
int m,n;
printf("enter the size of 2d array as m and n\n");
scanf("%d%d",&m,&n);
int arr[m][n];
printf("enter the elements of the 2d array.......\n");
for(int i=0;i<m;++i){
for(int j=0;j<n;++j){
scanf("%d",&arr[i][j]);
}
}
print_spiral((int*)arr,m,n);
return 0;
}