(Solved) : Modify C Code Convert Bag Dynamic Array Show Screenshot Sample Outputs Bagh Pragma Class B Q37238770 . . .
Modify this C++ code to convert this bag into a dynamic array.Show screenshot of sample outputs.
–Bag.h–
#pragma once
class Bag
{
public:
static const int CAPACITY = 10;
Bag();
void insert(int item);
void removeOne(int item);
void removeAll(int item);
int occurrences(int item);
int size();
void display();
private:
int count;
int data[CAPACITY];
};
–Bag.cpp–
#include <iostream>
#include “Bag.h”
using namespace std;
Bag::Bag()
{
count = 0;
}
void Bag::insert(int item)
{
if (count < CAPACITY)
{
data[count] = item;
count++;
}
}
int Bag::occurrences(int item)
{
int counter = 0;
for (int i = 0; i < count; i++)
{
if (data[i] == item)
counter++;
}
return counter;
}
void Bag::removeOne(int item)
{
int index = 0;
while (index < count && data[index] !=item)
index++;
if (index < count)
{
data[index] = data[count -1];
count–;
}
}
void Bag::removeAll(int item)
{
int index = 0;
int allRemoved = 0;
while (index < count)
{
if (data[index] == item)
{
count–;
data[index] =data[count];
allRemoved++;
}
else
index++;
}
}
int Bag::size()
{
return count;
}
void Bag::display()
{
for (int i = 0; i < count; i++)
cout << data[i] << “”;
cout << endl;
}
–BagTest.cpp–
#include <iostream>
#include “Bag.h”
using namespace std;
void getAges(Bag& ages)
{
int userInput;
cout << “Type the ages in your family.”<< endl;
cout << “Type a negative number when you aredone.” << endl;
cin >> userInput;
while (userInput >= 0)
{
if (ages.size() <ages.CAPACITY)
{
ages.insert(userInput);
cout <<“There’s ” << ages.size() << ” in the bag.” <<endl;
}
else
cout <<“Bag is full. Cannot add age.” << endl;
ages.display();
cin >> userInput;
}
}
void checkAges(Bag& ages)
{
int userInput;
cout << “Type those ages again. Press returnafter each age: ” << endl;
while (ages.size() > 0)
{
cin >> userInput;
if (ages.occurrences(userInput)> 0)
{
ages.removeOne(userInput);
cout <<“Yes, I’ve found that age and removed it.” << endl;
}
else
cout <<“No, that age does not occur.” << endl;
ages.display();
}
}
int main()
{
Bag* ages = new Bag();
getAges(*ages);
checkAges(*ages);
cout << “May your family live long andprosper.” << endl;
// keep this stuff at the end of main
cout << endl;
system(“pause”);
return 0;
}
Expert Answer
Answer to Modify this C++ code to convert this bag into a dynamic array. Show screenshot of sample outputs. –Bag.h– #pragma once…
OR