C Program of Set Operations in Maths

# C Program of Set Operations in Maths

20222
3
SHARE

C program of set operations in Maths : A set in is a collection of well defined and distinct objects, considered as an object in its own right. In the below program, i have represented a set using an  array of integers(for sake of simplicity, concept can be extended to array of characters too). The below code that i have written is a Menu driven switch case based program of most of the basic operations in sets. Previously in my Menu driven program of Temperature conversion i have shown how to repeat the logic using Goto labels, this time i will be showing it using do while looping method.

Set Operations that are being covered in C program :

1. Create two sets.

2. Union of two sets : Contains all distinct elements of sets A and B.

3. Intersection of two sets : Contains all distinct common elements of sets A and B.

4. Difference of two sets : Contains all elements of Set A that are not present in Set B.

5. Symmetric set of two sets : Union of A diff B and B diff A.

Now let us see how to write a Switch case Menu driven which takes choices as input from user and performs functions corresponding to choice selected.

### C program of Set Operations in Maths :

```#include<stdio.h>
#include<conio.h>
#define MAX 30
void create(int set[]);
void print(int set[]);
void Union(int set1[],int set2[],int set3[]);
void intersection(int set1[],int set2[],int set4[]);
void difference(int set1[],int set2[],int set5[]);
void symmetric(int set1[],int set2[],int set6[]);
int member(int set[],int x);

int main()
{ int set1[MAX],set2[MAX],set3[MAX];
int x,op;
set1[0]=set2[0]=set3[0]=0;
printf("-------------------------------------------------------------\n");
printf("----------------made by C code champ ------------------------\n");
printf("-------------------------------------------------------------\n");
printf("\n\n\tMENU BASED C PROGRAM OF SET OPERATIONS \n");
do
{
printf("\n1)Create two sets.");
printf("\n2)Print the set.");
printf("\n3)Union of two sets.");
printf("\n4)Intersection of two sets A intesection B.");
printf("\n5)Difference between two sets A - B");
printf("\n6)Symmetrec Difference between two sets i.e (A-B)U(B-A).");
printf("\n7)Exit from program\n\n");
scanf("%d",&op);
printf("\n\n");
switch(op)
{
case 1: printf("\nCreate Set A: ");
create(set1);
printf("\nCreate Set B: ");
create(set2);
break;
case 2: printf("\nSet A:\n");
print(set1);
printf("\n\nSet B:\n");
print(set2);
break;
case 3: Union(set1,set2,set3);
print(set3);
break;
case 4: intersection(set1,set2,set3);
print(set3);
break;
case 5: difference(set1,set2,set3);
print(set3);
break;
case 6: symmetric(set1,set2,set3);
print(set3);
break;
case 7: break;
default:
break;
}
printf("\n\nPress a key to continue......\n\n");
getch();
}while(op!=7);
}

void create(int set[])
{   int n,i,x;
set[0]=0;
printf("\nNo. of elements in the set : ");
scanf("%d",&n);
printf("\nEnter set elements : \n");
for(i=1;i<=n;i++)
scanf("%d",&set[i]);
set[0]=n;

}

void  print(int set[])
{
int i,n;
n=set[0];
printf("\nMembers of the set :--> ");
for(i=1;i<=n;i++)
printf("%d  ",set[i]);
}

void Union(int set1[],int set2[],int set3[])
{ int i,n;
set3[0]=0;
n=set1[0];
for(i=0;i<=n;i++)
set3[i]=set1[i];

n=set2[0];
for(i=1;i<=n;i++)
if(!member(set3,set2[i]))
set3[++set3[0]]=set2[i];
}

int member(int set[],int x)
{
int i,n;
n=set[0];
for(i=1;i<=n;i++)
if(x==set[i])
return(1);
return(0);
}

void intersection(int set1[],int set2[],int set3[])
{
int i,n;
set3[0]=0;
n=set1[0];
for(i=1;i<=n;i++)
if(member(set2,set1[i]))
set3[++set3[0]]=set1[i];
}

void difference(int set1[],int set2[],int set3[])
{
int i,n;
n=set1[0];
set3[0]=0;
for(i=1;i<=n;i++)
if(!member(set2,set1[i]))
set3[++set3[0]]=set1[i];
}

void symmetric(int set1[],int set2[],int set3[])
{
int i,n;
n=set1[0];
set3[0]=0;
//Calculate set1-set2
for(i=1;i<=n;i++)
if(!member(set2,set1[i]))
set3[++set3[0]]=set1[i];
//Calculate set2-set1
n=set2[0];
for(i=1;i<=n;i++)
if(!member(set1,set2[i]))
set3[++set3[0]]=set2[i];

}```

We hope you all have enjoyed the Menu driven . If you have any issues or concerns related to code ask me in form of comments.