0% found this document useful (0 votes)
2 views

Dstl Lab File

The document contains multiple C programming lab exercises focused on set operations, including union, intersection, set difference, symmetric difference, power set, Boolean truth tables, Cartesian products, minimum cost spanning trees, and shortest path algorithms. Each section provides a code implementation along with prompts for user input and expected outputs. The exercises are designed to enhance understanding of discrete structures and logic through practical coding tasks.
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)
2 views

Dstl Lab File

The document contains multiple C programming lab exercises focused on set operations, including union, intersection, set difference, symmetric difference, power set, Boolean truth tables, Cartesian products, minimum cost spanning trees, and shortest path algorithms. Each section provides a code implementation along with prompts for user input and expected outputs. The exercises are designed to enhance understanding of discrete structures and logic through practical coding tasks.
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/ 21

Discrete Structure & Theory of Logic: LAB

1. Write a program in C to create two sets and perform the union operation
on sets:

#include <stdio.h>
int main()
{
int a[3],b[3],c[6],i,j,k=0;
printf("Enter the element of set A\n");
for(i=0;i<3;i++)
{
printf("Enter the element:");
scanf("%d",&a[i]);
}
printf("Enter the element of set B\n");
for(j=0;j<3;j++)
{
printf("Enter the element :");
scanf("%d",&b[j]);

}
for(i=0;i<3;i++)
{
c[i]=a[i];

}
for(j=0;j<3;j++)
{
c[3+j]=b[j];
}
// remove duplicate element
printf("The union of two sets:");
for(i=0;i<6;i++)
{
k=0;
for(j=i+1;j<6;j++)
{
if(c[i]==c[j])
{
k=1;
break;

}
}
if(k==0)
{
printf("%d\t",c[i]);
}
}
return 0;
}
Output:
Discrete Structure & Theory of Logic
LAB2: write a program in C create two sets and perform an
intersection operation.

#include<stdio.h>
int main()
{
int a[50],b[50],c[100],n1,n2,n,k=0,i,j;
// taking input of set A
printf(“Enter the number of element of set A\n”);
scanf(“%d”,&n1);
printf(“Enter the elements of set A\n”);
for(i=0;i<n1;i++)
scanf(“%d”,&a[i]);
// taking input of set B
printf(“Enter the number of elements of set B\n”);
scanf(“%d”,&n2);
printf(“Enter elements of set B\n”);
for(i=0;i<n2;i++)
scanf(“%d”,&b[i]);

// Logic of intersection
for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(a[i]==b[j])
{
c[k] =a[i];
k++;
}
}
}
// Printing the elements of intersection of set A and Set B
printf(“intersection of set A and Set B is: \n”);
for(i=0;i<k;i++)
printf(“%d\n”,c[i]);
return 0;
}

Output:
LAB3: write a program in C create two sets and perform a set difference operation.

#include<stdio.h>
int main()
{
//how to find the difference
int A[20],B[20];
int rangeA, rangeB, i,j,k;

printf("Get range of set A\n");


scanf("%d",&rangeA);
for(i=0;i<rangeA;i++)
{
printf("Get here value of set A:");
scanf("%d",&A[i]);
}
printf("Get range of set B\n");
scanf("%d",&rangeB);
for(k=0;k<rangeB;k++)
{
printf("Get here value of set B:");
scanf("%d",&B[k]);
}
printf("Difference of set (A-B) is :\n");
for(i=0;i<rangeA;i++)
{
for(j=0;j<rangeB;j++)
{
if(A[i]==B[j])
{
break;
}
}
if(j==rangeB)
{
printf("%d\t",A[i]);
}
}
return 0;
}

OUTPUT:
LAB4: write a program in C two create a set and perform Symmetric Difference
Operations

#include<stdio.h>
int main()
{
int a[20],b[30],c[50],f1=0;
int i,j,n,m,k;
printf("Enter the size of first Set A:\n");
scanf("%d",&n);
printf("\nEnter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the size of second Set B:\n");
scanf("%d",&m);
printf("\nEnter the elements:\n");
for(i=0;i<m;i++)
{
scanf("%d",&b[i]);
}
k=0;
for(i=0;i<n;i++)
{
f1 =0;
for(j=0;j<m;j++)
{
if(a[i]==b[j])
{
f1 =0;
break;
}
else
{
f1 =1;
}
}
if(f1==1)
{
c[k]=a[i];
k++;
}
}
for(i=0;i<m;i++)
{
f1 =0;
for(j=0;j<n;j++)
{
if(b[i]==a[j])
{
f1 =0;
break;
}
else
{
f1=1;
}
if(f1==1)
{
c[k]=b[i];
k++;
}
}
printf("\nSymmetric Difference { (A -B) U (B-A) } is :");
for(i=0;i<k;i++)
{
if(c[i]!=c[i+1])
{
printf("%d\t",c[i]);
}
}
return 0;
}
OUTPUT:
LAB5: write a program in C perform the power set operation on set

#include <stdio.h>
#include<math.h>
void printPowerSet(char *set, int set_size)
{

int pow_set_size =pow(2,set_size);


int counter, j;

for(counter =0;counter<pow_set_size;counter++)
{
for(j=0;j<set_size;j++)
{
if(counter & (1<<j))

printf("%c",set[j]);

}
printf(" ");

}
}

// Driver program to the test powerset


int main()
{
char set[]={ 'a','b','c'};
printf("{}");
printPowerSet(set,3);
return 0;
}

OUTPUT:
LAB 6: Write a program in C to Display the Boolean Truth Table for AND, OR,
NOT.

#include<stdio.h>

void main()
{
int a[2][2],b[2][2],c[2];
int i,j;
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
a[i][j]=(i&&j);
b[i][j]=(i||j);
}
}

