Represent set using a one-dimensional array and implement Union, Intersection, Difference, Symmetric difference of two sets.
x
#include<stdio.h>
int main()
{
int
set1[10],set2[10],uni[10],i,j,k,z,flag=1;
int
lset1,lset2;
int
sd[10],inter[10],diff[10];
int g;
setbuf(stdout,
NULL);
printf("Enter
the size of first array:");
scanf("%d",&lset1);
for(i=1;i<=lset1;i++)
{
printf("Enter
the element :");
scanf("%d",&set1[i]);
}
printf("\nEnter
the size of second array:");
scanf("%d",&lset2);
for(j=1;j<=lset2;j++)
{
printf("Enter
the element:");
scanf("%d",&set2[j]);
}
do
{
printf("Please
select your choice from the following\n 1) Union\n 2) Intersection\n 3)
Difference\n 4) Symmetric Difference");
printf("\nEnter
your choice:");
scanf("%d",&z);
switch(z)
{
case 1:
k=1;
for(i=1;i<=lset1;i++)
{
uni[k]=set1[i];
k++;
}
for(j=1;j<=lset2;j++)
{
flag=1;
for(i=1;i<=lset1;i++)
{
if(set2[j]==set1[i])
{
flag=0;
break;
}
}
if(flag==1)
{
uni[k]=set2[j];
k++;
}
}
printf("Union
is: ");
for(i=1;i<k;i++)
{
printf("
%d ",uni[i]);
}
break;
case 2:
k=1;
for(i=1;i<=lset1;i++)
{
for(j=1;j<=lset2;j++)
{
if(set1[i]==set2[j])
{
inter[k]=set1[i];
k++;
}
}
}
printf("Intersection
is: ");
for(i=1;i<k;i++)
{
printf("
%d",inter[i]);
}
break;
case 3:
k=1;
for(i=1;i<=lset1;i++)
{
flag=1;
for(j=1;j<=lset2;j++)
{
if(set1[i]==set2[j])
{
flag=0;
break;
}
}
if(flag==1)
{
diff[k]=set1[i];
k++;
}
}
printf("Difference
is:");
for(i=1;i<k;i++)
{
printf("
%d",diff[i]);
}
break;
case 4:
k=1;
for(i=1;i<=lset1;i++)
{
flag=1;
for(j=1;j<=lset2;j++)
{
if(set1[i]==set2[j])
{
flag=0;
break;
}
}
if(flag==1)
{
sd[k]=set1[i];
k++;
}
}
for(j=1;j<=lset2;j++)
{
flag=1;
for(i=1;i<=lset1;i++)
{
if(set2[j]==set1[i])
{
flag=0;
break;
}
}
if(flag==1)
{
sd[k]=set2[j];
k++;
}
}
printf("Symmetric
difference is:");
for(i=1;i<k;i++)
{
printf("
%d",sd[i]);
}
break;
default:
printf("\n\nInvalid
choice\ntry again later");
}
printf("\nDo
you want to continue?Enter 1 for Yes or enter 0 for No : ");
scanf("%d",&g);
}while(g==1);
}
Output
Enter
the size of first array:3
Enter
the element :1
Enter
the element :2
Enter
the element :3
Enter
the size of second array:3
Enter
the element:3
Enter
the element:4
Enter
the element:5
Please
select your choice from the following
1) Union
2) Intersection
3) Difference
4) Symmetric Difference
Enter
your choice:1
Union
is: 1
2 3 4 5
Do you
want to continue?Enter 1 for Yes or enter 0 for No : 1
Please
select your choice from the following
1) Union
2) Intersection
3) Difference
4) Symmetric Difference
Enter
your choice:2
Intersection
is: 3
Do you
want to continue?Enter 1 for Yes or enter 0 for No : 1
Please
select your choice from the following
1) Union
2) Intersection
3) Difference
4) Symmetric Difference
Enter
your choice:3
Difference
is: 1 2
Do you
want to continue?Enter 1 for Yes or enter 0 for No : 1
Please
select your choice from the following
1) Union
2) Intersection
3) Difference
4) Symmetric Difference
Enter
your choice:4
Symmetric
difference is: 1 2 4 5
Do you want to continue?Enter 1 for
Yes or enter 0 for No :
No comments:
Post a Comment