Dstl Lab File
Dstl Lab File
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;
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)
{
for(counter =0;counter<pow_set_size;counter++)
{
for(j=0;j<set_size;j++)
{
if(counter & (1<<j))
printf("%c",set[j]);
}
printf(" ");
}
}
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);
}
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
return min_index;
}
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]);
}
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: