-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzero_matrix.cpp
78 lines (61 loc) · 1.02 KB
/
zero_matrix.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
#include <iostream>
using namespace std;
void zero_matrix(int **matrix,int x,int y){
int rowArray[x];
int colArray[y];
for(int i=0; i<x; i++){
rowArray[i] = 0;
}
for(int j=0; j<y; j++){
colArray[j] = 0;
}
for(int i=0; i<x; i++){
for(int j=0; j<y; j++){
if(matrix[i][j] == 0){
rowArray[i] = 1;
colArray[j] = 1;
}
}
}
for(int i=0; i<x; i++){
if(rowArray[i] == 1){
for(int z=0; z<y; z++){
matrix[i][z] = 0;
}
}
}
for(int j=0; j<y; j++){
if(colArray[j] == 1){
for(int z=0; z<x; z++){
matrix[z][j] = 0;
}
}
}
}
int main(){
int x=4, y=5;
int **matrix;
matrix = new int *[x];
for(int i=0; i<x; i++){
matrix[i] = new int [y];
for(int j=0; j<y; j++){
matrix[i][j] = i+j;
}
}
matrix[2][2] = 0;
for(int i=0; i<x; i++){
for(int j=0; j<y; j++){
cout<< matrix[i][j] << "\t";
}
cout<< endl;
}
cout<< endl;
zero_matrix(matrix,x,y);
for(int i=0; i<x; i++){
for(int j=0; j<y; j++){
cout<< matrix[i][j] << "\t";
}
cout<< endl;
}
return 0;
}