Question

Requirements The assignment is to create a dynamic array implementation of a set (defined in set.h)....

Requirements

The assignment is to create a dynamic array implementation of a set (defined in set.h). Add the efficiency of each function to the documentation in the herder file. Use the test_set.cpp as your test program.

_______________________________________________________________________________________________________________________________________________________

set.h file

#ifndef _SET_H_
#define _SET_H_

#include <cstdlib>
#include <iostream>

class set
{
public:
typedef int value_type;
typedef std::size_t size_type;
set(size_type initial_capacity);
// postcondition: empty set with initial_capacity has been created
~set();
// postcondition: all dynamically allocated memory has been deallocated
set(const set& s);
// copy of s has been created;
set& operator= (const set& s);
// postcondition: exact copy of s has been assigned to the current set object
bool erase(const value_type& target);
// postcondition: returned true if target was removed from set ow false if target was not in the set
bool insert(const value_type& entry);
// postcondition: if entry was not in the set, then entry has been added - ow nothing was done
void operator+=(const set& addend);
// postcondition: non-duplicating elements from addend have been added to the set
size_type size() const;
// postcondition: number of elements in the set has been returned
bool contains(const value_type& target) const;
// postcondition: returned whether target is in the set
friend std::ostream& operator<<(std::ostream& output, const set& s);
private:
void reserve (size_type new_capacity);
// precondition: size() <= new_capacity
// postcondition: capacity is new_capacity
value_type* data; // The array to store items
size_type used; // How much of array is used
size_type capacity;
};

#endif // _SET_H_

_______________________________________________________________________________________________________________________________________________________

test_set.cpp

#include <iostream>
#include "set.h"

int main(int argc, char **argv)
{
set s(5);
s.insert (7);
std::cout << s << std::endl;
s.insert (8);
std::cout << s << std::endl;
s.insert(3);
std::cout << s << std::endl;
s.insert (2);
s.insert (1);
s.insert (22);
std::cout << s << std::endl;
set s1(s);
std::cout << s1 << std::endl;
set s2 (78);
s2 = s;
std::cout << s2 << std::endl;
}

Homework Answers

Answer #1

Code

set.h

#ifndef _SET_H_
#define _SET_H_

#include <cstdlib>
#include <iostream>

class set
{
   public:
   typedef int value_type;
   typedef std::size_t size_type;
   set(size_type initial_capacity);
   // postcondition: empty set with initial_capacity has been created
   ~set();
   // postcondition: all dynamically allocated memory has been deallocated
   set(const set& s);
   // copy of s has been created;
   set& operator= (const set& s);
   // postcondition: exact copy of s has been assigned to the current set object
   bool erase(const value_type& target);
   // postcondition: returned true if target was removed from set ow false if target was not in the set
   bool insert(const value_type& entry);
   // postcondition: if entry was not in the set, then entry has been added - ow nothing was done
   void operator+=(const set& addend);
   // postcondition: non-duplicating elements from addend have been added to the set
   size_type size() const;
   // postcondition: number of elements in the set has been returned
   bool contains(const value_type& target) const;
   // postcondition: returned whether target is in the set
   friend std::ostream& operator<<(std::ostream& output, const set& s);
private:
   void reserve (size_type new_capacity);
   // precondition: size() <= new_capacity
   // postcondition: capacity is new_capacity
   value_type* data; // The array to store items
   size_type used; // How much of array is used
   size_type capacity;
};

#endif // _SET_H_

set.cpp

#include"set.h"

