/**Program to implement set operations using arrays
and perform such as union,intersection
difference,symmetric difference **/
#include<stdio.h>
//main()
int main(void)
{
int iSetA[10]={0},iSetB[10]={0};
int iResultAB[20]={0},iResultBA[20]={0};//array declaration
int iChoice = 0,iSetAno = 0,iSetBno=0;
int no=0,iResultno1=0,iResultno2=0;//variable declaration
//function declarations
void accept(int[],int);
void display(int[],int);
void union_arr(int[],int,int[],int);
void intersection(int[],int ,int[],int);
int diff_AB(int[],int,int[],int,int[],int);
int diff_BA(int[],int,int[],int,int[],int);
void sym_diff(int[],int,int[],int,int[],int);
clrscr();
do
{
printf("*********MENU********");
printf("\nThe operations that can be performed are:\n");
printf("\t1.Accept\n\t2.Display\n\t3:Union\n\t4.Intersection\n");
printf("\t5.Diff(array1-array2)\n\t6.Diff(array2-array1)\n");
printf("\t7.Symmetric difference\n");
printf("Enter your choice\n");
scanf("%d",&iChoice);
switch(iChoice)
{
case 1:
//To accept set values
printf("\nEnter cardinality of array1\n");
scanf("%d",&iSetAno); //to accept size of array1
accept(iSetA,iSetAno); //call to accept()
printf("\nEnter cardinality of array1\n");
scanf("%d",&iSetBno); //to accept size of array2
accept(iSetB,iSetBno); //call to accept()
break;
case 2:
//To display set values
display(iSetA,iSetAno);
display(iSetB,iSetBno);
break;
case 3:
//To perform union operation
union_arr(iSetA,iSetAno,iSetB,iSetBno);
break;
case 4:
//To perform intersection operation
intersection(iSetA,iSetAno,iSetB,iSetBno);
break;
case 5:
//to perform difference(A-B)
iResultno1=diff_AB(iSetA,iSetAno,iSetB,iSetBno,iResultAB,iResultno1);
display(iResultAB,iResultno1);
break;
case 6:
//to perform difference(B-A)
iResultno2=diff_BA(iSetA,iSetAno,iSetB,iSetBno,iResultBA,iResultno2);
display(iResultBA,iResultno2);
break;
case 7:
//To perform symmetric difference
sym_diff(iSetA,iSetAno,iSetB,iSetBno,iResultAB,no);
break;
default:
printf("Choice not present");
}
}while(iChoice>0&&iChoice<=7);
getch();
return 0;
}
//To accept array values
void accept(int arr[],int no)
{
int i=0;
printf("\nEnter %d elements of array 1 ",no);
for(i=0;i<no;i++)
{
scanf("%d",&arr[i]);
}
}
//To display array values
void display(int arr[],int no)
{
int i = 0;
printf("\nElements of set = { ");
if(no==0)
{
printf("No elements to display");
}
for(i=0;i<no;i++)
{
printf("%d,",arr[i]);
}
printf("}\n");
}
//To perform union operation on arrays
void union_arr(int arr1[],int no1,int arr2[],int no2)
{
int i =0,j=0,k = 0,flag = 0,result[20]={0};
for(i=0;i<no1;i++)
{
result[k]=arr1[i];k++;
}
for(j=0;j<no2;j++)
{
flag=0;
for(i=0;i<no1;i++)
{
if(arr2[j]==arr1[i])
{
flag++;
break;
}
}
if(flag==0)
{
result[k]=arr2[j]; k++;
}
}
display(result,k);
}
//To perform intersection operation on arrays
void intersection(int arr1[],int no1,int arr2[],int no2)
{
int i =0,j=0,k = 0,flag = 0,result[20]={0};
for(j=0;j<no2;j++)
{
flag=0;
for(i=0;i<no1;i++)
{
if(arr2[j]==arr1[i])
{
flag++;
break;
}
}
if(flag==1)
{
result[k]=arr2[j]; k++;
}
}
display(result,k);
}
//To perform difference(B-A) operation on arrays
int diff_BA(int arr1[],int no1,int arr2[],int no2,int result[],int k)
{
int i =0,j=0,flag = 0;
for(j=0;j<no2;j++)
{
flag=0;
for(i=0;i<no1;i++)
{
if(arr2[j]==arr1[i])
{
flag++;
break;
}
}
if(flag==0)
{
result[k]=arr2[j];
k++;
}
}
return k;
}
//To perform difference(A-B) operation on arrays
int diff_AB(int arr1[],int no1,int arr2[],int no2,int result[],int k)
{
int i =0,j=0,flag = 0;
for(j=0;j<no1;j++)
{
flag=0;
for(i=0;i<no2;i++)
{
if(arr1[j]==arr2[i])
{
flag++;
break;
}
}
if(flag==0)
{
result[k]=arr1[j];
k++;
}
}
return k;
}
//To perform symmetric difference[(A-B)U(B-A)] operation on arrays
void sym_diff(int arr1[],int no1,int arr2[],int no2,int result[],int k)
{
int i=0,j=0,c[20]={0},d[20]={0};
k = diff_AB(arr1,no1,arr2,no2,result,k);
for(i=0;i<k;i++)
c[i]=result[i];
k = diff_BA(arr1,no1,arr2,no2,result,k);
for(j=0;j<k;j++)
d[j]=result[j];
union_arr(c,i,d,j);
}
awesome thankz gagan
ReplyDelete