C++ finish the code

Anonymous
timer Asked: Apr 24th, 2016

Question description

Finish the code. There are 2 TODO codes in this program. I'm not paying more than 7 dollars because this is not a big task.
#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);

}

Tutor Answer

(Top Tutor) Studypool Tutor
School: Cornell University
Studypool has helped 1,244,100 students
flag Report DMCA
Similar Questions
Hot Questions
Related Tags

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors