How do you use Intersetcion and union between two sets in c
How do you use Intersetcion and union between two sets in c++, just need a little example with code please.
Solution
#include <iostream>
using namespace std;
int Union(int set1[], int set2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n)
{
if (set1[i] < set2[j]) //arrange elements in order from both sets
cout<<\" \"<<set1[i++];
else if (set2[j] < set1[i])
cout<<\" \"<<set2[j++];
else
{
cout<<\" \"<<set2[j++];
i++;
}
}
/* Print remaining elements of the larger array */
while(i < m)
cout<<set1[i++];
while(j < n)
cout<<\" \"<<set2[j++];
}
int Intersection(int set1[], int set2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n)
{
if (set1[i] < set2[j])
i++;
else if (set2[j] < set1[i])
j++;
else /* if arr1[i] == arr2[j] */
{
cout<<\" \"<<set2[j++];
i++;
}
}
}
int main()
{
int set1[] = {1, 2, 4, 5, 6};
int set2[] = {2, 3, 5, 7};
int m = sizeof(set1)/sizeof(set1[0]);
int n = sizeof(set2)/sizeof(set2[0]);
cout<<\"\ Union of sets\";
Union(set1, set2, m, n);
cout<<\"\ Intersection of sets\";
Intersection(set1, set2 ,m,n);
return 0;
}
output:

