I need to modify the bag CLASS to use a Dynamic Array. Couple options and one of
ID: 3905718 • Letter: I
Question
I need to modify the bag CLASS to use a Dynamic Array. Couple options and one of them is to modify the BAG CLASS to contain an object of Dynamic Array Class.
Bag.h //Start
#pragma once
#include <cstdlib>
class Bag
{
public:
typedef double value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
Bag();
size_type size();
void insert(const value_type& entry);
size_type count(const value_type& target) const;
bool erase_one(const value_type& target);
size_type erase(const value_type& target);
void operator +=(const Bag& addEnd);
private:
value_type data[CAPACITY]; // array to store items
size_type used; // how much of array is used
};
Bag operator +(const Bag& b1, const Bag& b2);
Bag.ccp //Start
#include "Bag.h"
#include <cassert>
#include <algorithm>
using namespace std;
Bag::Bag()
{
used = 0;
}
bool Bag::erase_one(const value_type& target)
{
size_type index = 0;
while (index < used && data[index] != target)
index++;
if (index >= used)
return false;
used--;
data[index] = data[used];
return true;
}
void Bag::insert(const value_type& entry)
{
assert(size() < CAPACITY);
data[used] = entry;
used++;
}
Bag::size_type Bag::count(const value_type& target) const
{
size_type answer = 0;
for (unsigned int i = 0; i < used; i++)
{
if (data[i] == target)
answer++;
}
return answer;
}
void Bag::operator +=(const Bag& addEnd)
{
assert(size() + addEnd.used <= CAPACITY);
for (unsigned int i = 0; i < addEnd.used; i++)
{
data[used] = addEnd.data[i];
used++;
}
}
Bag operator +(const Bag& b1, const Bag& b2)
{
Bag answer;
//assert(b1.size() + b2.size() <= Bag::CAPACITY);
answer += b1;
answer += b2;
return answer;
}
Bag::size_type Bag::erase(const value_type& target)
{
size_type index = 0;
size_type numErased = 0;
while (index < used)
{
if (data[index] == target)
{
used--;
data[index] = data[used];
numErased++;
}
else
index++;
}
return numErased;
}
Bag::size_type Bag::size()
{
return used;
}
Bag2.ccp //Start
#include "Bag.h"
#include <iostream>
#include <cstdlib>
using namespace std;
void getAges(Bag& ages)
{
int userInput;
cout << "Type the ages in your family." << endl;
cout << "Type a negative number when you are done." << endl;
cin >> userInput;
while (userInput >= 0)
{
if (ages.size() < ages.CAPACITY)
ages.insert(userInput);
else
cout << "I have run out of room and can't add that age." << endl;
cin >> userInput;
}
}
void checkAges(Bag& ages)
{
int userInput;
cout << "Type those ages again. Press return after each age:" << endl;
while (ages.size() > 0)
{
cin >> userInput;
if (ages.erase_one(userInput))
cout << "Yes, I've found that age and removed it." << endl;
else
cout << "No, that age does not occur." << endl;
}
}
void main()
{
// First test:
Bag ages;
getAges(ages);
checkAges(ages);
cout << "May your family live long and prosper." << endl;
// Second test:
Bag testBag;
for (int i = 0; i < 30; i++)
{
double r = 1.0 + (rand() % 12);
testBag.insert(r);
}
cout << " NEXT TEST: " << endl;
cout << "Bag size: " << testBag.size() << endl;
cout << "Occurences of the number 6: " << testBag.count(6) << endl;
Bag::size_type count = testBag.erase(6);
cout << "Bag size: " << testBag.size() << endl;
cout << "Occurences of the number 6: " << testBag.count(6) << endl;
system("pause");
}
Explanation / Answer
Since dynamic memory is being used, we need to implement the copy constructor, assignment operator and destructor.
Given below is the modified code for the question.
Please do rate the answer if it was helpful. Thank you
Bag.h
=====
#pragma once
#include <cstdlib>
class Bag
{
public:
typedef double value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
Bag();
//copy constructor,assignment operator, destructor
Bag(const Bag& other);
Bag& operator=(const Bag& other);
~Bag();
size_type size();
void insert(const value_type& entry);
size_type count(const value_type& target) const;
bool erase_one(const value_type& target);
size_type erase(const value_type& target);
void operator +=(const Bag& addEnd);
private:
value_type *data; // dynamic array to store items
size_type used; // how much of array is used
};
Bag.cpp
=====
#include "Bag.h"
#include <cassert>
#include <algorithm>
using namespace std;
Bag::Bag()
{
used = 0;
data = new value_type[CAPACITY]; //allocate dynamic array
}
bool Bag::erase_one(const value_type& target)
{
size_type index = 0;
while (index < used && data[index] != target)
index++;
if (index >= used)
return false;
used--;
data[index] = data[used];
return true;
}
void Bag::insert(const value_type& entry)
{
assert(size() < CAPACITY);
data[used] = entry;
used++;
}
Bag::size_type Bag::count(const value_type& target) const
{
size_type answer = 0;
for (unsigned int i = 0; i < used; i++)
{
if (data[i] == target)
answer++;
}
return answer;
}
void Bag::operator +=(const Bag& addEnd)
{
assert(size() + addEnd.used <= CAPACITY);
for (unsigned int i = 0; i < addEnd.used; i++)
{
data[used] = addEnd.data[i];
used++;
}
}
Bag operator +(const Bag& b1, const Bag& b2)
{
Bag answer;
//assert(b1.size() + b2.size() <= Bag::CAPACITY);
answer += b1;
answer += b2;
return answer;
}
Bag::size_type Bag::erase(const value_type& target)
{
size_type index = 0;
size_type numErased = 0;
while (index < used)
{
if (data[index] == target)
{
used--;
data[index] = data[used];
numErased++;
}
else
index++;
}
return numErased;
}
Bag::size_type Bag::size()
{
return used;
}
//copy constructor
Bag::Bag(const Bag& other)
{
data = NULL;
*this = other;
}
//assignment operator
Bag& Bag::operator=(const Bag& other)
{
if(data != NULL)
delete[] data;
data = new value_type[CAPACITY];
used = other.used;
for(int i = 0; i < used; i++)
data[i] = other.data[i];
return *this;
}
Bag::~Bag()
{
delete[] data;
}
output
====
Type the ages in your family.
Type a negative number when you are done.
2
10
12
14
20
24
50
-1
Type those ages again. Press return after each age:
2
Yes, I've found that age and removed it.
10
Yes, I've found that age and removed it.
12
Yes, I've found that age and removed it.
12
No, that age does not occur.
14
Yes, I've found that age and removed it.
20
Yes, I've found that age and removed it.
24
Yes, I've found that age and removed it.
50
Yes, I've found that age and removed it.
May your family live long and prosper.
NEXT TEST:
Bag size: 30
Occurences of the number 6: 3
Bag size: 27
Occurences of the number 6: 0