The goal of this assignment is to reinforce implementation of container class co
ID: 3789219 • Letter: T
Question
The goal of this assignment is to reinforce implementation of container class concepts in C++. Specifically, the assingment is to do problem 3.5 on page 149 of the text, you need to implement the set operations union, intersection and relative complement.
The question on page 149 states
A bag can contain more than one copy of an item. For example, the chapter describes a bag that contains the number 4 and two copies of the number 8. This bag behavior is different from a set, which can contain only a single copy of any given item. Write a new container class called set, which is similar to a bag, except that a set can contain only one copy of any given item. You’ll need to change the interface a bit. For example, instead of the bag’s count function, you’ll want a constant member function such as this:
bool set::contains
(const value_type& target) const;
// Postcondition: The return value is true if
// target is in the set; otherwise the return
// value is false.
Make an explicit statement of the invariant of the set class. Do a time analysis for each operation. At this point, an efficient implementation is not needed. For example, just adding a new item to a set will take linear time because you’ll need to check that the new item isn’t already present. Later we’ll explore more efficient implementations (including the implementation
of set in the C++ Standard Library). You may also want to add additional operations to your set class, such as an operator for subtraction.
Explanation / Answer
#include <iostream>
#include <assert.h>
#include <stdlib.h>
#include "set.h"
void set::insert(const Item& entry)
{
assert(size() < CAPACITY);
//set::const;
if(contains(entry==false))
{
data[used] = entry;
used++;
}
}
bool set::contains(const Item& target)const
{
for(int i = 0;i < used;i++ )
{
if(data[i]==target)
return true;
}
return false;
}
void set::display()
{
if(used==0)
{
cout<<"The bag is empty."<<endl;
}
else{
for(size_t i=0; i<used;> cout<<data[i]<<" ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.