-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.cpp
81 lines (71 loc) · 2.08 KB
/
5.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
74
75
76
77
78
79
80
81
// print spiral loop in 2-d array
// in this we will first interate to the first row then to the last column of array then to the last row/end row of the array and at last to the first column thus creating a spiral anfd then moving inwards;
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int a[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>a[i][j];
}
}
int start_row=0,start_column=0,row_end=n-1,column_end=m-1;
while(start_row<=row_end && start_column<=column_end){
// startring row
for(int col=start_column;col<=column_end;col++){
cout<< a[start_row][col]<<" ";
}
start_row++;
// end column
for(int row = start_row;row<=row_end;row++){
cout<< a[row][column_end]<<" ";
}
column_end--;
// end row
for(int col=column_end;col>=start_column;col--){
cout<< a[row_end][col]<<" ";
}
row_end--;
// starting column
for(int row=row_end;row>=start_row;row--){
cout<< a[row][start_column]<<" ";
}
start_column++;
}
return 0;
}
// transport of an 2-D array
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int a[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>a[i][j];
}
}
for(int i=0;i<n;i++){
// j is starting from i bcoz we only want element above diagnol element to be converted to ij to ji
for(int j=i;j<m;j++){
// we will swap it using tem variable
int temp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cout<<a[i][j]<<" ";
}cout<<endl;
}
return 0;
}
// https://drive.google.com/file/d/1Y-dVpTnL_OVYNl-9MYbAvrup26xdIPUQ/view