Question

In: Computer Science

Assume we have two sequences of values S1 containing 1, 5, 3, 6, 7, 8 while...

Assume we have two sequences of values S1 containing 1, 5, 3, 6, 7, 8 while S2 containing 2, 5, 6, 9, 7. We store these two sequences as sets and perform a set intersection and set difference. Write C++ code to do that respectively.

Solutions

Expert Solution

C++ Program:

#include <iostream>
#include <set>
#include <iterator>

using namespace std;

int main()
{
   // empty set containers
   set <int, greater <int> > set1;  
   set <int, greater <int> > set2;
   set <int, greater <int> > setintersection;
   set <int, greater <int> > setdifference;

   // inserting elements into set1
   set1.insert(1);
   set1.insert(5);
   set1.insert(3);
   set1.insert(6);
   set1.insert(7);
   set1.insert(8);
  
   // inserting elements into set2
   set2.insert(2);
   set2.insert(5);
   set2.insert(6);
   set2.insert(9);
   set2.insert(7);
  
   //Set intersection of two sets give a set
   //with the elements present in both sets
   //Finding set intersection
   set <int, greater <int> > :: iterator itr1;
   for (itr1 = set1.begin(); itr1 != set1.end(); ++itr1)
   {
       set <int, greater <int> > :: iterator itr2;
       for(itr2 = set2.begin(); itr2 != set2.end(); ++itr2)
       {
       //Find a match, add it to intersection set
       if(*itr1 == *itr2)
       setintersection.insert(*itr1);
       }
  
   }

   // printing set intersection
   set <int, greater <int> > :: iterator itr3;
   cout << "\nThe set intersection is : ";
   for (itr3 = setintersection.begin(); itr3 != setintersection.end(); ++itr3)
   {
       cout << *itr3 << '\t';
   }
   cout << endl;
  
   //Set Difference of two sets gives a set
   //with elements present in first set and not present in second set
   //Finding set difference
   set <int, greater <int> > :: iterator itr4;
   for (itr4 = set1.begin(); itr4 != set1.end(); ++itr4)
   {
   int flag=0;
       set <int, greater <int> > :: iterator itr2;
       for(itr2 = set2.begin(); itr2 != set2.end(); ++itr2)
       {
       if(*itr4 == *itr2){
       flag=1;
       itr2=set2.end();
       }
       }
       //Not find a match, add it to difference set
   if(flag==0)
   setdifference.insert(*itr4);
   }

   // printing set difference
   set <int, greater <int> > :: iterator itr5;
   cout << "\nThe set difference is : ";
   for (itr5 = setdifference.begin(); itr5 != setdifference.end(); ++itr5)
   {
       cout << *itr5 << '\t';
   }
   cout << endl;

   return 0;

}

Code Screenshots:

Output Screenshot:


Related Solutions

C++, Java, Python. Assume we have two sequences of values S1 containing 1, 5, 3, 6,...
C++, Java, Python. Assume we have two sequences of values S1 containing 1, 5, 3, 6, 7, 8 while S2 containing 2, 5, 6, 9, 7. We’d store these two sequences as sets and performance set intersection and set difference. Write C++, Java, Python codes to do that respectively. Compare and contrast the readability and writability.
Use the following data Row 1- 1, 3, 3, 3, 5, 5, 6, 6, 7, 8...
Use the following data Row 1- 1, 3, 3, 3, 5, 5, 6, 6, 7, 8 Row 2- 94, 87, 83, 73, 91, 68, 74, 82, 65, 65 A. Find the equation of the regression line for the given data, row 1 represent the x values and row 2 the y values. Sketch a scatter plot of the data and draw the regression line. Input the values of the slope for the regression line when Row 1 represents the x...
x 2 8 5 9 4 3 9 6 7 8 y 3 6 5 7...
x 2 8 5 9 4 3 9 6 7 8 y 3 6 5 7 9 7 4 6 9 9 -5.48x + 0.17 5.48x + 0.17 -0.17x + 5.48 0.17x + 5.48
5. (20%) Suppose we have an array int a [8] = {1, 2, 3, 5, 6};...
5. (20%) Suppose we have an array int a [8] = {1, 2, 3, 5, 6}; and we also have a linked list L of 5 entries 1 -> 2 -> 3 -> 5 -> 6, where 1 is the first in the linked list L, followed by 2 etc. We want to put a new item 4 between 3 and 5 in the array a and in the linked list L (a) Explain in plain English how you do...
Assume that you have a fair 6 sided die with values {1, 2, 3, 4, 5,...
Assume that you have a fair 6 sided die with values {1, 2, 3, 4, 5, 6} and a fair 12 sided die with values {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}. A discrete random variable is generated by rolling the two dice and adding the numerical results together. (a) Create a probability mass function that captures the probability of all possible values of this random variable. You may use R or draw the pmf...
3, 7, 8, 5, 6, 4, 9, 10, 7, 8, 6, 5 Using the previous question...
3, 7, 8, 5, 6, 4, 9, 10, 7, 8, 6, 5 Using the previous question 's scores, If three points were added to every score in this distribution, what would be the new mean? If three points were added to every score in this distribution, what would be the new standard deviation. Remember, you have already calculated population standard deviation in a previous problem. This problem requires two answers.
Group A Group B 1 5 5 8 5 6 5 7 5 8 6 8...
Group A Group B 1 5 5 8 5 6 5 7 5 8 6 8 7 9 6 16 10 20 13 24 At the significance level of 0.05, are the drug concentrations in Group A and Group B different? Using the most appropriate t test.
Matrix A2= [1 2 3; 4 5 6; 7 8 9; 3 2 4; 6 5...
Matrix A2= [1 2 3; 4 5 6; 7 8 9; 3 2 4; 6 5 4; 9 8 7] Note: TA2 is defined to be a linear transformation that maps any vector x to A2* x. That is TA2 = A2*x. Also the range of the Linear transformation represented by A2 is the same as the column space of A2. l) Find a basis for the null(TA2). m) Find nullity of A2, TA2 and A2tA2. n) Find rank(A2), rank(A2t),...
n = 8 measurements: 5, 3, 6, 7, 6, 5, 4, 7 Calculate the sample variance,...
n = 8 measurements: 5, 3, 6, 7, 6, 5, 4, 7 Calculate the sample variance, s2, using the definition formula. (Round your answer to four decimal places.) s2 = Calculate the sample variance, s2 using the computing formula. (Round your answer to four decimal places.) s2 =   Find the sample standard deviation, s. (Round your answer to three decimal places.) s =
For any two real sequences {an} and {bn}, prove that Rudin’s Ex. 5 We assume that...
For any two real sequences {an} and {bn}, prove that Rudin’s Ex. 5 We assume that the right hand side is defined, that is, not of the form ∞ − ∞ or −∞ + ∞. lim sup (an + bn) ≤ lim sup an + lim sup bn. Proof If lim sup an = ∞ or lim sup bn = ∞, there is nothing to prove
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT