/*
Title: Represent sets using one dimensional arrays and implement functions to perform
  1) union
  2) intersection
  3) difference
  4) symmetric difference of two sets
*/

#include<stdio.h>
#include<stdlib.h>

int accept(int a[])
{
int i,n;
printf("\nEnter no of elements:" );
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
return n;
}

void disp(int a[],int n)
{
int i;
printf("{ ");
for(i=0;i<n;i++)
printf(" %d",a[i]);

if(n==0)
printf(" EMPTY ");
printf(" }");
}

int uni(int a[],int b[],int c[],int n1,int n2)
{
int i,j,n3=0,isfound=0;
for(i=0;i<n1;i++) //To copy all elements of set a into set c
{
c[n3]=a[i];
n3++;
}

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

{
isfound=0;
for(i=0;i<n3;i++)
{
if(b[j]==c[i])

{
isfound=1;
break;
}
}
if(isfound==0)
{
c[n3++]=b[j];
}
}
return n3;
}

int intsect(int a[],int b[],int c[],int n1,int n2)
{
int i,j,n3=0,isfound=0;

for(j=0;j<n2;j++)
{
isfound=0;
for(i=0;i<n1;i++)
{
if(b[j]==a[i])
{
isfound=1;
break;
}
}

if(isfound==1)
c[n3++]=b[j];

}
return n3;
}


int diff(int a[],int b[],int c[],int n1,int n2)
{
int i,j,n3=0,isfound=0;

for(j=0;j<n2;j++)
{
isfound=0;
for(i=0;i<n1;i++)
{
if(b[j]==a[i])
{
isfound=1;
break;
}
}

if(!isfound)
c[n3++]=b[j];
}
return n3;
}


int main()
{
int ch,a[10],b[10],c[10],n1=0,n2=0,n3,k1,k2,c1[10],c2[10];
do
{
system("clear");
printf("\nSET A= ");
disp(a,n1);
printf("\nSET B: ");
disp(b,n2);
printf("\n********MENU************\n");
printf("\n1.INPUT\n2.UNION\n3.INTERSECT\n4.DIFF\n5.SYMM DIFF\n6.EXIT\nENTER UR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter set A: ");
n1=accept(a);
printf("\nEnter set B: ");
n2=accept(b);
break;
case 2:
printf("\nAUB=");
n3=uni(a,b,c,n1,n2);
disp(c,n3);
getchar();
break;
case 3:
printf("\nA@B=");
n3=intsect(a,b,c,n1,n2);
disp(c,n3);
getchar();
break;
case 4:
printf("\nA-B= ");
n3=diff(b,a,c,n2,n1);
disp(c,n3);
printf("\nB-A= ");
n3=diff(a,b,c,n1,n2);
disp(c,n3);
getchar();
break;
case 5:
k1=diff(b,a,c1,n2,n1);
k2=diff(a,b,c2,n1,n2);
n3=uni(c1,c2,c,k1,k2);
printf("\nAsdB:");
disp(c,n3);
getchar();
break;
case 6:
exit(0);
}
getchar();
}while(ch!=5);
return 0;
}


//END OF PROGRAM