Question

The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal...

The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal is to create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Your program must compile. Use test_set.cpp as your test program.

Set.h & Test_Set.cpp is code that is already given. All I need is for you add comments to Set.cpp describing each function.

FILE: 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;
  static const size_type INITIAL_CAPACITY = 30;

  set(size_type initial_capacity = INITIAL_CAPACITY);
// postcondition: empty set has been created

  ~set();
// postcondition: set has been deallocated
  
  set (const set& source);
  // postcondition: a copy of source has been created

  set& operator = (const set& source);
  // postcondition: 
  
  void insert (const value_type& entry);
  // postcondition: entry is in the set

  void remove (const value_type& entry);
// postcondition: entry is not in the set

  size_type size() const;
// postcondition: number of elements in the set has been returned

  bool contains (const value_type& entry) const;
// postcondition: whether entry is in the set has been returned

  friend set set_union (const set& s1, const set& s2);
  //postcondition: union of s1 & s2 has been returned

  friend set set_intersection (const set& s1, const set& s2);
  // postcondition: intersection of s1 & s2 has been returned

  friend set set_difference (const set& s1, const set& s2);
// postcondition: difference of s1 - s2 has been returned

  friend bool is_subset (const set& s1, const set& s2);
// postcondition: returned whether s1 is a subset of s2

  friend bool operator == (const set& s1, const set& s2);
  // postcondition: returned whether s1 & s2 are equal

  friend std::ostream& operator << (std::ostream& output, const set& s);
// postcondition: s has been displayed on output

private:
  size_type find (const value_type& entry) const;
  // returned location of entry in the set if entry is in the set - used otherwise
  void resize (unsigned int new_size);
  value_type* data;
  size_type used;
  size_type capacity;
};


#endif

File: Test_Set.cpp

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

int main ()
{
  set s;
  assert (!s.contains (7));
  s.insert (7);
  assert (s.contains (7));
  s.remove (7);
  assert (!s.contains (7));
  
  set s1;
  s1.insert (4);
  s1.insert (5);
  s1.insert (-24);
  s1.insert (89);
  s1.insert (34);
  s1.insert (11);
  s1.insert (0);
  s1.insert (3);
  s1.insert (14);
  s1.insert (28);
  std::cout << s1 << std::endl;

  set s2;
  s2.insert (6);
  s2.insert (-5);
  s2.insert (-24);
  s2.insert (-89);
  s2.insert (34);
  s2.insert (-11);
  s2.insert (0);
  s2.insert (3);
  std::cout << s2 << std::endl;

  set s3 = set_union (s1, s2);
  assert (s3.contains (4));
  assert (s3.contains (0));
  assert (s3.contains (-5));
  std::cout << s3 << std::endl;

  set s4 = set_intersection (s1, s2);
  assert (s4.contains (34));
  assert (!s4.contains (4));
  assert (!s4.contains (-5));
  std::cout << s4 << std::endl;

  set s5 = set_difference (s1, s2);
  assert (s5.contains (4));
  assert (!s5.contains (0));
  assert (!s5.contains (-5));
  std::cout << s5 << std::endl;

  assert (is_subset (s5, s1));

  set s6(s2);
  assert (s6 == s2);
  std::cout << "all tests passed" << std::endl;
  return 0;
}

File: Set.cpp (comment before each function on this one)

#include "set.h"
#include <cassert>
#include<iostream>
using namespace std;

set::set(size_type initial_capacity)
{
capacity = initial_capacity;
used = 0;
data = new value_type[capacity];
}

set::set (const set& source)
{
capacity = source.capacity;
used = source.used;
data = new value_type[capacity];
for(int i = 0; i < used; i++)
data[i] = source.data[i];
}

set::~set()
{
capacity = 0;
used = 0;
delete data;
}

void set::insert (const value_type& entry)
{
bool found = false;
for(int i = 0; i < used; i++)
if(data[i] == entry)
{
found = true;
break;
}
if(!found && used < capacity)
{
data[used] = entry;
used++;
}
}