for(i=0;i<=1;i++)
{
c[i]=(!i);
}

printf("\nThe Truth Table for AND Gate( && ) is..\n");


printf(" A B : C=A&&B\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf(" %d %d : %d\n",i,j,a[i][j]);
}
}
printf("\nThe Truth Table for OR Gate( || ) is..\n");
printf(" A B : C=A||B\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf(" %d %d : %d\n",i,j,b[i][j]);
}
}
printf("\nThe Truth Table for NOT Gate (!) is..\n");
printf(" A : B = !A\n");
for(i=0;i<=1;i++)
{
printf(" %d : %d\n",i,c[i]);
}
}

OUTPUT:
LAB 7 : Write a C Program to find the Cartesian Product of two sets

#include<stdio.h>
int main()
{
int a[10],b[20],n1,n2;
int i,j;
printf("Enter the size of set A\n");
scanf("%d",&n1);
printf("Enter the element of set A\n");
for(i =0;i<n1;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the size of set B\n");
scanf("%d",&n2);
printf("Enter the elements of set B\n");
for( j=0;j<n2;j++)
{
scanf("%d",&b[j]);
}
// logic for cartesian product.

printf("{ ");
for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
printf("(%d %d)", a[i],b[j]);
}
}
printf("}");
return 0;
}

OUTPUT:
LAB 8: Write a program in C for a minimum cost-spanning tree.

#include <stdio.h>
#include <limits.h>
#define V 5

int minKey(int key[], int mstSet[]) {


int min = INT_MAX, min_index;
int v;
for (v = 0; v < V; v++)
if (mstSet[v] == 0 && key[v] < min)
min = key[v], min_index = v;

return min_index;
}

int printMST(int parent[], int n, int graph[V][V]) {


int i;
printf("Edge Weight\n");
for (i = 1; i < V; i++)
printf("%d - %d %d \n", parent[i], i, graph[i][parent[i]]);
}
void primMST(int graph[V][V]) {
int parent[V];
int key[V], i, v, count; // Key values used to pick minimum weight edge in cut
int mstSet[V];
for (i = 0; i < V; i++)
key[i] = INT_MAX, mstSet[i] = 0;
key[0] = 0; // Make key 0 so that this vertex is picked as first vertex
parent[0] = -1; // First node is always root of MST

// The MST will have V vertices


for (count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);
mstSet[u] = 1;
for (v = 0; v < V; v++)

if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v])


parent[v] = u, key[v] = graph[u][v];
}
printMST(parent, V, graph);
}

int main() {
int graph[V][V] = { { 0, 2, 0, 6, 0 }, { 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 }, { 6, 8, 0, 0, 9 }, { 0, 5, 7, 9, 0 }, };
primMST(graph);

return 0;
}
OUTPUT:
LAB 9: Write a program in C for finding the shortest path in a Graph
#include <stdio.h>
#include <limits.h>
#define V 9
int minDistance(int dist[], int sptSet[]) {
int min = INT_MAX, min_index;
int v;
for (v = 0; v < V; v++)
if (sptSet[v] == 0 && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
void printSolution(int dist[], int n) {
printf("Vertex Distance from Source\n");
int i;
for (i = 0; i < V; i++)
printf("%d \t\t %d\n", i, dist[i]);
}

void dijkstra(int graph[V][V], int src) {


int dist[V];

int sptSet[V]; // sptSet[i] will 1 if vertex i is included in shortest


int i, count, v;
for (i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = 0;

dist[src] = 0;
for (count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet);
sptSet[u] = 1;
for (v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u]
+ graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
printSolution(dist, V);
}
int main() {
int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 0, 10, 0, 2, 0, 0},
{0, 0, 0, 14, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
dijkstra(graph, 0);
return 0;
}
OUTPUT:

You might also like