0% found this document useful (0 votes)
34 views27 pages

Lab After 15

The document describes a C/C++ program to find the meet (logical AND) of two Boolean matrices. It defines a function called "findMeet" that takes two Boolean matrices and the result matrix as parameters along with the row and column sizes. The function performs a logical AND operation on corresponding elements of the two matrices and stores the results in the result matrix. The main function takes the two matrices as input, calls findMeet to find their meet, and displays the result matrix.

Uploaded by

shresthaloozah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views27 pages

Lab After 15

The document describes a C/C++ program to find the meet (logical AND) of two Boolean matrices. It defines a function called "findMeet" that takes two Boolean matrices and the result matrix as parameters along with the row and column sizes. The function performs a logical AND operation on corresponding elements of the two matrices and stores the results in the result matrix. The main function takes the two matrices as input, calls findMeet to find their meet, and displays the result matrix.

Uploaded by

shresthaloozah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Lab no: 15

Date:2023/10/07
Title: WAP in C/C++ to find the meet of two Boolean matrices.

Boolean matrix:
In mathematics, a Boolean matrix is a matrix with entries from a Boolean algebra. When the two-element
Boolean algebra is used, the Boolean matrix is called a logical matrix. (In some contexts, particularly computer
science, the term "Boolean matrix" implies this restriction.)

Let U be a non-trivial Boolean algebra (i.e., with at least two elements). Intersection, union, complementation,
and containment of elements are expressed in U. Let V be the collection of n × n matrices that have entries taken
from U. Complementation of such a matrix is obtained by complementing each element. The intersection or
union of two such matrices is obtained by applying the operation to entries of each pair of elements to obtain the
corresponding matrix intersection or union. A matrix is contained in another if each entry of the first is
contained in the corresponding entry of the second.

The product of two Boolean matrices is expressed as follows:

(𝐴, 𝐵)𝑖𝑗 =∪ (𝐴 𝑖𝑘, 𝐵 𝑘𝑗)

Compiler: Dev C++


Language: C
Source Code:
#include <stdio.h>
void findMeet(int mat1[][100], int mat2[][100], int result[][100], int rows, int cols) //function for meet of boolean matrix
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
result[i][j] = mat1[i][j] && mat2[i][j]; // Perform logical AND operation
}
}
}
void displayMatrix(int mat [][100], int rows, int cols) // Function to display a matrix
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
int main()
{
int mat1[100][100], mat2[100][100], result[100][100];
int rows, cols
printf("Enter the number of rows and columns for the matrices: ");
scanf("%d %d", &rows, &cols);
printf("Enter the elements of the first matrix (0 or 1):\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
scanf("%d", &mat1[i][j]);
}
}
printf("Enter the elements of the second matrix (0 or 1): \n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
scanf("%d", &mat2[i][j]);
}
}
findMeet(mat1, mat2, result, rows, cols);
printf("Meet of the two matrices:\n");
displayMatrix(result, rows, cols);
return 0;
}
Output:
Lab no: 16 Date:2023/10/07
Title: WAP in C/C++ to find the product of two Boolean matrices.

Boolean matrix:
In mathematics, a Boolean matrix is a matrix with entries from a Boolean algebra. When the two-element
Boolean algebra is used, the Boolean matrix is called a logical matrix. (In some contexts, particularly computer
science, the term "Boolean matrix" implies this restriction.)

Let U be a non-trivial Boolean algebra (i.e., with at least two elements). Intersection, union, complementation,
and containment of elements are expressed in U. Let V be the collection of n × n matrices that have entries taken
from U. Complementation of such a matrix is obtained by complementing each element. The intersection or
union of two such matrices is obtained by applying the operation to entries of each pair of elements to obtain the
corresponding matrix intersection or union. A matrix is contained in another if each entry of the first is
contained in the corresponding entry of the second.

The product of two Boolean matrices is expressed as follows:

(𝐴, 𝐵)𝑖𝑗 =∪ (𝐴 𝑖𝑘, 𝐵 𝑘𝑗)

Compiler: Dev C++


Language: C
Source Code:
#include <stdio.h>
#define ROWS 3
#define COLS 3
void multiplyBooleanMatrices(int mat1[ROWS][COLS], int mat2[ROWS][COLS], int result[ROWS][COLS])
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
result[i][j] = 0;
for (int k = 0; k < COLS; k++)
{
result[i][j] += mat1[i][k] && mat2[k][j];
}
}
}
}

void displayMatrix(int mat[ROWS][COLS])