void set::remove (const value_type& entry)
{
int pos = -1;
for(int i = 0; i < used; i++)
if(data[i] == entry)
{
pos = i;
break;
}
for(int i = pos; i < used-1; i++)
data[i] = data[i+1];
used--;   
}

set::size_type set::size() const
{
return used;
}

bool set::contains (const value_type& entry) const
{
for(int i = 0; i < used; i++)
if(data[i] == entry)
return true;
return false;
}

set set_union (const set& s1, const set& s2)
{
set temp;
for(int i = 0; i < s1.size(); i++)
temp.insert(s1.data[i]);
for(int i = 0; i < s2.size(); i++)
temp.insert(s2.data[i]);
return temp;
}

set set_intersection (const set& s1, const set& s2)
{
set temp;
for(int i = 0; i < s1.size(); i++)
if(s2.contains(s1.data[i]))
temp.insert(s1.data[i]);
return temp;
}

set set_difference (const set& s1, const set& s2)
{
set temp;
for(int i = 0; i < s1.size(); i++)
if(!s2.contains(s1.data[i]))
temp.insert(s1.data[i]);
return temp;
}

bool is_subset (const set& s1, const set& s2)
{
for(int i = 0; i < s1.size(); i++)
if(!s2.contains(s1.data[i]))
return false;
return true;
}

bool operator == (const set& s1, const set& s2)
{
if(is_subset(s1, s2) && is_subset(s2, s1))
return true;
return false;
}

