(IntegerSet Class) Create class IntegerSet for which each object can hold integers in the range 0 through 100. Represent the set internally as a vector of bool values. Element a[i] is true if integer i is in the set. Element a[j] is false if integer j is not in the set. The default constructor initializes a set to the so-called “empty set,” i.e., a set for which all elements contain false.

Provide member functions for the common set operations. For example, provide a unionOf- Sets member function that creates a third set that is the set-theoretic union of two existing sets (i.e., an element of the result is set to true if that element is true in either or both of the existing sets, and an element of the result is set to false if that element is false in each of the existing sets). Provide 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 result is set to false if that ele- ment is false in either or both of the existing sets, and an element of the result is set to true if that element is true in each of the existing sets). Provide an insertElement member function that places a new integer k into a set by setting a[k] to true. Provide a deleteElement member function that deletes integer m by setting a[m] to false. Provide a printSet member function that 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 vector has a value of true). Print --- for an empty set. Provide an isEqualTo member function that determines whether two sets are equal. Provide an additional constructor that receives an array of integers and the size of that array and uses the array to initialize a set object. Now write a driver program to test your IntegerSet class. Instantiate several IntegerSet objects. Test that all your member functions work properly.

What will be an ideal response?


```
// Header file for class IntegerSet
#ifndef INTEGER_SET_H
#define INTEGER_SET_H

#include
using namespace std;

class IntegerSet
{
public:
static const int setSize = 101; // number of set elements

// default constructor
IntegerSet()
: set( setSize )
{
} // end IntegerSet constructor

IntegerSet( int [], int ); // constructor that takes an initial set
IntegerSet unionOfSets( const IntegerSet& ) const;
IntegerSet intersectionOfSets( const IntegerSet& ) const;
void inputSet(); // read values from user
void insertElement( int );
void deleteElement( int );
void printSet() const;
bool isEqualTo( const IntegerSet& ) const;
private:
vector< bool > set; // range of 0 - 100

// determines a valid entry to the set
int validEntry( int x ) const
{
return ( x >= 0 && x < setSize );
} // end function validEntry
}; // end class IntegerSet

#endif
```
```
// Member-function definitions for class IntegerSet.
#include
#include
#include "IntegerSet.h" // IntegerSet class definition
using namespace std;

// constructor creates a set from array of integers
IntegerSet::IntegerSet( int array[], int size )
: set( setSize )
{
for ( int i = 0; i < size; i++ )
insertElement( array[ i ] );
} // end IntegerSet constructor

// input a set from the user
void IntegerSet::inputSet()
{
int number;

do
{
cout << "Enter an element (-1 to end): ";
cin >> number;

if ( validEntry( number ) )
set[ number ] = true;
else if ( number != -1 )
cerr << "Invalid Element\n";
} while ( number != -1 ); // end do...while

cout << "Entry complete\n";
} // end function inputSet

// prints the set to the output stream
void IntegerSet::printSet() const
{
int x = 1;
bool empty = true; // assume set is empty

cout << '{';

for ( int i = 0; i < setSize; i++ )
{
if ( set[ i ] )

{ cout << setw( 4 ) << i << ( x % 10 == 0 ? "\n" : "" );
empty = false; // set is not empty
x++;
} // end if
} // end for

if ( empty )
cout << setw( 4 ) << "---"; // display an empty set

cout << setw( 4 ) << "}" << '\n';
} // end function printSet

// returns the union of two sets
IntegerSet IntegerSet::unionOfSets( const IntegerSet &r ) const
{
IntegerSet temp;

// if element is in either set, add to temporary set
for ( int i = 0; i < setSize; i++ )
temp.set[ i ] = set[ i ] || r.set[ i ];

return temp;
} // end function unionOfSets

// returns the intersection of two sets
IntegerSet IntegerSet::intersectionOfSets( const IntegerSet &r ) const
{
IntegerSet temp;

// if element is in both sets, add to temporary set
for ( int i = 0; i < setSize; i++ )
temp.set[ i ] = set[ i ] && r.set[ i ];

return temp;
} // end function intersectionOfSets

// insert a new integer into this set
void IntegerSet::insertElement( int k )
{
if ( validEntry( k ) )
set[ k ] = true;
else
cerr << "Invalid insert attempted!\n";
} // end function insertElement

// removes an integer from this set
void IntegerSet::deleteElement( int k )
{
if ( validEntry( k ) )
set[ k ] = false;
else
cerr << "Invalid delete attempted!\n";
} // end function deleteElement

// determines if two sets are equal
bool IntegerSet::isEqualTo( const IntegerSet &r ) const
{
for ( int i = 0; i < setSize; i++ )
if ( set[ i ] != r.set[ i ] )
return false; // sets are not equal

return true; // sets are equal
} // end function isEqualTo
```
```
// Driver program for class IntegerSet.
#include
#include "IntegerSet.h" // IntegerSet class definition
using namespace std;

int main()
{
IntegerSet a;
IntegerSet b;
IntegerSet c;
IntegerSet d;

cout << "Enter set A:\n";
a.inputSet();
cout << "\nEnter set B:\n";
b.inputSet();
c = a.unionOfSets( b );
d = a.intersectionOfSets( b );
cout << "\nUnion of A and B is:\n";
c.printSet();
cout << "Intersection of A and B is:\n";
d.printSet();

if ( a.isEqualTo( b ) )
cout << "Set A is equal to set B\n";
else
cout << "Set A is not equal to set B\n";

cout << "\nInserting 77 into set A...\n";
a.insertElement( 77 );
cout << "Set A is now:\n";
a.printSet();

cout << "\nDeleting 77 from set A...\n";
a.deleteElement( 77 );
cout << "Set A is now:\n";
a.printSet();

const int arraySize = 10;
int intArray[ arraySize ] = { 25, 67, 2, 9, 99, 105, 45, -5, 100, 1 };
IntegerSet e( intArray, arraySize );

cout << "\nSet E is:\n";
e.printSet();

cout << endl;
} // end main
```
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 }

Computer Science & Information Technology

You might also like to view...

____________________ data means manipulating it in some way.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

Complete function incr so its effect mimics the prefix increment of an integer variable: that is, both ++i and incr(&i) would add 1 to i and evaluate to the incremented value.

What will be an ideal response?

Computer Science & Information Technology

Folders that display a(n) ____ contain compressed files.

A. icon B. image C. zipper D. vice

Computer Science & Information Technology

Are the Internet and the World Wide Web one and the same? Why or why not?

What will be an ideal response?

Computer Science & Information Technology