-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathZeroMatrix01.cpp
More file actions
76 lines (60 loc) · 1.33 KB
/
ZeroMatrix01.cpp
File metadata and controls
76 lines (60 loc) · 1.33 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
#include<bits/stdc++.h>
using namespace std;
void ZeroMatrix(int row, int column) {
int zeroMatrix[row][column];
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cin >> zeroMatrix[i][j];
}
}
bool zeroRows[row];
bool zeroColumns[column];
memset(zeroRows, false, sizeof(zeroRows));
memset(zeroColumns, false, sizeof(zeroColumns));
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
if(!zeroMatrix[i][j]) {
zeroRows[i] = true;
zeroColumns[j] = true;
}
}
}
// Nullify Rows
for (int i = 0; i < row; ++i) {
if(zeroRows[i]) {
for (int j = 0; j < column; ++j) {
zeroMatrix[i][j] = 0;
}
}
}
// Nullify Columns
for (int i = 0; i < column; ++i) {
if(zeroColumns[i]) {
for (int j = 0; j < row; ++j) {
zeroMatrix[j][i] = 0;
}
}
}
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << zeroMatrix[i][j] << " ";
}
cout << endl;
}
}
int main() {
int row, column;
cin >> row >> column;
ZeroMatrix(row, column);
}
/*
Input:
3 3
1 1 1
1 0 1
1 1 0
Output:
1 0 0
0 0 0
0 0 0
*/