std::ostream& operator << (std::ostream& output, const set& s)
{
output << "{ ";
for(int i = 0; i < s.size()-1; i++)
output <<s.data[i]<<", ";
output<<s.data[s.size()-1]<<" }";
return output;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

AS PER THE TIME I HAVE I HAVE SOLVED FIRST TWO PARTS PLEASE REPOST REMAINING

IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE THERE TO HELP YOU

ANSWER:

CODE:

set.h

This class models a mathematical set.

*/

#ifndef _SET_H

#define _SET_H

#include <cstdlib>

#include <iostream>

class set {

public:

typedef int value_type;

typedef std::size_t size_type;

static const size_type CAPACITY = 30;

set();

// postcondition: empty set has been created

void insert(const value_type &entry);

// precondition: if entry is not in the set, set is not full

// postcondition: entry is in the set

void remove(const value_type &entry);

// postcondition: entry is not in the set

size_type size() const;

// returned: number of elements in the set

bool contains(const value_type &entry) const;

// returned: whether entry is in the set

friend set set_union(const set &s1, const set &s2);

// returned: union of s1 & s2

friend set set_intersection(const set &s1, const set &s2);

// returned: intersection of s1 & s2

friend set set_difference(const set &s1, const set &s2);

// returned: difference of s1 - s2

friend bool is_subset(const set &s1, const set &s2);

// returned: whether s1 is a subset of s2

friend bool operator==(const set &s1, const set &s2);

// returned: whether s1 & s2 are equal

friend std::ostream &operator<<(std::ostream &output, const set &s);

// postcondition: s has been displayed on output

private:

size_type find(const value_type &entry) const;

// returned location of entry in the set if entry is in the set - used

// otherwise

value_type data[CAPACITY];

size_type used;

};

#endif

set.cpp

#include "set.h"

set::size_type set::find(const value_type &entry) const

{

for (int i = 0; i < used; i++)

{

if (data[i] == entry)

{

return i;

}

}

return used;

}

// returned location of entry in the set if entry is in the set - used

// otherwise

set::set()

{

used = 0;

}

// postcondition: empty set has been created

void set::insert(const value_type &entry)

{

if (!contains(entry) && used < CAPACITY)

{

data[used++] = entry;

}

}

// precondition: if entry is not in the set, set is not full

// postcondition: entry is in the set

void set::remove(const value_type &entry)

{

int loc = find(entry);

if (loc != used)

{

for (int i = loc; i < used; i++)

{

data[i] = data[i + 1];

}

used--;

}

}

// postcondition: entry is not in the set

set::size_type set::size() const

{

return used;

}

// returned: number of elements in the set

bool set::contains(const value_type &entry) const

{

if (find(entry) != used)

return true;

return false;

}

// returned: whether entry is in the set

set set_union(const set &s1, const set &s2)

{

set temp;

for (int i = 0; i < s1.size(); i++)

temp.insert(s1.data[i]);

for (int i = 0; i < s2.size(); i++)

temp.insert(s2.data[i]);

return temp;

}

// returned: union of s1 & s2

set set_intersection(const set &s1, const set &s2)

{

set temp;

for (int i = 0; i < s1.size(); i++)

{

if (s2.contains(s1.data[i]))

{

temp.insert(s1.data[i]);

}

}

return temp;

}

// returned: intersection of s1 & s2

set set_difference(const set &s1, const set &s2)

{

set temp;

for (int i = 0; i < s1.size(); i++)

{

if (!s2.contains(s1.data[i]))

{

temp.insert(s1.data[i]);

}

}

return temp;

}

// returned: difference of s1 - s2

bool is_subset(const set &s1, const set &s2)

{

for (int i = 0; i < s1.size(); i++)

{

if (!s2.contains(s1.data[i]))

return false;

}

return true;

}

// returned: whether s1 is a subset of s2

bool operator==(const set &s1, const set &s2)

{

if (s1.size() == s2.size())

{

for (int i = 0; i < s1.size(); i++)

{

if (!s1.contains(s2.data[i]))

return false;

}

return true;

}

return false;

}

// returned: whether s1 & s2 are equal

std::ostream &operator<<(std::ostream &output, const set &s)

{

for (int i = 0; i < s.size(); i++)

{

output << s.data[i] << " ";

}

output << std::endl;

return output;

}

test_set.cpp

#include "set.h"

#include <cassert>

#include <iostream>

int main()

{

set s;

assert(!s.contains(7));

s.insert(7);

assert(s.contains(7));

s.remove(7);

assert(!s.contains(7));

set s1;

s1.insert(4);

s1.insert(5);

s1.insert(-24);

s1.insert(89);

s1.insert(34);

s1.insert(11);

s1.insert(0);

s1.insert(3);

s1.insert(14);

s1.insert(28);

std::cout << s1 << std::endl;

set s2;

s2.insert(6);

s2.insert(-5);

s2.insert(-24);

s2.insert(-89);

s2.insert(34);

s2.insert(-11);

s2.insert(0);

s2.insert(3);

std::cout << s2 << std::endl;

set s3 = set_union(s1, s2);

assert(s3.contains(4));

assert(s3.contains(0));

assert(s3.contains(-5));

std::cout << s3 << std::endl;

set s4 = set_intersection(s1, s2);

assert(s4.contains(34));

assert(!s4.contains(4));

assert(!s4.contains(-5));

std::cout << s4 << std::endl;

set s5 = set_difference(s1, s2);

assert(s5.contains(4));

assert(!s5.contains(0));

assert(!s5.contains(-5));

std::cout << s5 << std::endl;

assert(is_subset(s5, s1));

std::cout << "all tests passed" << std::endl;

return 0;

}

HOPE IT HELPS YOU

RATE THUMBSUP PLEASE

Add a comment
Know the answer?
Add Answer to:
The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The goal of this task is to reinforce the implementation of container class concepts using linked...

    The goal of this task is to reinforce the implementation of container class concepts using linked lists. Specifically, the task is to create an implementation file using a linked list. You need to use the header files, set3.h and node1.h, and the test program, test_set3.cpp. Your documentation must include the efficiency of each function. Please make your program as efficient and reusable as possible. set3.h #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type;...

  • C++ Create a static implementation of a set. Add the efficiency of each function to the...

    C++ Create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Use test_set.cpp as your test program. Header: /************************************ This class models a mathematical set. */ #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type; typedef std::size_t size_type; static const size_type CAPACITY = 30; set(); // postcondition: empty set has been created void insert (const value_type& entry); // precondition: if entry is not in...

  • c++ Sequence class is a class that supports sequence of integers. Run the project ad verify...

    c++ Sequence class is a class that supports sequence of integers. Run the project ad verify that project works for integers. Next convert your sequence class into a template class. Demonstrate in your main function by using a sequence of int, float, string. sequence.cxx #include // Provides assert using namespace std; namespace main_savitch_3 { sequence::sequence() { used = 0; current_index = 0; } void sequence::start() { current_index = 0; } void sequence::advance() // Library facilities used: assert.h { assert(is_item()); current_index++;...

  • The purpose of this lab is to help reinforce container class concepts and linked list concepts...

    The purpose of this lab is to help reinforce container class concepts and linked list concepts in C++. Specifically, the lab to repeat the sequence lab from last week except to use a linked list. You need to use the author's files sequence3.h and sequence_exam3.cpp. Author - Michael Main, Book - Data Structures and other objects using c++, 4th edition // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is the header file for the...

  • PLEASE HURRY. Below is the prompt for this problem. Use the code for bag1.cxx, bag1.h and...

    PLEASE HURRY. Below is the prompt for this problem. Use the code for bag1.cxx, bag1.h and my code for bag.cpp. Also I have provided errors from linux for bag.cpp. Please use that code and fix my errors please. Thank you The goal of assignment 3 is to reinforce implementation of container class concepts in C++. Specifically, the assignment is to do problem 3.5 on page 149 of the text. You need to implement the set operations union, intersection, and relative...

  • The purpose of this program is to help reinforce container class concepts and linked list concepts...

    The purpose of this program is to help reinforce container class concepts and linked list concepts in C++. Specifically, the task is to implement the sequence class using a linked list. You need to use the author's file sequence3.h to create your implementation file named sequence3.cpp. Please make your code as efficient and reusable as possible. Please make sure code can pass any tests. sequence3.h // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is...

  • Was converting the main into a template but I am recieving errors, what should I be...

    Was converting the main into a template but I am recieving errors, what should I be doing? #include<iostream> #include <cstdlib> #include "set.h" using namespace std; int main() {    set<Item> list;    list.insert(4);    list.insert(7);    list.insert(9);    list.insert(3); cout<< "The list contains "<<list.contains(4)<<" number of fours"<<endl;    list.erase(4); cout<< "The list contains "<<list.contains(4)<<" number of fours"<<endl; } #ifndef SET_H #define SET_H #include<cstdlib> namespace std {    template<class Item> class set_item { public: typedef Item value_type; set_item( ) { next...

  • Use a B-Tree to implement the set.h class. #ifndef MAIN_SAVITCH_SET_H #define MAIN_SAVITCH_SET_H #include <cstdlib> // Provides...

    Use a B-Tree to implement the set.h class. #ifndef MAIN_SAVITCH_SET_H #define MAIN_SAVITCH_SET_H #include <cstdlib> // Provides size_t namespace main_savitch_11 { template <class Item> class set { public: // TYPEDEFS typedef Item value_type; // CONSTRUCTORS and DESTRUCTOR set( ); set(const set& source); ~set( ) { clear( ); } // MODIFICATION MEMBER FUNCTIONS void operator =(const set& source); void clear( ); bool insert(const Item& entry); std::size_t erase(const Item& target); // CONSTANT MEMBER FUNCTIONS std::size_t count(const Item& target) const; bool empty( ) const...

  • C++ programming language: In this program you will create a simplified bag that acts like a...

    C++ programming language: In this program you will create a simplified bag that acts like a stack meaning that the Last item inserted is the First Item that comes out. Your backend implementation must use a linked list. The code should pass the test (there's only 1) and there should be no memory leaks. Note that passing the test does not ensure full credit! The functions are listed in the suggested order of implementation but you may implement them in...

  • Please Help This: please follow style guidelines Rule 1: use const where appropriate Rule 2: every...

    Please Help This: please follow style guidelines Rule 1: use const where appropriate Rule 2: every member function must include a description in the header file including precondition and postcondition. Rule 3: every member variable must include a description of what the variable holds. Rule 4: Classes should be capitalized. Bag, not bag. Person, not person. (I know this is different than how the book does it) Rule 5: member variables of classes should be preceded by “m_”. If it’s...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT