Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the set class as specified in the provided Set.h file. You are NOT all

ID: 3820953 • Letter: I

Question

Implement the set class as specified in the provided Set.h file. You are NOT allowed to modify the Set.h file. It includes methods to check how many elements are in the set, add an element to the set, remove an element from the set, remove all elements from the set, check if an element is in the set, and check if one set is a subset of another. It specifies two stand-alone functions using operator overloading to union and take the difference of two sets. It specifies an output operator that must output the items in the set on one line separate by a comma and space. See the sample output below. for example, if a set contained 1, 2, 3, it vould output 1, 2, 3 for example if a set contained just 1, it would output Also write a main.cpp function that tests your set implementation. You may either use the C assert macro (see section 8.14 of the book) that will stop the program and tell you if an assertion fails or you may write code such as: Set a; if (a num Elements 0) cerr empty set does not have zero elements end else cerr You may assume that a set will never contain more than 100 elements since the Set class uses an array of size 100. Note that the operators are friends of the classes so they are allowed to access the private data of any Set instance.

Explanation / Answer

Here is the code for you:

#include "SetOperationsUsingArrays.h"
Set::Set()
{
    _numItems = 0;
}
void Set::addElement(int x)
{
    bool found = false;
    for(int i = 0; i < _numItems; i++)
        if(_items[i] == x)
        {
           found = true;
           break;
        }
    if(!found)
    {
       _items[_numItems] = x;
       _numItems++;
    }     
}
void Set::removeElement(int x)
{
    int pos = 0;
    for(int pos = 0; pos < _numItems; pos++)
        if(_items[pos] == x)
            break;
    if(pos != _numItems)
    {
       for(; pos < _numItems-1; pos++)
           _items[pos] = _items[pos+1];
       _numItems--;
    }              
}
void Set::removeAllElements()
{
    _numItems = 0;
}
bool Set::containsElement(int x) const
{
    for(int i = 0; i < _numItems; i++)
        if(_items[i] == x)
            return true;
    return false;      
}
bool Set::isSubsetOf(const Set &other) const
{
    for(int i = 0; i < other._numItems; i++)
        if(!containsElement(other._items[i]))
            return false;
    return true;      
}
Set operator+(const Set &a, const Set &b)
{
    Set c;
    for(int i = 0; i < a._numItems; i++)
        c.addElement(a._items[i]);
    for(int i = 0; i < b._numItems; i++)
        c.addElement(b._items[i]);  
    return c;  
}
Set operator-(const Set &a, const Set &b)
{
    Set c;
    for(int i = 0; i < a._numItems; i++)
        if(!b.containsElement(a._items[i]))  
            c.addElement(a._items[i]);
    return c;  
}
std::ostream& operator<<(std::ostream &os, const Set &a)
{
    os << "{";
    for(int i = 0; i < a._numItems-1; i++)
        os << a._items[i] << ", ";
    os << a._items[a._numItems-1] << "}";
    return os;  
}