-
Notifications
You must be signed in to change notification settings - Fork 17
/
Boolean Matrix.cpp
71 lines (60 loc) · 1.47 KB
/
Boolean 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
// A Boolean Matrix Question
#include <bits/stdc++.h>
using namespace std;
#define R 3
#define C 4
void modifyMatrix(bool mat[R][C])
{
bool row[R];
bool col[C];
int i, j;
/* Initialize all values of row[] as 0 */
for (i = 0; i < R; i++)
row[i] = 0;
/* Initialize all values of col[] as 0 */
for (i = 0; i < C; i++)
col[i] = 0;
// Store the rows and columns to be marked as
// 1 in row[] and col[] arrays respectively
for (i = 0; i < R; i++)
{
for (j = 0; j < C; j++)
{
if (mat[i][j] == 1)
{
row[i] = 1;
col[j] = 1;
}
}
}
// Modify the input matrix mat[] using the
// above constructed row[] and col[] arrays
for (i = 0; i < R; i++)
for (j = 0; j < C; j++)
if (row[i] == 1 || col[j] == 1)
mat[i][j] = 1;
}
/* A utility function to print a 2D matrix */
void printMatrix(bool mat[R][C])
{
int i, j;
for (i = 0; i < R; i++)
{
for (j = 0; j < C; j++)
cout << mat[i][j];
cout << endl;
}
}
// Driver Code
int main()
{
bool mat[R][C] = {{1, 0, 0, 1},
{0, 0, 1, 0},
{0, 0, 0, 0}};
cout << "Input Matrix \n";
printMatrix(mat);
modifyMatrix(mat);
printf("Matrix after modification \n");
printMatrix(mat);
return 0;
}