I\'m having trouble with inserting and removing a element from a dynamic array.
ID: 3837912 • Letter: I
Question
I'm having trouble with inserting and removing a element from a dynamic array. Also, i am having issues obtaining a list of elements with an array and checking to see if it is in the array or not. Everything is in c++.
Here is my code:
#include
#include
using namespace std;
const int MIN = 0;
const int MAX = 255;
class IntegerSet
{
private:
int *aPtr;
int array;
public:
IntegerSet();
IntegerSet(int m);
IntegerSet(int arr[], int n);
int elements (int arr[]) const;
bool isElem(int m) const;
IntegerSet &insert(int k); //allow cascading calls
IntegerSet &remove(int k);
friend IntegerSet operator+(const IntegerSet s1, const IntegerSet s2);
friend IntegerSet operator*(const IntegerSet s1, const IntegerSet s2);
friend bool operator<=(const IntegerSet s1, const IntegerSet s2);
friend bool operator>=(const IntegerSet s1, const IntegerSet s2);
friend ostream &operator<<(ostream &out, const IntegerSet s);
friend istream &operator>>(istream &in, IntegerSet s);
IntegerSet operator=(const IntegerSet &s);
IntegerSet(const IntegerSet &s);
~IntegerSet(){if (array>0) delete [] aPtr;}
};
IntegerSet::IntegerSet()
{
array = MIN;
aPtr = new int[array];
for(int i = 0; i {
aPtr[i] = 0;
}
}
IntegerSet::IntegerSet(int m)
{
array = m + 1;
aPtr = new int [array];
for(int i = 0; i {
aPtr[i] = 0;
}
}
IntegerSet::IntegerSet( int arr[], int n)
{
array = n;
aPtr = new int[n];
for (int i = 0; i {
aPtr[i] = 0;
}
for (int j = 0; j {
if (arr[j]<=MAX && arr[j]>=0)
{
aPtr[arr[j]] = 1;
}
}
}
int IntegerSet::elements (int arr[]) const
{
return arr[255];
}
bool IntegerSet::isElem(int m) const
{
if(m>255)
{
return false;
}
else
{
return true;
}
}
IntegerSet& IntegerSet::insert(int k)
{
array[k] = 1;
IntegerSet temp = new IntegerSet(a);
return temp;
}
IntegerSet& IntegerSet::remove(int k)
{
array[k] = 0;
IntegerSet temp = new IntegerSet(a);
return temp;
}
IntegerSet operator+(const IntegerSet s1, const IntegerSet s2)
{
return IntegerSet(s1.array + s2.array);
}
IntegerSet operator*(const IntegerSet s1, const IntegerSet s2)
{
return IntegerSet(s1.array * s2.array);
}
bool operator<=(const IntegerSet s1, const IntegerSet s2)
{
return s1.array <= s2.array;
}
bool operator>=(const IntegerSet s1, const IntegerSet s2)
{
return s1.array >= s2.array;
}
ostream &operator<<(ostream &out, const IntegerSet s)
{
int value = 0;
out<<"{";
for(int i = 0; i<=s.array; i++)
{
if(s.aPtr[i] == 1)
{
if(value == 0)
{
out< value++;
}
}
else
{
out<<", "< value++;
}
}
out<<"}";
return out;
}
istream &operator>>(istream &in, IntegerSet s)
{
int n = 0, j = 0;
while(n !=-1 && j<=MAX)
{
in>>n;
if(n=0)
{
if(n>=s.array)
{
int *tempPtr = 0;
tempPtr = new int[n + 1];
for(int i=0; i {
tempPtr[i] = 0;
if(s.aPtr[i] == 1)
{
tempPtr[i] = 1;
}
}
delete [] s.aPtr;
s.aPtr = tempPtr;
s.array = n + 1;
}
s.aPtr[n] = 1;
}
j++;
}
return in;
}
IntegerSet IntegerSet:: operator=(const IntegerSet &s)
{
if(array > 0)
{
delete [] aPtr;
}
array = s.array;
aPtr = new int[array];
for (int i = 0; i {
aPtr[i] = s.aPtr[i];
}
return *this;
}
IntegerSet::IntegerSet(const IntegerSet &s)
{
array = s.array;
aPtr = new int [array];
for(int i = 0; i {
aPtr[i] = s.aPtr[i];
}
}
void print1(IntegerSet s); // pass by value
void print2(IntegerSet &s); // pass by reference
void print3(const IntegerSet &s); // pass by reference with const
int main()
{
int arr1[6] = {5, 2, 20, 4, 13, 256};
int arr2[256]; // can hold up to 256 elements
int count;
IntegerSet s1; // {}
IntegerSet s2(2); // {2}
IntegerSet s3(arr1, 6); // {2 4 5 13 20}, 256 was ignored
IntegerSet s4 = s1; // {}
IntegerSet s5 = s2; // {2}
cout << "TC1: s1 should be empty ";
cout << "s1: " << s1 << endl; // s1: {}
cout << "TC2: s3 should have 2, 4, 5, 13, and 20 ";
s3.remove(500); // ignore since it is not a valid element
cout << "remove an illegal element (500) so ignore completely ";
cout << "s3: " << s3 << endl; // s3: {2 4 5 13 20}
cout << "TC3: s5 should have 2 ";
cout << "s5: " << s5 << endl; // s5: {2}
s2.insert(7); // s2 is now {2 7}
cout << "TC4: insert 7 into s2 so s2 should have 2 and 7 ";
cout << "s2: " << s2 << endl; // s2: {2 7}
s4 = s2 * s3; // s4 is now {2}, s2 and s3 are still the same
cout << "TC5: intersection of s2 and s3 should be 2 ";
cout << "s4: " << s4 << endl; // s4: {2}
cout << "TC6: input 5 300 255 so s5 should have 5 and 255 ";
cout << "Enter values for a set (-1 to stop): ";
cin >> s5; // enter: 5 300 255 -1
// s5 is now {5, 255}, 300 was ignore
cout << "s5: " << s5 << endl; // s5: {5 255}
cout << "TC7: cascading calls ";
s3.remove(7).remove(2); // s3 is now {4 5 13 20}
// 2 was removed and 7 is not in the set
s3.insert(99).insert(5);// s3 is now {4 5 13 20 99}
// 99 was inserted and 5 is already in the set
cout << "removed 2 and inserted 99 so s3 should have 4, 5, 13, 20, and 99 ";
cout << "s3: " << s3 << endl; // s3: {4 5 13 20 99}
cout << "TC8: getting the count (5) and elements for s3 (4 5 13 20 99) ";
count = s3.elements(arr2);
cout << "count: " << count << endl;
cout << "Element(s): ";
for (int i = 0; i < count; i++)
cout << arr2[i] << " ";
cout << endl;
cout << "TC9: s4 should not be a subset of s3 ";
if (s4 <= s3) // s4 is not a subset of s3
cout << "s4 is a subset of s3 ";
else
cout << "s4 is not a subset of s3 ";
cout << "TC10: assign s3 to s4 ";
s4 = s3; // s4: {4 5 13 20 99}
cout << "s4 now is a subset of s3 ";
if (s4 <= s3) // s4 is now a subset of s3
cout << "s4 is a subset of s3 ";
else
cout << "s4 is not a subset of s3 ";
cout << "TC11: passing object and 0 should not be in s3 ";
print1(s3);
print2(s3);
print3(s3);
// Create more objects and test cases below
return 0;
}
void print1(IntegerSet s)
{
if (s.isElem(0))
cout << "print1 (pass by value): 0 is in the set." << endl;
else
cout << "print1 (pass by value): 0 is not in the set." << endl;
}
void print2(IntegerSet &s)
{
if (s.isElem(0))
cout << "print2 (pass by reference): 0 is in the set." << endl;
else
cout << "print2 (pass by reference): 0 is not in the set." << endl;
}
void print3(const IntegerSet &s)
{
if (s.isElem(0))
cout << "print3 (pass by reference with const): 0 is in the set." << endl;
else
cout << "print3 (pass by reference with const): 0 is not in the set." << endl;
}
Explanation / Answer
//The purpose of this program is to create a function that can add or remove elements to an array #include #include #include using namespace std; //Function Prototypes string* addEntry(string *dynamicArray, int &size, string newEntry); string* deleteEntry(string *dynamicArray, int &size, string entryToDelete); int main() { int size=5; string *strPtr=new string[size]; int i=0; strPtr[i++] = "Frink"; strPtr[i++] = "Wiggum"; strPtr[i++] = "Nahasapeemapetilon"; strPtr[i++] = "Quimby"; strPtr[i++] = "Flanders"; coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.