1. (60pts) Write a program that takes two sets A and B with maximal size being 1
ID: 3885421 • Letter: 1
Question
1. (60pts) Write a program that takes two sets A and B with maximal size being 10 as input. Use alphabetic letters as set elements. For example, A={a, b, c, d, e, f, g, h, j, k}. Implement the following basic operations from set theory and print out the results in the console: (a) union of A and B. (b) intersection of A and B. (c) subtraction of A and B. That is , AB. (d) decide if A is a subset of B. Return TRUE if yes, otherwise FALSE. Note:
(1) There are some C++ algorithms doing the same operations (such as set union, set intersection et al.), but you are NOT allowed to use them. You must write your functions to implement these operations.
(2) To represent set in C++, you can use, but not restrict to, std:vector from the standard template library by using #include. More information about vector in C++ can be found at http://www.cplusplus.com/reference/vector/vector/
2. (20pts) Based on the functions implemented in Problem 1, write a program that takes three sets A, B and C as input. Then implement Question (a) and (b), and answer Question (c) as follows: (a) A B C (b) A B A C (c) Compare the results from (a) and (b), what conclusion can you draw from your observation? You may try different setups for A, B and C to confirm your conclusion
Explanation / Answer
Hi, I have answered Q1 (both parts).
Please repost other question in separate post.
Please let me know in case of any issue in Q1.
#include <iostream>
#include <vector>
using namespace std;
vector<char> my_union(vector<char> A, vector<char> B) {
vector<char> u;
// adding all elements of A in u
for(int i=0; i<A.size(); i++)
u.push_back(A[i]);
// adding B's elements
for(int i=0; i<B.size(); i++) {
// if current element from B is not in A, then add in union
if(!(std::find(A.begin(), A.end(), B[i]) != A.end())) {
u.push_back(B[i]);
}
}
return u;
}
vector<char> my_intersection(vector<char> A, vector<char> B) {
vector<char> I;
// adding B's elements
for(int i=0; i<B.size(); i++) {
// if current element from B is also in A, then add in intersection
if(std::find(A.begin(), A.end(), B[i]) != A.end()) {
I.push_back(B[i]);
}
}
return I;
}
vector<char> my_difference(vector<char> A, vector<char> B) {
vector<char> D;
// adding B's elements
for(int i=0; i<A.size(); i++) {
// if current element from A is also in B, then add in difference
if(std::find(B.begin(), B.end(), A[i]) != B.end()) {
D.push_back(A[i]);
}
}
return D;
}
int main() {
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.