MCPcopy Create free account
hub / github.com/SR-Sunny-Raj/Hacktoberfest2021-DSA / findOriginalMatrix

Function findOriginalMatrix

07. Matrix/Generate_Matrix.cpp:29–105  ·  view source on GitHub ↗

Function to find the matrix, A[][] satisfying the given conditions

Source from the content-addressed store, hash-verified

27// Function to find the matrix, A[][]
28// satisfying the given conditions
29void findOriginalMatrix(
30 vector<vector<int> > B, int N, int M)
31{
32 // Store the final matrix
33 int A[N][M];
34
35 // Initialize all the elements of
36 // the matrix A with 1
37 for (int i = 0; i < N; ++i) {
38 for (int j = 0; j < M; ++j) {
39 A[i][j] = 1;
40 }
41 }
42
43 // Traverse the matrix B[][] row-wise
44 for (int i = 0; i < N; ++i) {
45 for (int j = 0; j < M; ++j) {
46
47 // If B[i][j] is equal to 0
48 if (B[i][j] == 0) {
49
50 // Mark all the elements of
51 // ith row of A[][] as 0
52 for (int k = 0; k < M; ++k) {
53 A[i][k] = 0;
54 }
55
56 // Mark all the elements of
57 // jth column of A[][] as 0
58 for (int k = 0; k < N; ++k) {
59 A[k][j] = 0;
60 }
61 }
62 }
63 }
64
65 // Check if the matrix B[][] can
66 // be made using matrix A[][]
67 for (int i = 0; i < N; ++i) {
68 for (int j = 0; j < M; ++j) {
69
70 // Store the bitwise OR of
71 // all elements of A[][] in
72 // ith row and jth column
73 int c = 0;
74
75 // Traverse through ith row
76 for (int k = 0; k < M; ++k) {
77 if (c == 1)
78 break;
79 c += A[i][k];
80 }
81
82 // Traverse through jth column
83 for (int k = 0; k < N; ++k) {
84 if (c == 1)
85 break;
86 c += A[k][j];

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected