Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

From Data Structures and Algorithm Analysis C++ 4th Edition: 5. Using the Standa

ID: 3805958 • Letter: F

Question

From Data Structures and Algorithm Analysis C++ 4th Edition:

5. Using the Standard Template Library set class, write C++ functions that does the following:

a) Returns the union of two sets as a new STL set. For simplicity, let us assume we have a set of integers. If one set has the members {1,3,4} and another set has the members {1, 2, 4}, you need to create and return a new set with the members {1,2,3,4}.

b) Returns the intersection of two sets as a new STL set. If one set has the members {1,3,4} and another set has the members {1, 2, 4}, you need to create and return a new set with the members {1,2,3,4}.

Explanation / Answer

Answer of a:- Returns the union of two sets:-

#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<math.h>

/* Function prints union of arr1[] and arr2[]
   m is the number of elements in arr1[]
   n is the number of elements in arr2[] */
int printUnion(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n)
{
    if (arr1[i] < arr2[j])
cout<<arr1[i++];
   
    else if (arr2[j] < arr1[i])
cout<<arr2[j++];
  
    else
    {
cout<<arr2[j++];
  
      i++;
    }
}

/* Print remaining elements of the larger array */
while(i < m)
cout<< arr1[i++];

while(j < n)
cout<< arr2[j++];

}

/* Driver program to test above function */
int main()
{ int size,size1;
int arr1[100],arr2[100];
cout<<"please input the size of array of first set";
cin>>size;
cout<<"please input the element of first set";
for(int i=0;i<size;i++)
{
cin>arr1[i];
}
cout<<"please input the size of array of second set";
cin>>size;
cout<<"please input the element of second set";
for(int i=0;i<size;i++)
{
cin>arr2[i];
}
int m = sizeof(arr1)/sizeof(arr1[0]);
int n = sizeof(arr2)/sizeof(arr2[0]);
printUnion(arr1, arr2, m, n);
getchar();
return 0;
}

Answer of b:- Returns the intersection of two sets:-

#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<math.h>


/* Function prints union of arr1[] and arr2[]
   m is the number of elements in arr1[]
   n is the number of elements in arr2[] */

int printIntersection(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n)
{
    if (arr1[i] < arr2[j])
      i++;
    else if (arr2[j] < arr1[i])
      j++;
    else /* if arr1[i] == arr2[j] */
    {
cout<<arr2[j++];
    
      i++;
    }
}
}/* Driver program to test above function */

int main()

{
int size,size1;

int arr1[100],arr2[100];

cout<<"please input the size of array of first set";

cin>>size;

cout<<"please input the element of first set";

for(int i=0;i<size;i++)

{

cin>arr1[i];

}

cout<<"please input the size of array of second set";

cin>>size;

cout<<"please input the element of second set";

for(int i=0;i<size1;i++)

{

cin>arr2[i];
}

int m = sizeof(arr1)/sizeof(arr1[0]);

int n = sizeof(arr2)/sizeof(arr2[0]);

   printIntersection(arr1, arr2, m, n);

getchar();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote