COMP 2103: Discrete Structures Fall 2018 Homework #1 Due 09/17/2018 (Monday) 10:
ID: 3744499 • Letter: C
Question
COMP 2103: Discrete Structures Fall 2018 Homework #1 Due 09/17/2018 (Monday) 10:00AM l. Textbook, exercise 1.1 (page #4-5): Questions: # 10, #16, and #28 (see example 2. Textbook, exercise 1.2(page #1 I): Questions: #2. and # 16. 3. Use the following information to answer the questions. A study of 115 people shows that 85 eat breakfast, 58 use dental floss regularly, and 27 subscribe to the morning paper. Among those who eat breakfast, 52 floss regularly and 15 get the morning paper. Further, 10 breakfast eaters both floss and get the paper. Finally, of the 115 people, 14 don't do any of these morning activities. How many eat breakfast, or floss regularly, or get the morning paper? How many of those who use dental floss regularly also get the morning paper? a. b. Write a C++program to implement union, intersection, and complement of B with respect to A. 4. Use the provided C++ code (operation.cpp) to start the homework. The first function Union has been implemented. Textboot: Discrete matnematca structurCS Kolman, us, RossExplanation / Answer
#include <iostream>
#include<set>
#include<iterator>
using namespace std;
void Union(int A[],int B[]);
void Intersection(int A[],int B[]);
void Minus(int A[],int B[]);
void Intersection(int A[], int B[])
{
set<int> seMinus; //take a set of type int
set<int>::iterator it;
for (int i = 0; i < A1.size(); i++) //insert all element of array A into set
seMinus.insert(arr1[i]);
for (int i = 0; i < B1.size(); i++) { // iterate over the set B to check the element of array B is in set or not
if (hs.find(B[i]) != hs.end()) //if yes delete that element from the set
for(it = seMinus.begin();it != seMinus.end(); ++it)
{ if(*it== B[i])
seMinus.erase(it);
}
}
for(it = seMinus.begin();it=seMinus.end();++it){ //iterate over set and printing it will give complement of B wrt A
cout<<*it<<endl;
}
}
void Intersection(int A[], int B[])
{
set<int> seIntersection; //take a set of type int
for (int i = 0; i < A1.size(); i++) //insert all element of array A into set
seIntersection.insert(arr1[i]);
for (int i = 0; i < B1.size(); i++) //iterate over the set if the element of array B is found in set print it
if (hs.find(B[i]) != hs.end())
cout << B[i] << " ";
}
void Union(int A[],int B[]){
set<int> seunion; //take a set of type int
for(int i=0;i<A.size();i++) //insert all element of array A into set
seunion.insert(A[i]);
for(int j=0;j<B.size();j++) //insert all element of array B into set
seunion.insert(B[i]);
set<int>::iterator it;
for ( it = seunion.begin(); it != seunion.end(); it++) // Iterate over set defined as set only stores unique
cout << *it << " "; //element it outs union of two sets
}
int main()
{
//Enter your arrays here and pass to the required function to calculate union,Intersection or difference of two
//se
cout<<"Enter size of array A";
cin>>n1;
int a[n1];
for(int i=0;i<n1;i++)
cin>>a[i];
cout<<"Enter size of array B";
cin>>n2;
int b[n2];
for(int i=0;i<n2;i++)
cin>>a[i];
union(a,b); // prints union of two array a and b
Intersection(a,b); // prints Intersection of two array a and b
Minus(a,b); //prints the compement of b wrt a
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.