Question

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 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

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Test:

#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;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1


#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;
}

Add a comment
Know the answer?
Add Answer to:
C++ Create a static implementation of a set. Add the efficiency of each function to the...
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 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>...

  • 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++ 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...

  • 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...

  • 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...

  • My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is...

    My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is not empty s1 (size 4): 4 6 2 17 s1 is not empty Testing copy constructor s1 (size 4): 4 6 2 17 s2 (size 4): 4 6 2 17 Testing clear() s1 (size 0): s2 (size 4): 0 1477251200 1477251168 1477251136 s3 (size 4): 28 75 41 36 Testing assignment operator s3 (size 4): 28 75 41 36 s4 (size 4): 28 75...

  • 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 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...

  • 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...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

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