{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
int main()
{
int matrix1[ROWS][COLS] = {{1, 0, 1},
{0, 1, 0},
{1, 0, 1}};
int matrix2[ROWS][COLS] = {{1, 1, 0},
{0, 1, 1},
{1, 0, 0}};
int resultMatrix[ROWS][COLS];
multiplyBooleanMatrices(matrix1, matrix2, resultMatrix);
printf("Matrix 1:\n");
displayMatrix(matrix1);
printf("Matrix 2:\n");
displayMatrix(matrix2);
printf("Product of Matrix 1 and Matrix 2:\n");
displayMatrix(resultMatrix);
return 0;
}

Output:
Lab no: 17 Date: 2023/09/11
Title: WAP in C/C++ to construct the truth table of Conjunction.

Conjunction:
The conjunction can be described as a statement, which can be formed by adding two statements with the help of
connector AND. The symbol ∧ is used for the conjunction. We can read this symbol as "and". If two statements,
x, and y are joined in a statement, then the conjunction can be indicated symbolically in the form of x ∧ y. This
statement will be true when both the combined statements are true. In all the other cases, it will be false. The
conjunction of x and y is shown in the following image:

Compiler: Dev C++

Language: C
Source Code:

#include <stdio.h>
int conjunction(int a, int b) // Function to compute the conjunction (logical AND) of two Boolean values
{
return a && b;
}
int main()
{
int a, b;
printf("Truth Table for Conjunction (AND):\n");
printf("a\tb\ta AND b\n");
printf("-----------------\n");
for (a = 0; a <= 1; a++) // Loop through all possible combinations of a and b (0 and 1)
{
for (b = 0; b <= 1; b++)
{
int result = conjunction(a, b); // Compute the result of a AND b
printf("%d\t%d\t%d\n", a, b, result); // Display the values and result
}
}
return 0;
}
Output:
Lab no: 18 Date: 2023/09/11
Title: WAP in C/C++ to construct the truth table of Disjunction.

Disjunction:
A disjunction statement of p and q is written as 𝑃 V 𝑄 .This means p or q. Statements p and q have either a
true or false value. To evaluate if a disjunction statement is true, either one or both statements need to be true. In
other words, either p or q is true. The truth values of p q are listed in the truth table below:

Compiler: Dev C++


Language: C
Source Code:
#include <stdio.h>
int OR(int a, int b) // Function to compute the OR operation
{
return a || b;
}
int main()
{
int a, b;
printf("Truth Table for Disjunction (OR):\n");
printf("A\tB\tA OR B\n");
for (a = 0; a <= 1; a++) // Loop through all possible values of A and B (0 or 1)
{
for (b = 0; b <= 1; b++)
{
printf("%d\t%d\t%d\n", a, b, OR(a, b));
}
}
return 0;
}

Output:
Lab no: 19 Date: 2023/09/11
Title: WAP in C/C++ to construct the truth table of Implication.

Implication:
An implication statement can be represented in the form "if.... then". The symbol ⇒ is used to show the
implication. Suppose there are two statements, P and Q. In this case, the statement "if P then Q" can also be
written as P ⇒ Q or P → Q, and it will be read as "P implies Q". In this implication, the statement P is a hypothesis,
which is also known as premise and antecedent, and the statement Q is conclusion, which is also known as the
consequent. The truth table of the statement "if P then Q" is described as follows:

Compiler: Dev C++

Language: C
Source Code:
#include <stdio.h>
int implication(int a, int b) // Function to compute the implication operation
{
return (!a) || b;
}
int main()
{
int a, b;
printf("Truth Table for Implication (->):\n");
printf("A\tB\tA -> B\n");
for (a = 0; a <= 1; a++) // Loop through all possible values of A and B (0 or 1)
{
for (b = 0; b <= 1; b++)
{
printf("%d\t%d\t%d\n", a, b, implication(a, b));
}
}
return 0;
}

Output:
Lab no: 20 Date: 2023/09/11

Title: WAP in C/C++ to check the validity of arguments using truth tables.

Truth table:
A truth table is a breakdown of all the possible truth values returned by a logical expression. A truth value is
typically either true or false, or 1 or 0. In some cases, the value might be based on another binary system, such as
on and off or open and closed, but these are not as common. Truth tables are used in Boolean algebra and in other
areas of mathematics and science that rely on Boolean logic to show the possible outcomes of an expression or
operation in terms of its truth or falseness.

Compiler: Dev C++


Language: C
Souce Code:
#include <stdio.h>
int evaluateExpression(int p, int q) // Function to compute the logical expression
{
return (!p) || q; // We can modify this function to evaluate our specific logical expression
}
int main()
{
int p, q;
printf("Truth Table for Argument Validity Check:\n");
printf("P\tQ\tExpression\n");
for (p = 0; p <= 1; p++) // Loop through all possible values of P and Q (0 or 1)
{
for (q = 0; q <= 1; q++)
{
int result = evaluateExpression(p, q);
printf("%d\t%d\t%d\n", p, q, result);
}
}
int isArgumentValid = 1; // Assume it's valid initially // Check if the expression is valid (always true)
for (p = 0; p <= 1; p++)
{
for (q = 0; q <= 1; q++)
{
int result = evaluateExpression(p, q);
if (!result)
{
isArgumentValid = 0; // Set to false if any result is false
break;
}
}
}
if (isArgumentValid)
{
printf("The argument is valid (always true).\n");
}
else
{
printf("The argument is not valid (not always true).\n");
}
return 0;
}

Output:
Lab no: 21 Date: 2023/09/11
Title: WAP in C/C++ to represent graph.

Graph:

A graph is a pictorial representation of any data in an organized manner. The graph shows the relationship
between variable quantities. In graph theory, the graph represents the set of objects that are related in some
sense to each other. The objects are basically mathematical concepts, expressed by vertices or nodes and the
relation between the pair of nodes, are expressed by edges. Formally, a graph is denoted as a pair G(V, E).

Where V represents the finite set vertices and E represents the finite set edges.

Therefore, we can say a graph includes non-empty set of vertices V and set of edges E.
Compiler: Dev C++

Language: C++

Source Code:

#include <iostream>

#include <vector>

using namespace std;

class Graph

private:

int V; // Number of vertices

vector<vector<int>> adjList; // Adjacency list

public:

Graph(int vertices)

V = vertices;

adjList.resize(V);

void addEdge(int v1, int v2) // Function to add an edge to the graph

adjList[v1].push_back(v2);

adjList[v2].push_back(v1); // For an undirected graph, add both directions

void printGraph() // Function to print the graph

for (int v = 0; v < V; v++)


{

cout << "Vertex " << v << " connected to:";

for (int neighbor : adjList[v])

cout << " " << neighbor;

cout << endl;

};

int main()

Graph g(5); // Create a graph with 5 vertices

g.addEdge(0, 1); // Add edges to the graph

g.addEdge(0, 2);

g.addEdge(1, 3);

g.addEdge(2, 3);

g.addEdge(3, 4);

g.printGraph(); // Print the graph

return 0;

}
Output:
Lab no: 22 Date: 2023/09/11
Title: WAP in C/C++ to check if the input relation is Equivalence relation or not.

