-
Notifications
You must be signed in to change notification settings - Fork 0
/
cr_zero_matrix.cpp
119 lines (111 loc) · 1.79 KB
/
cr_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
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
/*
2
3 4
1 5 6 0
0 7 9 3
4 0 5 3
3 4
1 5 6 0
1 7 9 3
4 1 0 3
*/
#include <stdio.h>
#define MAXIS 100
int T, N, M;
int a[MAXIS + 1][MAXIS + 1];
static void
clearMemory()
{
int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < M; j++)
a[i][j] = 0;
}
static void
printMatrix()
{
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++)
printf("%d ", a[i][j]);
printf("\n");
}
}
static void
nullifyRow(int row)
{
for (int j = 0; j < M; j++)
a[row][j] = 0;
}
static void
nullifyCol(int col)
{
for (int i = 0; i < N; i++)
a[i][col] = 0;
}
static void
nullifyMatrix()
{
int i, j;
bool firstRow = false;
bool firstCol = false;
// first row
for (i = 0; i < M; i++)
if (a[0][i] == 0) {
firstRow = true;
break;
}
// first col
for (i = 0; i < N; i++)
if (a[i][0] == 0) {
firstCol = true;
break;
}
// rest of the matrix
for (i = 1; i < N; i++)
for (j = 1; j< M; j++)
if (a[i][j] == 0) {
a[i][0] = 0;
a[0][j] = 0;
}
printf("D1:\n");
printMatrix();
// now we know which row and column to be nullify using information stored in previous step.
// row first
for (i = 1; i < N; i++)
if (a[i][0] == 0)
nullifyRow(i);
// cols now
for (j = 1; j < M; j++)
if (a[0][j] == 0)
nullifyCol(j);
printf("D2:\n");
printMatrix();
// now first row:
if (firstRow) nullifyRow(0);
// now rist col:
if (firstCol) nullifyCol(0);
}
int main()
{
freopen("random_matrixes.txt", "r", stdin);
setbuf(stdout, NULL);
int T, i, j;
scanf("%d", &T);
while (T--) {
// inputs:
scanf("%d %d", &N, &M);
for (i = 0; i < N; i++)
for (j = 0; j < M; j++)
scanf("%d", &a[i][j]);
printf("Matrix Before: \n");
printMatrix();
// solve & outputs:
nullifyMatrix();
printf("Matrix After: \n");
printMatrix();
// clear memory:
clearMemory();
}
return 0;
}