In: Computer Science
Assignment requirements
You will be creating your custom Set (think a list of unique elements) class in C++. Please do NOT use STL, or any pre-defined library for this assignment.
1. The data type of the set collection: array (or vector).
2. Create three operations (based on Set ADT descriptions
-union()
-intersection()
-difference()
3. Put some test code in main()
NOTE: If there is any doubt in understanding the code feel free to put it in the comments, or if any changes are needed.
Here is the complete c++ code
//These are the required header file
#include <iostream>
#include <vector>
using namespace std;
class Myset
{
public:
vector<int> arr;
Myset()
{
//The constructor will clear the vector, ie it will empty it
arr.clear();
}
void addElement(int a)
{
for (int k : arr)
{
//If there is an element already in the array then go out of the loop
if (k == a)
return;
}
arr.push_back(a);
}
//To show all the elements
void showAll()
{
for (int k : arr)
cout << k << " ";
cout << "\n";
}
void findUnion(Myset b)
{
cout << "The union of \n";
for (int k : arr)
cout << k << " ";
cout << "\nAND\n";
for (int k : b.arr)
cout << k << " ";
cout << "\nis\n";
for (int k : arr)
cout << k << " ";
//For union we dont show repeated elements in both the sets
for (int j : b.arr)
{
bool found = false;
for (int k : arr)
{
if (k == j)
{
found = true;
break;
}
}
//We will show j only it it is not found in the arr
if (!found)
cout << j << " ";
}
cout << "\n";
}
void findIntersection(Myset b)
{
cout << "The intersection of \n";
for (int k : arr)
cout << k << " ";
cout << "\nAND\n";
for (int k : b.arr)
cout << k << " ";
cout << "\nis\n";
//For intersection we show only those elements that are in both the sets
for (int j : b.arr)
{
bool found = false;
for (int k : arr)
{
if (k == j)
{
//It is found we can break out of the loop
found = true;
break;
}
}
//If the element is found in both we show it else not
if (found)
cout << j << " ";
}
cout << "\n";
}
void findDifference(Myset b)
{
cout << "The difference of \n";
for (int k : arr)
cout << k << " ";
cout << "\nAND\n";
for (int k : b.arr)
cout << k << " ";
cout << "\nis\n";
//In the difference we have to find the elements that are in set a but not in set b
for (int k : arr)
{
bool found = false;
for (int j : b.arr)
{
if (j == k)
{
found = true;
break;
}
}
//If it is not found then we show the element else we do not
if (!found)
cout << k << " ";
}
cout << "\n";
}
};
int main()
{
Myset a, b;
//Adding elements to set a
a.addElement(2);
a.addElement(3);
a.addElement(4);
a.addElement(6);
a.addElement(6);
a.addElement(7);
a.addElement(3);
cout << "Showing set a: ";
a.showAll();
//Adding elements to set b
b.addElement(1);
b.addElement(2);
b.addElement(2);
b.addElement(3);
b.addElement(5);
b.addElement(9);
b.addElement(9);
b.addElement(7);
cout << "Showing set b: ";
b.showAll();
a.findUnion(b);
a.findIntersection(b);
a.findDifference(b);
return 0;
}
Here is the output of the above code, it can be seen that duplicates are not added in the set
NOTE: If you like the answer a thumbs up would make my day :)