# A Boolean Matrix Question in C++?

Here we will see one interesting Boolean matrix problem. One Boolean matrix is given which contains 0’s and 1’s. Our goal is to find where 1 is marked. If the 1 is marked at position mat[i,j], then we will make all entries to 1 of the row i and column j. Let us see an example. If the matrix is like below −

1 0 0 1
0 0 0 0
0 0 0 0
0 1 0 0

Then after modification, it will be −

1 1 1 1
1 1 0 1
1 1 0 1
1 1 1 1

## Algorithm

### matrixUpdate(matrix[R,C])

begin
define two matrices row[R] and col[C], and fill them with 0
mark the row and column indices of mat[R,C] where 1 is placed into the row[R] and col[C] matrices
check row[R] and col[C] if the place is marked, then fill all places of that row and column with 1’s.
end

## Example

#include <iostream>
#define R 4
#define C 4
using namespace std;
void updateMatrix(bool mat[R][C]) {
bool row[R];
bool col[C];
int i, j;
for (int i = 0; i < R; i++) { //set all elements of row matrix as 0
row[i] = 0;}
for (i = 0; i < C; i++) { //set all elements of col matrix as 0
col[i] = 0;}
for (int i = 0; i < R; i++) { //mark row and col matrix to identify where 1 is present
for (int j = 0; j < C; j++) {
if (mat[i][j] == 1) {
row[i] = 1;
col[j] = 1;
}
}
}
for (i = 0; i < R; i++) { //set all 1s to the row and col, where 1 is marked
for (j = 0; j < C; j++) {
if ( row[i] == 1 || col[j] == 1 ) {
mat[i][j] = 1;
}
}
}
}
void displayMatrix(bool mat[R][C]) {
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cout << mat[i][j];
}
cout << endl;
}
}
main() {
bool mat[R][C] = { {1, 0, 0, 1},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 1, 0, 0}
};
cout << "Given Matrix" << endl;
displayMatrix(mat);
updateMatrix(mat);
cout << "Updated Matrix" << endl;
displayMatrix(mat);
}

## Output

Given Matrix
1001
0000
0000
0100
Updated Matrix
1111
1101
1101
1111