C++ Finish the code

0 comments

There are two “TODO” to do. There will be more info attached in a file. You do need to make a new code from scratch, since most of it is done

I’m not paying more than 7 dollars because it’s not a big job. 

#include <iostream>
#include <string>

using namespace std;

//==========================================================================
//                                 CONSTANTS
//==========================================================================

// error codes
const int ERROR_CAPACITY_OVERFLOW = 1;

// maximum size allowed for a set
const int MAX_SET_SIZE = 256;

//==========================================================================
//                                  TYPES
//==========================================================================

// structure to represent sets of integers
struct IntSet {

    int elem[MAX_SET_SIZE]; // the elements in the set (occupied 0 to n-1)
    int size = 0;           // the number of elements currently in the set

};

//==========================================================================
//                                FUNCTIONS
//==========================================================================

//--------------------------------------------------------------------------
// isEmpty(s): returns true if the set is empty and false otherwise.
//--------------------------------------------------------------------------

bool isEmpty(IntSet s) {

    // s is empty if its size is zero
    return s.size == 0;

}

//-------------------------------------------------------------------------
// isMember(s, x): Returns true if x is a memeber of set s and false otherwise.
//--------------------------------------------------------------------------

bool isMember(IntSet s, int x) {

    // for every occupied cell of our array...
    for (int i = 0; i < s.size ; i++) {

        // compare contents to x
        if (s.elem[i] == x)
            return true;

    }

    // if we're here, x was not in the set
    return false;

}

//--------------------------------------------------------------------------
// add(s, x): adds element x into set s. If x already is a member of s, then
// this operation has no effect.
//--------------------------------------------------------------------------

void add(IntSet &s, int x) {

    // if there is room remaining
    if (s.size < MAX_SET_SIZE) {

        // if x is not already in the set
        if (!isMember(s, x)) {

            // place x in the elem array of s
            s.elem[s.size] = x;

            // update the size of s
            s.size++;

        }

    }
    else {

        // the set is filled to capacity
        cout << "Error: attempted to add an element to a maximum capacity set.";
        exit(ERROR_CAPACITY_OVERFLOW);

    }

}

//--------------------------------------------------------------------------
// clear(s): Clear the set s so that it is empty.
//--------------------------------------------------------------------------

void clear(IntSet &s) {
    s.size = 0;
}

//--------------------------------------------------------------------------
// print(s): Prints set s to the console.
//--------------------------------------------------------------------------

void print(IntSet s) {

    cout << "{";
    for (int i = 0; i < s.size - 1; i++) {
        cout << s.elem[i] << ", ";
    }
    if (s.size > 0) {
        cout << s.elem[s.size - 1];
    }
    cout << "}" << endl;

}

//-------------------------------------------------------------------------
// setIntersection(s1, s2, result): Compute the intersection of s1 and s2 and
// place it into result.
//--------------------------------------------------------------------------

void setIntersection(IntSet s1, IntSet s2, IntSet &result) {

    clear(result);

    // TODO: Complete the code below...

    // for every member of s1
        // if it is a member of s2
            // add it to the result

}

//-------------------------------------------------------------------------
// setUnion(s1, s2, result): Compute the union of s1 and s2 and place it into
// result.
// ------------------------------------------------------------------------

void setUnion(IntSet s1, IntSet s2, IntSet &result) {

    clear(result);

    // TODO: Complete the code below...

    // for every member of s1
        // add it to the result

    // for every member of s2
        // add it to the result

}

//--------------------------------------------------------------------------
//                                 MAIN PROGRAM
//--------------------------------------------------------------------------

int main() {

    // fill s1 with {1, 2, 3, 4, 5}
    IntSet s1;
    for (int x = 1; x <= 5; x++) {
        add(s1, x);
    }
    cout << "s1: ";
    print(s1);

    // fill s2 with {4, 5, 6, 7, 8}
    IntSet s2;
    for (int x = 4; x <= 8; x++) {
        add(s2, x);
    }
    cout << "s2: ";
    print(s2);

    // compute intersection and print it
    IntSet res1;
    setIntersection(s1, s2, res1);
    cout << "intersection of s1 and s2: ";
    print(res1);

    // compute union and print it
    IntSet res2;
    setUnion(s1, s2, res2);
    cout << "union of s1 and s2: ";
    print(res2);

}

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}