Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having trouble writing some code that should compare the elements in two se

ID: 3633942 • Letter: I

Question

I am having trouble writing some code that should compare the elements in two sets and return the elements that these two sets have in common.

This is the code that I have written for my Union set but the code for the intersection is not as simple. The intersection operator (*=) only takes one argument (const UIntSet& s) but in the test file there are 3 sets, s1, s2, s3, and depending on what I type in the program it determines which two sets to compare to determine the intersection. I will include the rest of my code below so that you can see what I have to work with.

UIntSet& UIntSet::operator+= (const UIntSet& s) // set = set union s
{
for(size_t i = 0; i < size_; ++i)
if(s.Member(i))
(*this).Insert(i);
return *this;
}


#ifndef _UINTSET_CPP
#define _UINTSET_CPP

#include <iostream>
#include <uintset.h>
#include <string>

//------------------------------------------------

UIntSet::UIntSet() : size_(64), bv_(size_) // default constructor
{}

//------------------------------------------------

UIntSet::UIntSet(size_t numbits) : size_(numbits), bv_(size_)
{}

//-----------------------------------------------

UIntSet::UIntSet(const UIntSet& s) : size_(s.size_), bv_(s.bv_)
{}

//----------------------------------------------

UIntSet::~UIntSet() // destructor
{}

void UIntSet::Dump(std::ostream& os) const
{
bv_.Dump(os);
}

//----------------------------------------------

UIntSet& UIntSet::operator = (const UIntSet& s) // assignment operator
{
if (this != &s)
{
size_ = s.size_;
bv_ = s.bv_;
}
return *this;
}

//-------------------------------------------

size_t UIntSet::Size() const // return number of elements in set
{
size_t count = 0;
for (size_t i = 0; i < size_; ++i)
if(Member(i) == 1)
++count;
return count;
}

//--------------------------------------------

void UIntSet::Insert(unsigned long n) // inserts n into set
{
bv_.Set(n);
}

void UIntSet::Remove(unsigned long n) // removes n from set
{
bv_.Unset(n);
}

//--------------------------------------------

void UIntSet::Clear() // makes set empty
{
bv_.Unset();
}

//--------------------------------------------

bool UIntSet::Member(unsigned long n) const // returns true iff n is in set
{
return bv_.Test(n);
}

//-------------------------------------------

bool UIntSet::Empty() const // true iff set is empty
{
for(size_t i=0; i < size_; ++i)
if(bv_.Test(i) == 1)
return false;
return true;
}

//--------------------------------------------

size_t UIntSet::Range() const // returns upper bound of range/univers [0,ub]
{
return size_;
}

//--------------------------------------------

UIntSet& UIntSet::operator+= (const UIntSet& s) // set = set union s
{
for(size_t i = 0; i < size_; ++i)
if(s.Member(i))
(*this).Insert(i);
return *this;
}

//--------------------------------------------

UIntSet& UIntSet::operator*= (const UIntSet& s) // set = set intersection s
{
for(size_t i = 0; i < size_; ++i)
if(s.Member(bv_)
(*this).Insert(i);
return *this;
}

//--------------------------------------------

UIntSet& UIntSet::operator-= (const UIntSet& s) // set = set difference s
{
for(size_t i = 0; i < size_; ++i)
if(s.Member(i)!=1)
(*this).Remove(i);
return *this;
}

// global functions & operators

//---------------------------------------------

std::ostream& operator << (std::ostream& os, const UIntSet& s)
{
os << '{';
for (size_t i =0; i < s.Range(); ++i)
if (s.Member(i)) // i is in the set
os << ' ' << i;
os << " }";
return os;
}

UIntSet operator+ (const UIntSet& s1,const UIntSet& s2) // returns s1 union s2
{
UIntSet s(s1);
s += s2;
return s;
}

UIntSet operator* (const UIntSet& s1, const UIntSet& s2) // returns s1 intersection s2
{
UIntSet s = s1;
s *= s2;
return s;
}

UIntSet operator- (const UIntSet& s1, const UIntSet& s2) // returns s1 difference s2
{
UIntSet s = s1;
s -= s2;
return s;
}
#endif


Explanation / Answer

Hi. Did you manage to figure it out? I'm having trouble with the implementation to overload the *= too :S