You are on page 1of 4

/*Title:To perform set operations using arrays : 1)union 2)intersection

3)Difference 4)Symmetric Differnce


Name:Saharsh Bhatia
Roll no:2655
Batch:H-6
Division: Se-6*/
#include<stdio.h>
#include<conio.h>
//function declaration
void display(int [10],int);
int accdata(int X[10],int);
int uni(int X[10],int Y[10],int Z[20],int x,int y);
int intrsn(int X[10],int Y[10],int Z[20],int x,int y);
int diff(int X[10],int Y[10],int Z[20],int x,int y);
int symdiff(int X[10],int Y[10],int Z[20],int x,int y);
void main()//main function
{
int ch,X[10],Y[10],Z[20],x,y,z,i,ans;
clrscr();
x=accdata(X,1);//accepting the values of sets
y=accdata(Y,2);
//menu
do{
//menu
clrscr();
printf("\n\tMENU::\n\t 1:UNION.\n\t 2:INTERSECTION\n\t 3.D
IFFRENCE\n\t 4.SYMM.DIFF\n");
printf("Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: z=uni(X,Y,Z,x,y);
printf("A={ ");
display(X,x);
printf("B={ ");
display(Y,y);
printf("\n The Union is:\n");
printf("C={ ");
display(Z,z);
break;
case 2: z=intrsn(X,Y,Z,x,y);
printf("A={ ");
display(X,x);
printf("B={ ");
display(Y,y);
printf("\n The intersection is:\n");
printf("C={ ");
display(Z,z);
break;
case 3: z=diff(X,Y,Z,x,y);
printf("A={ ");
display(X,x);
printf("B={ ");
display(Y,y);
printf("\n The Difference is:\n");
printf("C={ ");

display(Z,z);
break;
case 4: z=symdiff(X,Y,Z,x,y);
printf("A={ ");
display(X,x);
printf("B={ ");
display(Y,y);
printf("\n The Symmetric Difference is:\n");
printf("C={ ");
display(Z,z);
break;
}
printf("\n\n\t\tDo u want to continue? (1-yes,2-no)");
scanf("%d",&ans);
}while(ans==1);
getch();
}
int accdata(int X[10],int y)//sets value
{
int x=0,i=0;
printf("\n\nPlz enter size of set%d(maximum10) :",y);
scanf("%d",&x);
printf("\nEnter elements of set %d: \n",y);
for(i=0;i<x;i++)
{
scanf("%d",&X[i]);
if(i>=1&&X[i]==X[i-1])
{
printf("Duplicate no enetered pls eneter another no.");
i--;
}
}
return x;
}
int uni(int X[10],int Y[10],int Z[20],int x,int y)
{//union set
int i,j,z;
for(i=0;i<x;i++)
Z[i]=X[i];
z=x;
for(i=0;i<y;i++)
{
for(j=0;j<z;j++)
{
if(Y[i]==Z[j])
break;
}
if(j==z)
{
Z[z]=Y[i];
z++;
}

}
return z;
}
int intrsn (int X[10],int Y[10],int Z[10],int x,int y)
{//intersection
int i,j,z=0;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
if(X[i]==Y[j])
{
Z[z]=X[i];
z++;
break;
}
else
continue;
}
}
return z;
}
void display(int X[10],int x)
{//for displaying the array in form of sets
int i=0;
for(i=0;i<x;i++)
printf("%d,",X[i]);
printf("\b}\n");
}
int diff(int X[10],int Y[10],int Z[20],int x,int y)
{//for calculating the difference of the function
int i,j,z=0;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
if(X[i]==Y[j])
break;
else if(j==(y-1))
{
Z[z]=X[i];
z++;
break;
}
}
}

return z;
}
int symdiff(int X[10],int Y[10],int Z[20],int x,int y)
{//for symm diff of the function
int Z1[10],Z2[10],p,q,r;
p=uni(X,Y,Z1,x,y);
q=intrsn(X,Y,Z2,x,y);
r=diff(Z1,Z2,Z,p,q);
return r;
}

You might also like