DSL Lab KCS353
DSL Lab KCS353
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;
scanf("%d",&m);
for(i=0;i<m;i++)
scanf("%d",&a[i]);
for(i=0;i<m;i++)
printf("%d\t",a[i]);
scanf("%d",&n);
scanf("%d",&b[i]);
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++;
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
OUTPUT
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
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);
}
#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;
result = sum(number);
int sum(int n) {
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}