Equivalence Relation:

An equivalence relation is a kind of binary relation that should be reflexive, symmetric and transitive. The well-
known example of an equivalence relation is the “equal to (=)” relation. In other words, two elements of the
given set are equivalent to each other if they belong to the same equivalence class. A relation R on a set A is
said to be an equivalence relation if and only if the relation R is reflexive, symmetric and transitive. The
equivalence relation is a relationship on the set which is generally represented by the symbol “∼”.

Compiler: Dev C++

Language: C

Source Code:
#include <iostream>

#include <vector>

#include <set>

using namespace std;

bool isReflexive(const vector<pair<int, int>>& relation, int n) // Function to check reflexivity

for (int i = 0; i < n; i++)

bool found = false;

for (const auto& pair : relation)

if (pair.first == i && pair.second == i)

found = true;

break;

if (!found)

return false;

return true;

bool isSymmetric(const vector<pair<int, int>>& relation) // Function to check symmetry


{

for (const auto& pair : relation)

if (pair.first != pair.second)

bool found = false;

for (const auto& reversePair : relation)

if (reversePair.first == pair.second && reversePair.second == pair.first)

found = true;

break;

if (!found)

return false;

return true;

bool isTransitive(const vector<pair<int, int>>& relation) // Function to check transitivity

for (const auto& pair1 : relation)


{

for (const auto& pair2 : relation)

if (pair1.second == pair2.first)

bool found = false;

for (const auto& pair3 : relation)

if (pair1.first == pair3.first && pair2.second == pair3.second)

found = true;

break;

if (!found)

return false;

return true;

int main()

{
int n;

cout << "Enter the number of elements in the set: ";

cin >> n;

vector<pair<int, int>> relation;

cout << "Enter the pairs of related elements (Enter -1 to stop):" << endl;

while (true)

int a, b;

cin >> a;

if (a == -1)

break;

cin >> b;

relation.push_back(make_pair(a, b));

bool isEquivalence = isReflexive(relation, n) && isSymmetric(relation) && isTransitive(relation);

if (isEquivalence)

cout << "The given relation is an equivalence relation." << endl;

else

cout << "The given relation is not an equivalence relation." << endl;

}
return 0;

Output:

You might also like