-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
106 lines (91 loc) · 2.46 KB
/
main.cpp
File metadata and controls
106 lines (91 loc) · 2.46 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
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
#include <iostream>
#include <vector>
#include <string>
class Matrix {
public:
Matrix(size_t rows, size_t cols) {
mRows = rows;
mCols = cols;
}
std::vector<std::vector<int>> data;
void print() {
for ( const std::vector<int> &v : data )
{
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
}
}
void checkMatrix() {
bool shouldClearFirstRow = false;
bool shouldClearFirstColumn = false;
// check first row
for (int i = 0; i < mCols; i++) {
if (data[0][i] == 0) {
shouldClearFirstRow = true;
break;
}
}
// check first column
for (int i = 0; i < mRows; i++) {
if (data[i][0] == 0) {
shouldClearFirstColumn = true;
break;
}
}
// mark rows and columns that need nullification
for (int row = 0; row < mRows; row++) {
for (int column = 0; column < mCols; column++) {
if (data[row][column] == 0) {
data[0][column] = 0;
data[row][0] = 0;
}
}
}
// nullify rows
for (int i = mRows - 1; i > 0; i--) {
if (data[i][0] == 0) {
nullifyRow(i);
}
}
// nullify columns
for (int i = mCols - 1; i > 0; i--) {
if (data[0][i] == 0) {
nullifyColumn(i);
}
}
if (shouldClearFirstColumn) {
nullifyColumn(0);
}
if (shouldClearFirstRow) {
nullifyRow(0);
}
}
private:
size_t mRows;
size_t mCols;
void nullifyRow(int row) {
for (int i = 0; i < mCols; i++) {
data[row][i] = 0;
}
}
void nullifyColumn(int column) {
for (int i = 0; i < mCols; i++) {
data[i][column] = 0;
}
}
};
int main() {
Matrix test = Matrix(3, 3);
std::vector<int> first {1, 2, 3};
std::vector<int> second {4, 0, 6};
std::vector<int> third {7, 8, 9};
test.data.push_back(first);
test.data.push_back(second);
test.data.push_back(third);
std::cout << "Before check: " << std::endl;
test.print();
test.checkMatrix();
std::cout << "After check: " << std::endl;
test.print();
return 0;
}