C Program to find the two sets Intersection and Union

Write a C Program to find the two sets Intersection and Union
A Set is a collection of well defined and distinct objects.
Intersection of two sets A and B is defined as, all the elements of set A, which are also elements of set B.Union of two sets A and B is defined as, all the elements of A and B, but not belonged to both.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
void Union(int set1[10],int set2[10],int m,int n);
void Intersection(int set1[10],int set2[10],int m,int n);
void main()
{
int a[10],b[10],m,n,i,j;
int ch;
clrscr();
printf("nEnter the number of elements in first set:n");
scanf("%d",&m);
printf("nEnter the elements:n");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
printf("nElement of First set:nn");
for(i=0;i<m;i++)
{
printf("%dt",a[i]);
}
printf("nEnter the number of elements in second set:n");
scanf("%d",&n);
printf("nEnter the elements:n");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
printf("nElement of second setn");
for(i=0;i<n;i++)
{
printf("%dt",b[i]);
}
for(;;)
{
printf("nnMenunn1.Unionn2.Intersection");
printf("n3.exit");
printf("nEnter your choice:n");
scanf("%d",&ch);
switch(ch) {
case 1:
Union(a,b,m,n);
break;
case 2:
Intersection(a,b,m,n);
break;
case 3:
exit(0);
}
getch();
}
}

void Union(int a[10],int b[10],int m,int n)
{
int c[20],i,j,k=0,flag=0;
for(i=0;i<m;i++)
{
c[k]=a[i];
k++;
}
for(i=0;i<n;i++)
{
flag=0;
for(j=0;j<m;j++)
{
if(b[i]==c[j])
{
flag=1;
break;
}
}
if(flag==0)
{
c[k]=b[i];
k++;
}
}
printf("nElement of resultant setnn");
for(i=0;i<k;i++)
{
printf("t%d",c[i]);
}
}
void Intersection(int a[10],int b[10],int m,int n)
{
int c[20],i,j,k=0,flag=0;
for(i=0;i<m;i++)
{
flag=0;
for(j=0;j<n;j++)
{
if(a[i]==b[j])
{
flag=1;
break;
}
}
if(flag==1)
{
c[k]=a[i];
k++;
}
}
if(k==0)
{
printf("nnResultant set is null set!n");
}else{
printf("nElement of resultant setn");
for(i=0;i<k;i++)
{
printf("t%d",c[i]);
}
}
}


Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C program to create a subsets using backtracking method.

C Program to find the subsets in the set. We use the backtracking method to solve this problem. Backtracking is the refinement method of Brute-Force method. Backtrack method means it finds the number of sub solutions and each may have number of sub divisions, and solution chosen for exactly one. Backtracking method is a recursive method. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
int inc[50],w[50],sum,n;
int promising(int i,int wt,int total)
{
return(((wt+total)>=sum)&&((wt==sum)||(wt+w[i+1]<=sum)));
}
/*
* You can find this program on GitHub
* https://github.com/snadahalli/cprograms/blob/master/subsets.c
*/
void main()
{
int i,j,n,temp,total=0;
clrscr();
printf("n Enter how many numbers:n");
scanf("%d",&n);
printf("n Enter %d numbers to th set:n",n);
for(i=0;i<n;i++)
{
scanf("%d",&w[i]);
total+=w[i];
}
printf("n Input the sum value to create sub set:n");
scanf("%d",&sum);
for(i=0;i<=n;i++)
for(j=0;j<n-1;j++)
if(w[j]>w[j+1])
{
temp=w[j];
w[j]=w[j+1];
w[j+1]=temp;
}
printf("n The given %d numbers in ascending order:n",n);
for(i=0;i<n;i++)
printf("%d t",w[i]);
if((total<sum))
printf("n Subset construction is not possible");
else
{
for(i=0;i<n;i++)
inc[i]=0;
printf("n The solution using backtracking is:n");
sumset(-1,0,total);
}
getch();
}
void sumset(int i,int wt,int total)
{
int j;
if(promising(i,wt,total))
{
if(wt==sum)
{
printf("n{t");
for(j=0;j<=i;j++)
if(inc[j])
printf("%dt",w[j]);
printf("}n");
}
else
{
inc[i+1]=TRUE;
sumset(i+1,wt+w[i+1],total-w[i+1]);
inc[i+1]=FALSE;
sumset(i+1,wt,total-w[i+1]);
}
}
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

–>

(c) www.c-program-example.com