0% found this document useful (0 votes)
5 views19 pages

DSL Lab KCS353

The document contains a lab manual for an engineering course with various C programming exercises focused on set operations, including union, intersection, difference, power set, Boolean truth tables, Cartesian products, minimum cost spanning trees, and relations. Each exercise includes code snippets, expected outputs, and explanations of the algorithms used. The manual serves as a practical guide for students to understand and implement fundamental concepts in set theory and programming.

Uploaded by

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

DSL Lab KCS353

The document contains a lab manual for an engineering course with various C programming exercises focused on set operations, including union, intersection, difference, power set, Boolean truth tables, Cartesian products, minimum cost spanning trees, and relations. Each exercise includes code snippets, expected outputs, and explanations of the algorithms used. The manual serves as a practical guide for students to understand and implement fundamental concepts in set theory and programming.

Uploaded by

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

ITS ENGINEERING COLLEGE, GR.

NOIDA
DSL LAB (Lab Manual)
KCS 353
E1. Program in C for Set Union operation.

#include<stdio.h>

#include<conio.h>

int main()

int a[10],b[10],m,n,i,j;

int c[20],k=0,flag=0;

int ch;

printf("Enter the number of elements in first set:\n");

scanf("%d",&m);

printf("Enter the elements:\n");

for(i=0;i<m;i++)

scanf("%d",&a[i]);

printf("\nElement of First set:\n");

for(i=0;i<m;i++)

printf("%d\t",a[i]);

printf("\nEnter the number of elements in second set:\n");

scanf("%d",&n);

printf("Enter the elements:\n");


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

scanf("%d",&b[i]);

printf("\nElement of Second set:\n");

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

printf("%d\t",b[i]);

for(i=0;i<m;i++)

c[k]=a[i];

k++;

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

flag=0;

for(j=0;j<m;j++)

if(b[i]==c[j])

flag=1;

break;
}

if(flag==0)

c[k]=b[i];

k++;

printf("\nElement of resultant set\n");

for(i=0;i<k;i++)

printf("%d\t",c[i]);

OUTPUT
Enter the number of elements in first set:
5
Enter the elements:
1
2
3
4
5
Element of First set:
1 2 3 4 5
Enter the number of elements in second set:
5
Enter the elements:
6
7
8
5
4
Element of Second set:
6 7 8 5 4
Element of resultant set
1 2 3 4 5 6 7 8

E2. Program in C for Set Intersection operation.


#include <stdio.h>
int main()
{
int a[10], b[10], flag = 0, n1, n2, i, j;
printf("Enter set1 size : ");
scanf("%d",&n1);
printf("\nEnter set2 size : ");
scanf("%d",&n2);
printf("\nEnter set1 element : ");
for(i = 0;i < n1;i++)
scanf("%d",&a[i]);
printf("\nEnter set2 element : ");
for(i = 0;i < n2;i++)
scanf("%d",&b[i]);
printf("Intersection: ");
for(i = 0;i < n1;i++)
{
for(j = 0;j < n2;j++)
{
if(b[i] == a[j])
{
flag = 1;
}
}
if(flag == 1)
{
printf("%d ", b[i]);
}
flag = 0;
}
return 0;
}

OUTPUT

Enter set1 size : 5


Enter set2 size : 3
Enter set1 element : 1 2 3 4 5
Enter set2 element : 1 2 6
Intersection: 1, 2

E3. Program in C for Set Difference operation.


#include<stdio.h>
#define max 100

int ifexists(int z[], int u, int v)


{
int i;
if (u==0) return 0;
for (i=0; i<=u;i++)
if (z[i]==v) return (1);
return (0);
}

void main()
{
int p[max], q[max], r[max];
int m,n;
int i,j,k;
printf("Enter length of first array:");
scanf("%d",&m);
printf("Enter %d elements of first array\n",m);
for(i=0;i<m;i++ )
scanf("%d",&p[i]);
printf("\nEnter length of second array:");
scanf("%d",&n);
printf("Enter %d elements of second array\n",n);
for(i=0;i<n;i++ ) scanf("%d",&q[i]);
k=0;
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
if (p[i]==q[j])

{ break;

}
}
if(j==n)
{
if(!ifexists(r,k,p[i]))
{
r[k]=p[i];
k++;
}
}
}
printf("\nThe difference of the two array is:\n");
for(i = 0;i<k;i++)
printf("%d\n",r[i]);
}
OUTPUT
Enter length of first array:4
Enter 4 elements of first array
1
2
3
4
Enter length of second array:4
Enter 4 elements of second array
2
4
5
6
The difference of the two array is:
1
3

