You are on page 1of 13

JSS Academy of Technical Education Department of Information Technology

Experiment No. 01
AIM : To perform union operation on sets.

DESCRIPTION :

For two given sets A and B , AUB is the set of distinct elements that
belong to set A and B or both .
If A = {1,2,3} and B = {3,4,5} then AUB = {1,2,3,4,5}

PROGRAM:

#include<stdio.h>
int main()
{
int arr1[100] , n1 , arr2[100] , n2 , arr3[100] , i , j , l , temp ;
printf("Enter size of 1st set ");
scanf("%d",&n1);
printf("Enter the 1st set \n");
for(i=0;i<n1;i++)
scanf("%d",&arr1[i]);
printf("Enter size of 2nd set ");
scanf("%d",&n2);
printf("Enter the 2nd set \n");
for(i=0;i<n2;i++)
scanf("%d",&arr2[i]);
int k=0;
for(i=0;i<n1;i++)
{
arr3[k]=arr1[i];
k++;
}
for(i=0;i<n2;i++)
{
arr3[k]=arr2[i];
k++;
}
for(i=0;i<k;i++)
{
for(j=i+1;j<k;j++)
{
if(arr3[i]==arr3[j])
Discrete Structure & Logic Lab (KCS-353) CHANDRA MOHAN SINGH 2100910130039
39
JSS Academy of Technical Education Department of Information Technology
{
for(l=j;l<k;l++)
arr3[l]=arr3[l+1];
k--;
}
}
}
for(i=0;i<k-1;i++)
{
for(j=0;j<k-1-i;j++)
{
if(arr3[j]>arr3[j+1])
{
temp=arr3[j];
arr3[j]=arr3[j+1];
arr3[j+1]=temp;
}
}
}
for(i=0;i<k;i++)
printf("%d\t",arr3[i]);
}

Output :

Discrete Structure & Logic Lab (KCS-353) CHANDRA MOHAN SINGH 2100910130039
39
JSS Academy of Technical Education Department of Information Technology

Experiment No. 02

AIM : To perform the intersection operation on sets

DESCRIPTION :

For two given sets A and B , A ∩ B is the set of distinct elements


that belong to
If A = {2,3,4} and B = {3,4,5} then A ∩ B = {3,4}

Program :
#include<stdio.h>
int main()
{
int arr1[100] , n1 , arr2[100] , n2 , arr3[100] , i , j , l , temp ;
printf("Enter size of 1st set ");
scanf("%d",&n1);
printf("Enter the 1st set \n");
for(i=0;i<n1;i++)
scanf("%d",&arr1[i]);
printf("Enter size of 2nd set ");
scanf("%d",&n2);
printf("Enter the 2nd set \n");
for(i=0;i<n2;i++)
scanf("%d",&arr2[i]);
int k=0;
i=0;
while(i<n1)
{
j=0;
while(j<n2)
{
if(arr1[i]==arr2[j])
{
arr3[k]=arr1[i];
k++;
i++;
j++;
}
else
j++ ;
Discrete Structure & Logic Lab (KCS-353) CHANDRA MOHAN SINGH 2100910130039
39
JSS Academy of Technical Education Department of Information Technology
}
i++;
}
for(i=0;i<k-1;i++)
{
for(j=0;j<k-1-i;j++)
{
if(arr3[j]>arr3[j+1])
{
temp=arr3[j];
arr3[j]=arr3[j+1];
arr3[j+1]=temp;
}
}
}
for(i=0;i<k;i++)
printf("%d\t",arr3[i]);
}

Output :

Discrete Structure & Logic Lab (KCS-353) CHANDRA MOHAN SINGH 2100910130039
39
JSS Academy of Technical Education Department of Information Technology

Experiment No. 03

AIM : To find the difference between the two sets.

DESCRIPTION :

The set that consists of the elements of A which are not elements of
B or vice versa. Difference is A-B or B-A.
A = {1,2,3} B = {2,3,4} A-B = {1} B-A = {4}

Program :

#include<stdio.h>
int main()
{
int s1[100],s2[100],s3[100],n1,n2,i,ch,j,k,c;
printf("Enter size of 1st set ");
scanf("%d",&n1);
printf("Enter the 1st set \n");
for(i=0;i<n1;i++)
scanf("%d",&s1[i]);
printf("Enter size of 2nd set ");
scanf("%d",&n2);
printf("Enter the 2nd set \n");
for(i=0;i<n2;i++)
scanf("%d",&s2[i]);
printf("Enter 1 for (s1-s2) or 2 for (s2-s1) -> ");
scanf("%d",&ch);
switch(ch)
{
case 1:
k=0;
i=0;
while(i<n1)
{
j=0;
c=0;
while(j<n2)
{
if(s1[i]!=s2[j])
c++;
Discrete Structure & Logic Lab (KCS-353) CHANDRA MOHAN SINGH 2100910130039
39
JSS Academy of Technical Education Department of Information Technology
j++;
}
if(c==n2)
{
s3[k]=s1[i];
k++;
}
i++;
}
printf("(s1-s2) is\t");
for(i=0;i<k;i++)
printf("%d\t",s3[i]);
break;

case 2:
k=0;
i=0;
while(i<n2)
{
j=0;
c=0;
while(j<n1)
{
if(s2[i]!=s1[j])
c++;
j++;
}
if(c==n1)
{
s3[k]=s2[i];
k++;
}
i++;
}
printf("(s2-s1) is\t");
for(i=0;i<k;i++)
printf("%d\t",s3[i]);
break ;

default:
printf("Enter a valid choice ");
}
}

