Write a program that performs zero-one matrix operations, including the join, th
ID: 3740007 • Letter: W
Question
Write a program that performs zero-one matrix operations, including the join, the meet, and the Boolean product. Your matrices should be of type bool and use logical AND (&&) and logical OR (||). Upload the .cpp file along with your captured output or output to a file as a .zip file. Write a program that performs zero-one matrix operations, including the join, the meet, and the Boolean product. Your matrices should be of type bool and use logical AND (&&) and logical OR (||). Upload the .cpp file along with your captured output or output to a file as a .zip file.Explanation / Answer
Solution:
code:
#include <iostream>
using namespace std;
bool **matOR(bool **A, bool **B, int rows, int cols){
bool **C = new bool*[rows];
for(int i = 0; i < rows; ++i){
C[i] = new bool[cols];
for(int j = 0; j < cols; ++j){
C[i][j] = A[i][j] || B[i][j];
}
}
return C;
}
bool **matAND(bool **A, bool **B, int rows, int cols){
bool **C = new bool*[rows];
for(int i = 0; i < rows; ++i){
C[i] = new bool[cols];
for(int j = 0; j < cols; ++j){
C[i][j] = A[i][j] && B[i][j];
}
}
return C;
}
bool **matProduct(bool **A, bool **B, int aRows, int aCols, int bRows, int bCols){
if(aCols == bRows){
bool **C = new bool*[aRows];
for(int i = 0; i < aRows; ++i){
C[i] = new bool[bCols];
for(int j = 0; j < bCols; ++j){
int val = 0;
for(int k = 0; k < aCols; ++k){
val += A[i][k] + B[k][j];
}
C[i][j] = val;
}
}
return C;
}
}
void print(bool **arr, int rows, int cols){
for(int i = 0; i < rows; ++i){
for(int j = 0; j < cols; ++j){
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
int main(){
bool **A;
bool **B;
bool **AorB;
bool **AandB;
bool **AproductB;
int aRows, aCols, bRows, bCols;
cout << "Enter number of rows in A: ";
cin >> aRows;
cout << "Enter number of cols in A: ";
cin >> aCols;
cout << "Enter number of rows in B: ";
cin >> bRows;
cout << "Enter number of cols in B: ";
cin >> bCols;
A = new bool*[aRows];
B = new bool*[bCols];
cout << "Enter Matrix A: " << endl;
for(int i = 0; i < aRows; ++i){
A[i] = new bool[aCols];
for(int j = 0; j < aCols; ++j){
cin >> A[i][j];
}
}
cout << "Enter Matrix B: " << endl;
for(int i = 0; i < bRows; ++i){
B[i] = new bool[bCols];
for(int j = 0; j < bCols; ++j){
cin >> B[i][j];
}
}
cout << "A is" << endl;
print(A, aRows, aCols);
cout << "B is" << endl;
print(B, bRows, bCols);
AorB = matOR(A, B, aRows, aCols);
cout << "A OR B is" << endl;
print(AorB, aRows, aCols);
AandB = matAND(A, B, aRows, aCols);
cout << "A AND B is" << endl;
print(AandB, aRows, aCols);
AproductB = matProduct(A, B, aRows, aCols, bRows, bCols);
cout << "A PRODUCT B is" << endl;
print(AproductB, aRows, bCols);
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.