Complete the code (IntegerSet.h IntegerSet.cpp lab8test.cpp) for a class Integer
ID: 3864320 • Letter: C
Question
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 set’s array is set to 1 if the element is 1 in either or both of the existing sets, and an element of the third set’s 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 set’s array is set to 0 if that element is 0 in either or both of the existing sets, and an element of the third set’s 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
IntergerSet.cpp
Test.cpp
Explanation / Answer
IntegerSet.h
#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 &);
//write prototype for destructor
~IntegerSet();
private:
int *set;
int size;
bool validEntry(int x) const
{
return x >= 0 && x < size;
}
};
#endif //INTSET_H
IntegerSet.cpp
//IntegerSet.cpp
#include <iostream>
#include <iomanip>
#include "IntegerSet.h"
using namespace std;
//constructor
IntegerSet::IntegerSet(int s)
{
cout << "Entering set A: ";
size = s;
set = new int[size];
int i=0;
for(i=0;i<=s;i++){
set[i] = 0;
}
/*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++){
set[i] = init.set[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;
}
IntegerSet IntegerSet::intersectionOfIntegerSets(const IntegerSet &r)
{
int tempSize = size < r.size ? size : r.size;
IntegerSet temp (tempSize);
temp.emptySet();
for (int i = 0; i < tempSize; i++){
if(set[i] == 1 && r.set[i] == 1){
temp.set[i] =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! ";
}
void IntegerSet::deleteElement(int k)
{
if (validEntry(k))
set[k] = 0;
else
cout << "Invalid insert attempted! ";
}
//write definition for deleteElement
bool IntegerSet::isEqualTo(const IntegerSet &r)
{
if(size!=r.size){
return false;
}
for (int i = 0; i < size; i++){
if(r.set[i] != set[i]){
return false;
}
}
return true;
}
//wirte definition for isEqualTo
//constructor
IntegerSet::~IntegerSet()
{
size = 0;
set = NULL;
}
//write definition for destructor
Test.cpp
#include <iostream>
#include "IntegerSet.h"
using namespace std;
int main()
{
IntegerSet a = IntegerSet(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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.