Repository navigation
Expand file tree
/
Copy pathsetRowsAndColumnsToZero.cpp
More file actions
145 lines (101 loc) · 3.2 KB
/
Copy pathsetRowsAndColumnsToZero.cpp
File metadata and controls
145 lines (101 loc) · 3.2 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
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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cassert>
#include <set>
using namespace std;
/*
QUESTION 1.7: Write an algorithm such that if an element in an MXN matrix is 0,
the entire row and oclumn are set to 0;
ex: 1 1 1 1 1 1 0 0 1 0
1 0 1 1 1 0 0 0 0 0
1 1 0 1 1 ==> 0 0 0 0 0
1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 1 0 0 1 0
Approach: Read the entire matrix, save the coordinates into an int array
Then go through the entire array of coordinates and call the
Helper functions updateRow and updateColumn to put zeros in their
respectives rows and columns, but first, make sure those rows and
columns have not been set to zero previously. This is why I have
two sets to store the row and columns that have already been set to 0
*/
void printArray(int array[][5]){
// --> prints the matrix
for(int i =0; i<5; i++){
for(int m =0; m<5; m++){
cout<< array[i][m]<<" ";
}
cout<<endl;
}
cout<<endl;
}
void initializeArray(int array[][5]){
// --> set the whole matrix equal to 1
for(int i =0; i<5; i++){
for(int m =0; m<5; m++){
array[i][m] = 1;
}
}
}
void updateRow(int array[][5], int row_number){
for(int i=0; i<5; i++){
array[row_number][i] = 0;
}
}
void updateColumn(int array[][5], int column_number){
for(int i=0; i<5; i++){
array[i][column_number] = 0;
}
}
void readMatrix(int array[][5], int * coordinates, int * index){
for(int row =0; row<5; row++){
for(int col =0; col<5; col++){
if(array[row][col] == 0){
coordinates[*index] = row;
coordinates[(*index)+1] = col;
*index +=2;
}
}
}
}
int main() {
int array[5][5];
int coordinates[50]; //--> in this case there is a total of 25 coordinates 2
// points for each coordinate
int index = 0;
initializeArray(array);
// printArray(array);
array[1][1] = 0;
array[2][2] = 0;
array[3][4] = 0;
printArray(array);
cout<<endl;
set<int>row_set;
set<int>column_set;
readMatrix(array,coordinates, &index);
cout<<"Reading matrix...."<<endl;
cout<<"Coordinates:"<<endl;
for(int i=0; i<6; i+=2){
cout<<"["<<coordinates[i]<<"] "<<"["<<coordinates[i+1]<< "] "<<endl;
}
cout<<endl;
for(int i = 0; i<6; i +=2){
int row = coordinates[i];
int col = coordinates[i+1];
cout<<"Row: "<<row<<endl;
cout<<"Col: "<<col<<endl;
if(row_set.find(row) == row_set.end()){//-->if it's not found in the set
cout<<"It's not in the row_set"<<endl;
updateRow(array,row);
row_set.insert(row);
}
if(column_set.find(col) == column_set.end()){//-->if it's not found in the set
cout<<"It's not in the col_set"<<endl;
updateColumn(array,col);
column_set.insert(col);
}
}
printArray(array);
return 0;
}