E4. Program in C for Power set operation on a set.


#include <stdio.h>
#include <math.h>

voidprintPowerSet(char*set, intset_size)
{
/*set_size of power set of a set with set_size
n is (2**n -1)*/
unsigned intpow_set_size = pow(2, set_size);
intcounter, j;
/*Run from counter 000..0 to 111..1*/
for(counter = 0; counter < pow_set_size; counter++)
{
for(j = 0; j < set_size; j++)
{
/* Check if jth bit in the counter is set
If set then pront jth element from set */
if(counter & (1<<j))
printf("%c", set[j]);
}
printf("n");
}
}
/*Driver program to test printPowerSet*/
intmain()
{
charset[] = {'a','b','c'};
printPowerSet(set, 3);
getchar();
return0;}
E5. Program in C for 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]);
}
}
E6. C prog to find Cartesian product of two sets.
#include<stdio.h>
#include<conio.h>
int main()
{
int a[50],b[50],c[50],i,s1,s2,j,k;
printf("Enter how many elements in set 1\n");
scanf("%d",&s1);
printf("Enter how many elements in set 2\n");
scanf("%d",&s2);
printf("Enter elements of set 1\n");
for(i=0;i<s1;i++)
{
scanf("%d",&a[i]);
}
printf("Enter elements of set 2\n");
for(i=0;i<s2;i++)
{
scanf("%d",&b[i]);
}
printf("cartessian product=");
printf("{");
for(i=0;i<s1;i++)
{
for(j=0;j<s2;j++)
{
printf("(%d,%d)",a[i],b[j]);
printf(",");
}
}
printf("}");}
E7. Minimum cost spanning tree
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]= {
0
}
,min,mincost=0,cost[10][10];
void main() {
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the adjacency matrix:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++) {
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n");
while(ne<n) {
for (i=1,min=999;i<=n;i++)
for (j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0) {
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0) {
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}
E8. Reflexive, symmetric and transitive relation.

#include<stdio.h>
#include<conio.h>
void main(){
int s[20],r[20][2],i,j,k,n,m,f=0,g=0,x,y,z=99;
clrscr();
printf("enter the no. of elements in a set:");
scanf("%d",&n);
printf("enter the elements of set:");
for(i=1;i<=n;i++)
scanf("%d",&s[i]);
printf("enter the no. of relation set:");
scanf("%d",&m);
printf("enter the pairs of relation set:");
for(i=1;i<=m;i++)
scanf("%d%d",&r[i][1],&r[i][2]);
printf("The entered relation is:");
printf("{");
for(i=1;i<=m;i++)
printf("(%d,%d),",r[i][1],r[i][2]);
printf("\b}");
//to check currect relation set elements look below
for(i=1;i<=m;i++){ //m is no of relation elemets
for(j=1;j<=n;j++){ //n is no of set elemets
if(r[i][1]==s[j])
f++;
if(r[i][2]==s[j]) // r[80][2] is metrix to store relation set
elements
g++;
} //end of for loop of j
if(f==0||g==0){
printf("\nYou entered wrong relation set.");
getch();
exit(0);
}
f=0;
g=0;
} //end of for loop of i
//intially f=0 and g=0;
//to check either it is refelexive or not
f=0;
for(i=1;i<=m;i++){
if(r[i][1]==r[i][2])
f++;
}
if(f==n)
printf("\nThe enter set relation is refelexive.");
//to check either it is symmetric or not
f=0;g=0;
for(i=1;i<=m;i++){
x=r[i][1];
y=r[i][2];
g++;
for(j=1;j<=m;j++){
if(x==r[j][2]&&y==r[j][1])
f++;
}
if(f==0)
goto d;
f=0;
}
if(g==m)
printf("\nThe relation is symmetric.");
//to check either it is transitive or not
d:
f=0;g=0;
for(i=1;i<=m;i++){
for(j=1;j<=m;j++){
for(k=1;k<=m;k++){
x=r[i][1];y=r[i][2];
if(y==r[j][1])
z=r[j][2];
if(x==r[k][1]&&z==r[k][2])
f++;
}}
if(f==0)
goto h;
f=0;
}
printf("\nThe relation is transitive.");
h:
getch();
}
E9. Recursive function (sum of natural number)

#include <stdio.h>
int sum(int n);

int main() {
int number, result;

printf("Enter a positive integer: ");


scanf("%d", &number);

result = sum(number);

printf("sum = %d", result);


return 0;
}

int sum(int n) {
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}

You might also like