-
Notifications
You must be signed in to change notification settings - Fork 0
/
27_2d_array_challenges.cpp
151 lines (128 loc) · 2.69 KB
/
27_2d_array_challenges.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
using namespace std;
void transpose()
{
int n;
cin >> n;
int a[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
// swap
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 < n; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
return;
}
// In a*b matrix and c*d matrix, b == c and multiplication matrix is of a*d.
void matrix_mutliplication()
{
int fr, fc_sr, sc;
cout << "first matrix input" << endl;
cout << "Rows: ";
cin >> fr;
cout << "Columns: ";
cin >> fc_sr;
int fm[fr][fc_sr];
for (int i = 0; i < fr; i++)
{
for (int j = 0; j < fc_sr; j++)
{
cin >> fm[i][j];
}
}
cout << endl;
cout << "second matrix input" << endl;
cout << "Rows: " << fc_sr << endl; // rows of second matrix should equal to the columns of first matrix
cout << "Columns: ";
cin >> sc;
int sm[fc_sr][sc];
for (int i = 0; i < fc_sr; i++)
{
for (int j = 0; j < sc; j++)
{
cin >> sm[i][j];
}
}
int mm[fr][sc];
for (int i = 0; i < fr; i++)
{
for (int j = 0; j < sc; j++)
{
int sum = 0;
for (int k = 0; k < fc_sr; k++)
{
sum += fm[i][k] * sm[k][j];
}
mm[i][j] = sum;
}
}
for (int i = 0; i < fr; i++)
{
for (int j = 0; j < sc; j++)
{
cout << mm[i][j] << " ";
}
cout << endl;
}
}
// matrix is in sorted order
/*
1 4 7 11
2 5 8 12
3 6 9 16
10 13 14 17
*/
void two_d_mtrx_srch(){
int n,m;
cout<<"Input rows and columns"<<endl;
cin>>n>>m;
int mtrx[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin>>mtrx[i][j];
}
}
int key;
cin>>key;
int i = 0, j = m-1;
while(i<n&&j>=0){
if(mtrx[i][j]==key){
cout<<key<<" @["<<i<<","<<j<<"]"<<endl;
return;
}
if(mtrx[i][j]>key)
j--;
else
i++;
}
cout<<"Key not found"<<endl;
return;
}
int main()
{
// transpose();
// matrix_mutliplication();
two_d_mtrx_srch();
return 0;
}