/*--------------------------------------------------------------------------------------------------
Program : Write
a program to perform Set operations :-
1. Union
2. Intersection
3. Difference
4. Symmetric Difference
Created By : JABIR DAUD PATHAN
Branch : COMPUTER ENGINEERING
--------------------------------------------------------------------------------------------------*/
/*--------------------<< Header File
>>---------------------*/
#include<conio.h>
#include<stdio.h>
#define size 10
/*-----------<< Global
Declaration of Functions
>>-----------*/
int accept(int []);
void display(int [],int);
void uni(int [],int [],int,int);
void intersection(int [],int
[],int,int);
void difference(int [],int
[],int,int);
void symmetricdiff(int [],int
[],int,int);
/*----<< function defination
for accept set elements >>------*/
int accept(int arr[size])
{
int i,no;
printf("\n\n\nEnter the limit of Set==>");
scanf("%d",&no);
printf("\nEnter the Element in Set==>\n");
for(i=0;i<no;i++)
{
scanf("%d",&arr[i]); //accept set elements from user
}
return no;
}
/*----<< function defination
for display set elements >>------*/
void display(int arr[size],int no)
{
int i;
printf("\n\nYour Entered Set is==>");
printf("\t{");
for(i=0;i<no;i++)
{
printf(" %d ",arr[i]); //display set elements
}
printf("}");
}
/*----<< function defination
for union of set >>------*/
void uni(int arr1[size],int
arr2[size],int no1,int no2)
{
int i=0,j=0,k=0,arr3[size],flag=0,temp;
for(i=0;i<no1;i++)
{
arr3[k++]=arr1[i];
}
for(i=0;i<no2;i++)
{
flag=0;
for(j=0;j<no1;j++)
{
if(arr2[i]==arr1[j])
{
flag=1;
}
}
if(flag==0)
{
arr3[k++]=arr2[i];
}
}
/* using bubble sort technique */
for(i=1;i<k;i++)
{
for(j=0;j<k-i;j++)
{
if(arr3[j]>arr3[j+1]) //condition to
find out smallest number
{
temp=arr3[j]; // we
arr3[j]=arr3[j+1]; // swapping
arr3[j+1]=temp; // elements
}
}
}
printf("\n\nThe Result of Union is");
display(arr3,k); //function call
}
/*----<< function defination
for intersection of set >>------*/
void intersection(int
arr1[size],int arr2[size],int no1,int no2)
{
int i=0,j=0,k=0,arr3[size];
for(i=0;i<no1;i++) //traverse
1st set element
{
for(j=0;j<no2;j++) //traverse 2nd set
element
{
if(arr1[i]==arr2[j]) //check arr1 is equal
to arr2
{
arr3[k]=arr1[i]; //assign arr1 to arr3
k++;
}
}
}
printf("\n\nThe Result of Intersection is==>");
display(arr3,k);
}
/*----<< function defination
for difference of set >>------*/
void difference(int arr1[size],int
arr2[size],int no1,int no2)
{
int i=0,j=0,k=0,arr3[size],flag=0;
for(i=0;i<no1;i++) //traverse
1st set element
{
for(j=0;j<no2;j++) //traverse 1st set
element
{
if(arr1[i]==arr2[j])
{
flag=0;
break;
}
else
{
flag=1;
}
}
if(flag==1)
{
arr3[k]=arr1[i]; //assign arr1 to arr3
k++;
}
}
printf("\n\nThe Result of difference is==>");
display(arr3,k);
}
/*----<< function defination
for symmetric difference of set >>------*/
void symmetricdiff(int
arr1[size],int arr2[size],int no1,int no2)
{
int i=0,j=0,k=0,arr3[size],flag=0;
for(i=0;i<no1;i++)
{
for(j=0;j<no2;j++)
{
if(arr1[i]==arr2[j])
{
flag=0;
break;
}
else
{
flag=1;
}
}
if(flag==1)
{
arr3[k]=arr1[i];
k++;
}
}
for(i=0;i<no2;i++)
{
for(j=0;j<no1;j++)
{
if(arr2[i]==arr1[j])
{
flag=0;
break;
}
else
{
flag=1;
}
}
if(flag==1)
{
arr3[k]=arr2[i];
k++;
}
}
printf("\n\nThe Result of symmetric Difference is==>");
display(arr3,k);
}
/*-----------<< Main Function Starts >>------------*/
void main()
{
int
i,j,ch,no1,no2,arr1[size],arr2[size],arr3[size];//variable declaration
char ans;
do
{
clrscr();
printf("\n\t\t\t-----------<<
MENU >>------------");
printf("\n\n\t\t\t1.Union of Two Sets.");
printf("\n\t\t\t2.Intersection of Two Sets.");
printf("\n\t\t\t3.Difference of Two Sets.");
printf("\n\t\t\t4.Symmetric Difference of Two Sets.");
printf("\n\t\t\t5.Exit.");
printf("\n\n\t\t\t------------------------------------");
printf("\n\n\t\t\tEnter your choice==>");
scanf("%d",&ch);//accept choice from user
switch(ch)
{
case 1:
no1=accept(arr1); //accept 1st set element
display(arr1,no1); //display 1st set
element
no2=accept(arr2); //accept 2nd set element
display(arr2,no2); //display 2nd set
element
uni(arr1,arr2,no1,no2); //function call for
union
break;
case 2:
no1=accept(arr1);
display(arr1,no1);
no2=accept(arr2);
display(arr2,no2);
intersection(arr1,arr2,no1,no2); //function
call for intersection
break;
case 3:
no1=accept(arr1);
display(arr1,no1);
no2=accept(arr2);
display(arr2,no2);
difference(arr1,arr2,no1,no2); //function call for difference
break;
case 4:
no1=accept(arr1);
display(arr1,no1);
no2=accept(arr2);
display(arr2,no2);
symmetricdiff(arr1,arr2,no1,no2);//function
call for symmetric difference
break;
case 5:
exit(0);
break;
}
printf("\n\n\tDo you want to Continue?(y/n)==>");
ans=getch();
}while(ans=='Y'||ans=='y');
getch();
}
/*----------<< End Of Main Function >>-----------*/
/*---------------<< OUTPUT
SCREEN >>----------------*/
-----------<< MENU
>>------------
1.Union of Two Sets.
2.Intersection of Two Sets.
3.Difference of Two
Sets.
4.Symmetric Difference
of Two Sets.
5.Exit.
------------------------------------
Enter your choice==>1
Enter the limit of Set==>4
Enter the Element in Set==>
1
6
4
2
Your Entered Set is==> { 1
6 4 2 }
Enter the limit of Set==>3
Enter the Element in Set==>
5
3
6
Your Entered Set is==> { 5
3 6 }
The Result of Union is
Your Entered Set is==> { 1
2 3 4 5 6 }
Do you want to Continue?(y/n)==>y
-----------<< MENU
>>------------
1.Union of Two Sets.
2.Intersection of Two
Sets.
3.Difference of Two
Sets.
4.Symmetric Difference
of Two Sets.
5.Exit.
------------------------------------
Enter your
choice==>2
Enter the limit of Set==>4
Enter the Element in Set==>
1
2
3
4
Your Entered Set is==> { 1
2 3 4 }
Enter the limit of Set==>4
Enter the Element in Set==>
3
4
5
6
Your Entered Set is==> { 3
4 5 6 }
The Result of Intersection is==>
Your Entered Set is==> { 3 4
}
Do you want to Continue?(y/n)==>y
-----------<< MENU
>>------------
1.Union of Two Sets.
2.Intersection of Two
Sets.
3.Difference of Two
Sets.
4.Symmetric Difference
of Two Sets.
5.Exit.
------------------------------------
Enter your
choice==>3
Enter the limit of Set==>5
Enter the Element in Set==>
1
2
3
4
5
Your Entered Set is==> { 1
2 3 4 5 }
Enter the limit of Set==>4
Enter the Element in Set==>
3
4
5
6
Your Entered Set is==> { 3
4 5 6 }
The Result of difference is==>
Your Entered Set is==> { 1 2
}
Do you want to Continue?(y/n)==>y
-----------<< MENU
>>------------
1.Union of Two Sets.
2.Intersection of Two
Sets.
3.Difference of Two
Sets.
4.Symmetric Difference
of Two Sets.
5.Exit.
------------------------------------
Enter your
choice==>4
Enter the limit of Set==>5
Enter the Element in Set==>
1
2
3
4
5
Your Entered Set is==> { 1
2 3 4 5 }
Enter the limit of Set==>5
Enter the Element in Set==>
3
4
5
6
7
Your Entered Set is==> { 3
4 5 6 7 }
The Result of symmetric Difference
is==>
Your Entered Set is==> { 1
2 6 7 }
Do you want to Continue?(y/n)==>y
-----------<< MENU >>------------
1.Union of Two Sets.
2.Intersection of Two
Sets.
3.Difference of Two
Sets.
4.Symmetric Difference
of Two Sets.
5.Exit.
------------------------------------
Enter your
choice==>5
what a brillient c program?
ReplyDeletewhat are no1 and no2 in union function ?
ReplyDelete