-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq1.8.cpp
More file actions
67 lines (57 loc) · 1.79 KB
/
q1.8.cpp
File metadata and controls
67 lines (57 loc) · 1.79 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
67
#include "common_header.hpp"
#include <iostream>
#include <list>
#include <vector>
using namespace std;
void replace_row_with_zeros(int *matrix, int row, int N) {
for (int cols = 0; cols < N; cols++)
*(matrix + row * N + cols) = 0;
}
void replace_col_with_zeros(int *matrix, int col, int M, int N) {
for (int rows = 0; rows < M; rows++)
*(matrix + rows * N + col) = 0;
}
int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) {
int M = 3, N = 5;
int matrix[M][N] = {{1, 0, 3, 4, 5}, {6, 7, 8, 0, 10}, {11, 12, 13, 14, 15}};
cout << "original matrix \n";
myspace::print_matrix(&matrix[0][0], M, N);
int it;
list<int> list_rows, list_cols;
vector<int> vector_rows_zero, vector_cols_zero, vector_rem_rows,
vector_rem_cols;
for (it = 0; it < M; it++)
list_rows.push_back(it);
for (it = 0; it < N; it++)
list_cols.push_back(it);
START_LOOP:
if (vector_rows_zero.size() > 0)
for (int rows : vector_rows_zero)
replace_row_with_zeros(&matrix[0][0], rows, N);
if (vector_cols_zero.size() > 0)
for (int cols : vector_cols_zero)
replace_col_with_zeros(&matrix[0][0], cols, M, N);
for (int rows : vector_rem_rows)
list_rows.remove(rows);
for (int cols : vector_rem_cols)
list_cols.remove(cols);
vector_rows_zero.clear();
vector_cols_zero.clear();
vector_rem_rows.clear();
vector_rem_cols.clear();
for (int rows : list_rows) {
for (int cols : list_cols) {
if (matrix[rows][cols] == 0) {
vector_rows_zero.push_back(rows);
vector_cols_zero.push_back(cols);
vector_rem_rows.push_back(rows);
vector_rem_cols.push_back(cols);
goto START_LOOP;
}
}
vector_rem_rows.push_back(rows);
}
cout << "modified matrix \n";
myspace::print_matrix(&matrix[0][0], M, N);
return 0;
}