Question
C++ Language ** Dynamic Memory ** There are 7 files in this project, and they are down below. And I guess the professor wants us
C++ Language
** Dynamic Memory **
There are 7 files in this project, and they are down below. And I guess the professor wants us to modify the set.cpp make sure the numbers are entered by the user to be set dynamically. If you pay attention to the set.cpp I marked in bold a section that may be needed to be changed. I have put all the codes down so that you can copy them to your computer to have an idea of how they work together. Only that Set.cpp must be modified so the number of elements can be set dynamically during the program's execution.
main.cpp
input.cpp
input.h
set.cpp
set.h
srchsort.cpp
srchsort.h
** Note: The goal of this assignment is to MODIFY the existing code. **
Implement a dynamically allocated version of the mathematical concept of a 'set'. First, examine the provided library and driver (in main) for a statically allocated Set class. You may also want to refresh your mathematical memory of the set concept before proceeding.
Now that you are familiar with how the Set class works, let's make it work better. Currently, the user is limited to a certain maximum number of elements in their Set. Change it so the number of elements can be set dynamically during the program's execution.
Extend the driver to test all the ADT's operations.
** Things to Consider **
1. If your set is implemented in dynamic memory:
1a. How do you access the members?
1b. How and when do you re-size the memory?
HERE are the CODE files:
main.cpp
#include
#include "input.h" #include "set.h"
using namespace std;
int main(void) { Set x, y, z; bool quit; long newone;
do { cout > newone; if (x.ismember(newone)) { cout
cout
cout
cout
cout
cout
Input.cpp
#include "input.h"
#include
using namespace std;
// Boundaries are assumed to be solid -- low
// Enum to say which end is to be bounded... // // enum BoundType { Low, High, Both };
// Bounded entry function for long integers... // long get_bounded(long low, long high, const char prompt[] /* = "Enter bounded value: " */, BoundType which_end /* = Both */) { long value; cout > value; while ((((which_end == Low) || (which_end == Both)) && (low > value)) || (((which_end == High) || (which_end == Both)) && (value > high))) { cout > value; } return value; }
// Overloaded for double data... // double get_bounded(double low, double high, const char prompt[] /* = "Enter bounded value: " */, BoundType which_end /* = Both */) { double value; cout > value; while ((((which_end == Low) || (which_end == Both)) && (low > value)) || (((which_end == High) || (which_end == Both)) && (value > high))) { cout > value; } return value; }
// Limiting a character entry to one of a few... // char get_in_set(const char set[] /* = "YyNn" */, const char prompt[] /* = "Shall we play a game? " */, const char errmsg[] /* = " Invalid entry..." "try again, schmuck! " */) { char response; cout > response; while (strchr(set, response) == nullptr) { cout > response; } return response; }
// look at the next non-whitespace character // char peek(istream & in) { while (isspace(in.peek())) { in.ignore(); } return (char)in.peek(); }
set.cpp
#include "set.h"
#include
#include "srchsort.h" #include "input.h"
using namespace std;
long Set::find(const long item) const { return bin_search(the_set, 0, cur_elem-1, item); }
void Set::arrange(void) { sort(the_set, 0, cur_elem-1); return; }
bool Set::add_elem(const long item) { bool worked = false; if (cur_elem 1) { arrange(); rem_dupl(); } } return worked; }
void Set::rem_elem(const long item, const long from_where) { for (long i = from_where; i
Set::Set(void) : cur_elem(0) { }
bool Set::ismember(const long item) const { return (find(item) >= 0); }
bool Set::union_with(const Set & set) { for (long i = 0; i
bool Set::difference(const Set & set) { for (long i = 0; i
// this is TERRIBLY inefficient!!! MUST fix someday... bool Set::intersection(const Set & set) { Set t(*this); // my twin? t.difference(set); // remove common parts difference(t); // now remove rest from me return true; }
bool Set::rem_elem(const long item) { long at; if ((at = find(item)) >= 0) { rem_elem(item, at); } return (at >= 0); }
void Set::output(ostream & out) const { long i; out
void Set::input(istream & in) { char brace; long i; in >> brace; i = 0; while ((i > the_set[i++]; } in >> brace; cur_elem = i; arrange(); rem_dupl(); return; }
void Set::rem_dupl(void) { long i = 0; while (i
bool Set::full(void) const { return (cur_elem == MAX_SET); }
set.h
#ifndef SET_HEADER_INCLUDED #define SET_HEADER_INCLUDED
#include
const long MAX_SET = 50;
class Set { long the_set[MAX_SET]; long cur_elem;
long find(const long item) const; void arrange(void); void rem_elem(const long item, const long fromwhere); void rem_dupl(void);
public: Set(void);
bool ismember(long item) const;
bool union_with(const Set & set); bool intersection(const Set & set); bool difference(const Set & set); bool add_elem(const long item); bool rem_elem(const long item);
bool full(void) const;
void input(std::istream & in); void output(std::ostream & out) const; };
#endif
C input.h > ... C srchsort.h >
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started