set::set(size_type initial_capacity)
{
   data= new value_type[initial_capacity];
   used=0;
   capacity=initial_capacity;
}
set::set(const set& s)
{
   this->capacity=s.capacity;
   this->used=s.used;
   this->data=s.data;
}
bool set::erase(const value_type& target)
{
   int count=0;
   for(int i=0; i<used; i++)
   {
       if(data[i]==target)
       {
           for(int j=i; j<(used-1); j++)
           {
               data[j]=data[j+1];
           }
           count++;
           return true;
       }
   }
   return false;
}
bool set::insert(const value_type & entry)
{
   if(used>=capacity)
   {
       size_type newSize=capacity*1.5;
       reserve(newSize);
       capacity=newSize;
   }
   if(!contains(entry))
   {
       data[used]=entry;
       used++;
       return true;
   }
   return false;
}
set & set::operator = (const set &s)
{
// Check for self assignment
if(this != &s)
{
       this->used=s.used;
       this->data=s.data;
}
return *this;
}
bool set::contains(const value_type& target) const
{
   for(int i=0;i<used;i++)
       if(data[i]==target)
           return true;
   return false;
}
void set::operator+=(const set& addend)
{
   for(int i=0;i<addend.used;i++)
   {
       if(!contains(addend.data[i]))
       {
           if(used>=capacity)
           {
               size_type newSize=capacity*1.5;
               reserve(newSize);
               capacity=newSize;
           }
           data[used]=addend.data[i];
           used++;
       }
   }
}
set::size_type set::size() const
{
   return used;
}
std::ostream& operator<<(std::ostream& output, const set& s)
{
   for(int i=0;i<s.used-1;i++)
       output<<s.data[i]<<",";
   output<<s.data[s.used-1];
   return output;
}
void set::reserve (size_type new_capacity)
{
   if(capacity<=new_capacity)
   {
       value_type* temp = new value_type[new_capacity];
       for(int i=0;i<used;i++)
       {
           temp[i]=data[i];
       }
       delete [] data;
       data = temp;
   }
}
set::~set()
{
   delete data;
}

test_set.cpp

#include <iostream>
#include "set.h"

int main(int argc, char **argv)
{
   set s(5);
   s.insert (7);
   std::cout << s << std::endl;
   s.insert (8);
   std::cout << s << std::endl;
   s.insert(3);
   std::cout << s << std::endl;
   s.insert (2);
   s.insert (1);
   s.insert (22);
   std::cout << s << std::endl;
   set s1(s);
   std::cout << s1 << std::endl;
   set s2 (78);
   s2 = s;
   std::cout << s2 << std::endl;
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
#ifndef BAG_H #define BAG_H #include <cstdlib> // Provides size_t using namespace std; class bag { public:...
#ifndef BAG_H #define BAG_H #include <cstdlib> // Provides size_t using namespace std; class bag { public: // TYPEDEFS and MEMBER CONSTANTS typedef int value_type; typedef std::size_t size_type; static const size_type CAPACITY = 30; // CONSTRUCTOR bag() {used = 0;} // MODIFICATION MEMBER FUNCTIONS size_type erase(const value_type& target); bool erase_one(const value_type& target); void insert(const value_type& entry); void operator +=(const bag& addend); void sort(const bag& b); //Sort the array in the bag object // CONSTANT MEMBER FUNCTIONS size_type size( ) const {...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity  ...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity   Code   Description   Project   File   Line   Suppression State Warning   C6385   Reading invalid data from 'DynamicStack': the readable size is '(unsigned int)*28+4' bytes, but '56' bytes may be read.   Here is the C++ code were I'm having the warning. // Sstack.cpp #include "SStack.h" // Constructor SStack::SStack(int cap) : Capacity(cap), used(0) {    DynamicStack = new string[Capacity]; } // Copy Constructor SStack::SStack(const SStack& s) : Capacity(s.Capacity), used(s.used)...
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined...
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined on page 257. The function takes as input a pointer to the head of a linked list consisting of numbers. The function should remove the second item in the list. If the list had only one item, the function should delete that item. If the list was empty, then let the list remain empty. In all cases return the new head of the list...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const size_t& count) class ListNode { public: typedef int value_type; ListNode (value_type d = value_type(), ListNode* n = NULL) { datum = d; next = n; }    //Assessor value_type getDatum () const { return datum; } ListNode const* getNext () const { return next; }    //Mutator void setDatum (const value_type& d) {datum = d; } ListNode* getNext () { return next; } void...
Circle.cpp /******************************************************* * * Assignment: Lab 2 *    * Class member functions look like other...
Circle.cpp /******************************************************* * * Assignment: Lab 2 *    * Class member functions look like other functions * with the following differences: * 1) The function name has the format Class::Method * 2) The function can access any private or public data * defined for the class, e.g. "radius" in this example * 3) The constructor function(s) have no return data type ********************************************************/ #include <iostream> #include <string> #include "circle.h" // the definion of our Circle class using namespace std; /****************************************************...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) //...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) // The Set Difference between two sets A and B is the set that consists of the elements of A which are not elements of B. Bag bag1 = (1,2,3) and Bag bag2 = (2,4,5) then bag1-=bag2 should return 1,3,4,5. //parameter a_bag to be subtracted from this (the calling) bag //post removes all data from items_ that is also found in a_bag //Since type is...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...