Discrete Structure & Logic Lab (KCS-353) CHANDRA MOHAN SINGH 2100910130039
39
JSS Academy of Technical Education Department of Information Technology

Output :

For (B-A)

For(A-B)

Discrete Structure & Logic Lab (KCS-353) CHANDRA MOHAN SINGH 2100910130039
39
JSS Academy of Technical Education Department of Information Technology

Experiment No. 04

AIM : To find the symmetric difference between the two sets.

DESCRIPTION :

Symmetric difference of two sets, also known as the disjunctive


union, is the set of elements which are in either of the sets, but not
in their intersection.
A = {1,2,3} B = {2,3,4} A Δ B = {1,4}

Program :
#include<stdio.h>
int unionset(int arr1[],int arr2[],int n,int m,int unionarr[]){

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


{
unionarr[i]=0;
}
for (int i = 0; i < n; i++)
{
unionarr[i]=arr1[i];
}
for (int i = n, j=0; j < m; i++,j++)
{

unionarr[i]=arr2[j];
}
int c;
int b=n+m;
while(b>0)
{
for (int i = 0; i < n+m; i++)
{
if(unionarr[i]>unionarr[i+1])
{
c=unionarr[i];
unionarr[i]=unionarr[i+1];
unionarr[i+1]=c;
Discrete Structure & Logic Lab (KCS-353) CHANDRA MOHAN SINGH 2100910130039
39
JSS Academy of Technical Education Department of Information Technology
}
}
b--;
}

int lastelement=unionarr[n+m-1];
int repeat=0;
int element=unionarr[0];
int flag=0;
while (element!=lastelement)
{
for (int i = flag; i < n+m; i++)
{
if(unionarr[i]!=unionarr[i+1])
{
// printf("%d,",unionarr[i]);
flag=i+1;
element=unionarr[i+1];
break;
}
}

unionarr[flag+1]=lastelement;
return unionarr;

int intersection(int arr1[],int arr2[],int n,int m,int maxarr[])


{
for (int i = 0; i < n; i++)
{
maxarr[i]=0;
}

int match=0;
int index=0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j< n; j++)
{
if(arr2[i]==arr1[j])
{
maxarr[index]=arr2[i];
index++;
break;
}
}
Discrete Structure & Logic Lab (KCS-353) CHANDRA MOHAN SINGH 2100910130039
39
JSS Academy of Technical Education Department of Information Technology

}
return maxarr;

void difference(int arrA[], int arrB[], int n, int m)


{

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


{
for (int j = 0; j < n; j++)
{

if(arrB[i]==arrA[j] )
{
arrA[j]=0;
}
}

}
for (int i = 0; i < n; i++)
{
if(arrA[i]!=0){

printf("%d ",arrA[i]);
}
}

int main()
{
int n ,m;
printf("Enter the length of first set :- ");
scanf("%d",&n);
printf("Enter the length of second set :- ");
scanf("%d",&m);
int arr1[n],arr2[m];
for (int i = 0; i < n; i++)
{
printf("enter the element of first set (%d) :- ",i+1);
scanf("%d",&arr1[i]);
Discrete Structure & Logic Lab (KCS-353) CHANDRA MOHAN SINGH 2100910130039
39
JSS Academy of Technical Education Department of Information Technology
}
for (int i = 0; i < m; i++)
{
printf("enter the element of second set (%d) :- ",i+1);
scanf("%d",&arr2[i]);
}
int unionarr[n+m];
unionset(arr1,arr2, n, m,unionarr);

if (n>m)
{
int maxarr[n];
intersection(arr1,arr2,n,m,maxarr);
difference( unionarr,maxarr, n+m, n);

}
else{
int maxarr[m];
intersection(arr2,arr1,m,n,maxarr);
difference( unionarr,maxarr, n+m, n);

return 0;
}

Output :

Discrete Structure & Logic Lab (KCS-353) CHANDRA MOHAN SINGH 2100910130039
39
JSS Academy of Technical Education Department of Information Technology

Experiment No. 05

AIM : To find the cartesian product between the two sets.

DESCRIPTION :

Cartesian Product of sets A and B is defined as the set of all


ordered pairs (x, y) such that x belongs to A and y belongs to B.
A = {1,2,3} B = {4,5}
A X B = { ( 1,4),( 1,5),( 2,4),( 2,5),( 3,4),( 3,5) }

Program :

#include<stdio.h>
void cartesianproduct(int arr1[],int arr2[], int n,int m)
{
printf("{");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf("( %d,%d)",arr1[i],arr2[j]);

}
printf("}");

int main()
{
int n ,m;
printf("Enter the length of first set :- ");
scanf("%d",&n);
printf("Enter the length of second set :- ");
scanf("%d",&m);
int arr1[n],arr2[m];
for (int i = 0; i < n; i++)
{
printf("enter the element of first set (%d) :- ",i+1);
scanf("%d",&arr1[i]);

Discrete Structure & Logic Lab (KCS-353) CHANDRA MOHAN SINGH 2100910130039
39
JSS Academy of Technical Education Department of Information Technology
}
for (int i = 0; i < m; i++)
{
printf("enter the element of second set (%d) :- ",i+1);
scanf("%d",&arr2[i]);
}
cartesianproduct(arr1,arr2,n,m);

return 0;
}

Output :

Discrete Structure & Logic Lab (KCS-353) CHANDRA MOHAN SINGH 2100910130039
39

You might also like