Question: Create class IntegerSet for which each object can hold integers in the range 0 through 100. A set is represented internally as an array of

Create class IntegerSet for which each object can hold integers in the range 0 through 100. A set is represented internally as an array of ones and zeros. Array element a[ i ] is 1 if integer i is in the set. Array element a[ j ] is 0 if integer j is not in the set. Provide two constructors for the class.

(1) One constructor does not have any parameter. This constructor initializes a set to the so-called empty-set, i.e., a set whose array representation contains all zeros. (2) An additional constructor that receives an array of integers and the size of that array and uses the array to initialize a set object.

Provide member functions for the common set operations. (1) A unionOfsets member function creates a third set that is the set-theoretic union of two existing sets (i.e. an element of the third arrays is set to 1 if that element is 1 in either or both of the existing sets, and an element of the third sets array is set to 0 if that element is 0 in each of the existing sets). (2) An intersectionOfSets member function which creates a third set which is the set-theoretic intersection of two existing sets (i.e. an element of the third sets array is set to 0 if that element is 0 in either or both of the existing sets, and an element of the third sets array is set to 1 if that element is 1 in each of the existing sets). (3) An insertElement member function inserts a new integer k into a set (by setting a[k] to 1). (4) A deleteElement member function that deletes integer m (by setting a[m] to 0). (5) A printSet member function prints a set as a list of numbers separated by spaces. Print only those elements that are present in the set (i.e. their position in the array has a value of 1). Print --- for an empty set. (6) An isEqualTo member function that determines whether two sets are equal. (7) An emptySet member function that sets all elements of set to 0. (8) An inputSet member function that reads value from user into the set. (9) A validEntry member function that determines a valid entry to the set (i.e. in the range 0 through 100).

Separate your class definition from implementation. Your class definition must be stored in a file called IntegerSet.h. Your class implementation must be stored in a file called IntegerSet.cpp. Use the test driver hw3.cpp (posted on Canvas) to test your IntegerSet class. The output is provided below.

Enter set A: Enter an element (-1 to end): 45 Enter an element (-1 to end): 76 Enter an element (-1 to end): 34 Enter an element (-1 to end): 6 Enter an element (-1 to end): -1 Entry complete Enter set B: Enter an element (-1 to end): 34 Enter an element (-1 to end): 8 Enter an element (-1 to end): 93 Enter an element (-1 to end): 45 Enter an element (-1 to end): -1 Entry complete Union of A and B is: { 6 8 34 45 76 93 } Intersection of A and B is: { 34 45 } Set A is not equal to set B Inserting 77 into set A Set A is now: { 6 34 45 76 77 } Deleting 77 from set A Set A is now: { 6 34 45 76 } Invalid insert attempted! Invalid insert attempted! Set e is: { 1 2 9 25 45 67 99 100 }

The code for hw3.cpp is below:

//hw3.cpp

#include

using namespace std;

#include "IntegerSet.h"

int main()

{

IntegerSet a, b, c, d; //create objects of Integerset

cout << "Enter set A: ";

a.inputSet();

cout << " Enter set B: ";

b.inputSet();

c = a.unionOfSets(b); //union of set A and set B

d = a.intersectionOfSets(b);

cout << " Union of A and B is: ";

c.printSet();

cout << "Intersection of A and B is: ";

d.printSet();

//test if set A is equal to set B

if (a.isEqualTo(b))

cout << "Set A is equal to set B ";

else

cout << "Set A is not equal to set B ";

// test insertion

cout << " Inserting 77 into set A... ";

a.insertElement(77);

cout << "Set A is now: ";

a.printSet();

// test deletion

cout << " Deleting 77 from set A... ";

a.deleteElement(77);

cout << "Set A is now: ";

a.printSet();

const int arraySize = 10;

int intArray[arraySize] = {25, 67, 2, 9, 99, 105, 45, -5, 100, 1};

// use the constructor that receives an array of integers

// and the size of that array to create a set object

IntegerSet e(intArray, arraySize);

cout << " Set e is: ";

e.printSet(); cout << endl;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!