Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the code (IntegerSet.h IntegerSet.cpp lab8test.cpp) for a class IntegerSet. Each object of class IntegerSet can hold integers in the range 0 through N where

Complete the code (IntegerSet.h IntegerSet.cpp lab8test.cpp) for a class IntegerSet.

Each object of class IntegerSet can hold integers in the range 0 through N where N is a integer. A set is represented internally as an array of N ones or 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. The default constructor initializes a set to the so-called empty set, i.e., a set whose array representation contains all zeros.

Description of the member functions:

unionOfIntegerSets member function creates a third set which is the set-theoretic union of two existing sets (i.e., an element of the third sets array is set to 1 if the 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).

intersectionOfIntegerSets member function 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).

insertElement member function inserts a new integer k into a set (by setting a[k] to 1).

deleteElement member function deletes integer m (by setting a[m] to 0).

Print 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.

isEqualTo member function determines if two sets are equal.

Overload the following operators for class IntegerSet.

= (assignment)

+ (set union: setA + setB returns a set that contains elements in setA or setB.)

+= (setA += setB assigns setA + setB to setA.)

= = isEqualTo (check if two sets are equal)

- (setA - setB returns a set that contains elements in setA , but not in setB.)

> (setA > setB iff all the elements in setB are also in setA)

Test the member function and operators with a sample output.

IntergetSet.h

//header file for integer set #ifndef INTSET_H #define INTSET_H //class IntegerSet definition class IntegerSet { public: IntegerSet (int n); //constructor: n is the size of the set IntegerSet (const IntegerSet &); //copy constructor IntegerSet unionOfIntegerSets(const IntegerSet &); IntegerSet intersectionOfIntegerSets(const IntegerSet &); void emptySet(); void inputSet(); void insertElement(int); void deleteElement(int); void print() const; bool isEqualTo(const IntegerSet &) const; //write prototype for destructor ~IntegerSet(); private: int *set; int size; bool validEntry(int x) const { return x >= 0 && x < size; } }; #endif //INTSET_H 

IntergerSet.cpp

//IntegerSet.cpp #include  #include  #include "IntegerSet.h" //constructor IntegerSet::IntegerSet(int s) { size = s; set = new int[size]; /*write call to emptySet */ } //initialize every element to zero void IntegerSet::emptySet() { for(int i = 0; i < size; i++) { set[i] = 0; } } //copy constructor IntegerSet::IntegerSet(const IntegerSet &init) { size = init.size; // write statement to allocate sufficient memory emptySet(); for (int i = 0; i < size ; i++) //write statement to copy element of init */ } //input set void IntegerSet::inputSet() { int number; //input set information do { cout << "Enter an element (-1 to end): "; cin >> number; if(validEntry(number)) set[number] = 1; else if (number != -1) cout << "Invalid Element "; } while (number != -1); cout << "Entry complete "; } void IntegerSet::print() const { int x = 1; bool empty = true; //assume set is empty cout <<'{'; for (int u = 0; u < size ; u++) { if (set[u]) { cout << setw(4) << u << (x%10 == 0 ? " " : ""); empty = false; ++x; } } if (empty) cout << setw(4) << "-----"; cout << setw(4) << "}" << " "; } //finds union of Integer sets IntegerSet IntegerSet::unionOfIntegerSets(const IntegerSet &r) { IntegerSet temp (size > r.size ? size : r.size); temp.emptySet(); for (int i = 0; i < size; i++) temp.set[i] =set[i]; for (int i = 0; i < r.size; i++) temp.set[i] |= r.set[i]; return temp; } //write definition of intersectionOfIntegerSets //inset element into set void IntegerSet::insertElement(int k) { if (validEntry(k)) set[k] = 1; else cout << "Invalid insert attempted! "; } //write definition for deleteElement //wirte definition for isEqualTo //write definition for destructor 

Test.cpp

#include  #include "IntegerSet.h" int main() { IntegerSet a(101); IntegerSet b(101); IntegerSet c(101); IntegerSet d(101); cout << "Enter set A: "; a.inputSet(); b.inputSet(); /*Write call to unionOfIntegerSets for object a passing it b; assign the result to c */ /*Write call to intersectionOfIntegerSets for object a passing it b; assign the result to d */ cout << "union of A and B is : "; c.print(); cout << "Intersection of A and B is : "; d.print(); if(a.isEqualTo(b)) cout << "Set A is equal to Set B "; else cout << "Set A is not equal to Set B "; cout << " Inserting 77 into set A ... "; a.insertElement(77); cout << "Set A is now: "; a.print(); cout << " Deleting 77 into set A ... "; a.deleteElement(77); cout << "Set A is now: "; a.print(); cout << endl; return 0; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Informix Database Administrators Survival Guide

Authors: Joe Lumbley

1st Edition

0131243144, 978-0131243149

More Books

Students also viewed these Databases questions