Question

In: Computer Science

Assignment requirements You will be creating your custom Set (think a list of unique elements) class...

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()

Solutions

Expert Solution

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 :)


Related Solutions

Python This part involves creating a function that will manage a List of unique strings. The...
Python This part involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test program that takes a string as input and then calls the function over and over until the...
Question 1 Have you set-up any unique EARCONS on your phone? Question 2 Do you think...
Question 1 Have you set-up any unique EARCONS on your phone? Question 2 Do you think mobile phone should have a different sound to the caller if your phone is busy? Question 3 Would you like to set-up your phone some that if persons A, B, or C (your favorite people) call you and you're on your phone would you like them to hear something other than "beep ... beep .... beep" (means your phone is busy)? Question 4 Would...
Create a class that generates permutations of a set of symbols. Requirements The class must be...
Create a class that generates permutations of a set of symbols. Requirements The class must be named PermutationGenerator. The PermutationGenerator class has two methods as follows. hasNext This method has no parameters.  It returns true if at least one permutation remains to be generated. next This method has no parameters.  It returns an array of the symbols (char[]) in a permutation (if any remain) or null otherwise. The following main method MUST be used, with NO CHANGES to test your class. public static void main(String[] args) { int count = 0; PermutationGenerator pg...
Retail Co has a unique set of HR practices. Describe and evaluate how you think the...
Retail Co has a unique set of HR practices. Describe and evaluate how you think the HR practices before the takeover have been influenced by:- 1 national culture organizational culture 2 other organizational
HEALTHCARE ADMINISTRATION For this week’s assignment, you will be creating a marketing tool for your practice...
HEALTHCARE ADMINISTRATION For this week’s assignment, you will be creating a marketing tool for your practice to advertise the new vaccine clinic. You can either create a brochure to send out in the mail or posters to display around town. Consider your audience (who are you targeting?) while you create this item. Think about what would catch your eye as a customer when putting this together. You can create this tool in Word as a poster with clip art, etc....
Each mission system and support system consists of a unique set of integrated system elements that...
Each mission system and support system consists of a unique set of integrated system elements that enable the system to accomplish its mission and objectives such as: Personnel, equipment, software, mission resources, procedural data, system responses, facilities. Identify what each component consist of an Imaging Company Information System
Creating a children’s book of your choice! -For this assignment, you must create an original story...
Creating a children’s book of your choice! -For this assignment, you must create an original story of your own making. -The story should be at least 5 pages long. (If your text is very short, you can consider full-page illustrations.) -You can choose the form of your text (i.e. will it be a poem? A rhyming narrative? Will it include rhyme at all? Will it be a fairy tale? Will it be an extended lyrical poem? Will it be a...
Creating a children’s book of your choice! -For this assignment, you must create an original story...
Creating a children’s book of your choice! -For this assignment, you must create an original story of your own making. -The story should be at least 5 pages long. (If your text is very short, you can consider full-page illustrations.) -You can choose the form of your text (i.e. will it be a poem? A rhyming narrative? Will it include rhyme at all? Will it be a fairy tale? Will it be an extended lyrical poem? Will it be a...
Use set notation to list the elements. 1.The set of positive integers between 5 and 9....
Use set notation to list the elements. 1.The set of positive integers between 5 and 9.    2. The set of seasons in a year. State whether the first set is a subset of the second set. 3. {7, 9, 11}; {The odd counting numbers}    4. {The integers larger than 5}; {7, 8, 9,…} Find the cardinality for the set. 5. A = {The number of seconds in a minute}    6. A = {-8, -7, -6,…, 0}
Assignment: The Ordered List In this assignment, you will create an ordered list of movies. The...
Assignment: The Ordered List In this assignment, you will create an ordered list of movies. The list will always be maintained in sorted order (ascending order, by movie title) by assuring that all new movies are inserted in the correct location in the list. Create a new project to hold your implementation of an ordered singly-linked list. You will need a main.cppto use as a test driver, as well as header and implementation files as described below. Movie To represent...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT