Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This program takes inputs for adding to a bag, removing from a bag, displaying the bag, and exits the program. Whenever the user inputs to

This program takes inputs for adding to a bag, removing from a bag, displaying the bag, and exits the program. Whenever the user inputs to add something into the bag, the bag will return an integer, which is the index that the item has been stored in.

Sample output:

image text in transcribed

Removing an item from the bag takes the integer (index) as the argument, and removes it from the bag.

Sample output:

image text in transcribed

However, when the user wants to remove something from the bag that has more than one object, and another object has a higher index than the item that's removed, the items that have a higher index than the item that got removed will shift down one index to fill-in the spot. How would I be able to fix this so that those items keep the index that they were stored in, without shifting everything down?

Example:

image text in transcribed

Code:

// main.cpp:

#include #include "BagInterface.h" #include "ArrayBag.h" using namespace std;

void insert(ArrayBag& bag, string addToBag, string items[], bool inUse) { cout

vector bagItems = bag.toVector(); }

void remove(ArrayBag& bag, int removeFromBag) { vector bagItems = bag.toVector(); bag.remove(removeFromBag); cout

void display(ArrayBag& bag) { cout bagItems = bag.toVector();

int numberOfEntries = (int)bagItems.size();

for (int i = 0; i

void bagFunctions(ArrayBag& bag) { int userInputNumerical{}; // Used for navigating the interface. string addToBag; // The item that's going to be entered into the bag. int removeFromBag; bool exit = false; // True = Exit the program. False = Keep the program going.

int const bagCapacity = 6; // Assuming that the bag will be 6 throughout the project.

string items[bagCapacity]; bool inUse[bagCapacity];

cout

cin >> userInputNumerical;

if (userInputNumerical == 1) { if (bag.getCurrentSize() == 6) { cout > addToBag; insert(bag, addToBag, items, inUse); } }

if (userInputNumerical == 2) { if (bag.isEmpty()) { cout > removeFromBag; remove(bag, removeFromBag); display(bag); } }

if (userInputNumerical == 3) { display(bag); }

if (userInputNumerical == 4) { exit = true; }

if (exit) { // Program is complete. }

else { bagFunctions(bag); } }

int main() { ArrayBag bag; cout

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

// ArrayBag.cpp:

template ArrayBag::ArrayBag() : itemCount(0), maxItems(DEFAULT_CAPACITY), inUse() { }

template ArrayBag::~ArrayBag() { // delete [] inUse; // delete [] items; }

template int ArrayBag::getCurrentSize() const {

return itemCount; }

template bool ArrayBag::isEmpty() const {

return itemCount == 0; }

template int ArrayBag::add(const ItemType& newEntry) {

int emptyIndex = getNextEmptyIndex(); if (emptyIndex >= 0 && (itemCount

template ItemType ArrayBag::remove(const int receipt) {

ItemType removedObject; if (inUse[receipt]) { removedObject = items[receipt]; items[receipt] = items[itemCount - 1]; items[itemCount - 1] = ""; inUse[itemCount - 1] = false; --itemCount; } return removedObject; }

template void ArrayBag::clear() {

itemCount = 0; }

template bool ArrayBag::contains(const ItemType& anEntry) const {

bool found = false; int curIndex = 0;

while (!found && curIndex

return found; }

template int ArrayBag::getFrequencyOf(const ItemType& anEntry) const {

int frequency = 0; int curIndex = 0;

while (curIndex

return frequency; }

template vector ArrayBag::toVector() const {

vector bagContents;

for (int i = 0; i

return bagContents; }

template int ArrayBag::getIndexOf(const ItemType& target) const {

bool found = false; int result = -1; int searchIndex = 0;

while (!found && searchIndex

return result; }

template int ArrayBag::getNextEmptyIndex() { int i = 0; bool found = false; int emptyIndex = -1; while (!found) { if (inUse[i] == 0) { emptyIndex = i; found = true; } else i++; } return emptyIndex; }

template void ArrayBag::printInUse() { for (int i = 0; i

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

// ArrayBag.h:

template class ArrayBag : public BagInterface { private: /** Maximum capacity of this bag. */ static const int DEFAULT_CAPACITY = 6;

/** Number of items in this bag. */ int itemCount;

/** Maximum capacity of this bag. */ int maxItems;

/** Data storage. */

ItemType items[DEFAULT_CAPACITY]; //ItemType* items = new ItemType[DEFAULT_CAPACITY];

bool inUse[DEFAULT_CAPACITY]; int getIndexOf(const ItemType& target) const;

public: ArrayBag();

~ArrayBag();

int getCurrentSize() const;

bool isEmpty() const;

int add(const ItemType& newEntry);

ItemType remove(const int receipt);

void clear();

int getFrequencyOf(const ItemType& anEntry) const;

bool contains(const ItemType& anEntry) const;

vector toVector() const;

int getNextEmptyIndex();

void printInUse(); };

#include "ArrayBag.cpp"

#endif

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

// BagInterface.h:

#ifndef _BAG_INTERFACE #define _BAG_INTERFACE

#include

using namespace std; template class BagInterface { public:

virtual ~BagInterface() {}

virtual int getCurrentSize() const = 0;

virtual bool isEmpty() const = 0;

virtual int add(const ItemType& newEntry) = 0;

virtual ItemType remove(const int receipt) = 0;

virtual void clear() = 0;

virtual int getFrequencyOf(const ItemType& anEntry) const = 0;

virtual bool contains(const ItemType& anEntry) const = 0;

virtual vector toVector() const = 0;

virtual int getNextEmptyIndex() = 0;

virtual void printInUse() = 0; };

#endif

This program will allow you to add/remove items into a bag. What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit Type what you would like to insert: Chips Chips was added to the bag. Chips has been assigned with receipt number: 0 What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit Type what you would like to insert: Salsa Salsa was added to the bag. Salsa has been assigned with receipt number: 1 What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit 3 The_bag contains 2 items: Chips is assigned with receipt: 0 Salsa is assigned with receipt: 1 What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit Type what you would like to remove: 1 Salsa was removed from the bag. The bag and the receipt numbers are now the following: The bag contains 1 items: Chips is assigned with receipt: 0 What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit This program will allow you to add/remove items into a bag. What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit Type what you would like to insert: Chips Chips was added to the bag. Chips has been assigned with receipt number: 0 What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit 1 Type what you would like to insert: Salsa Salsa was added to the bag. Salsa has been assigned with receipt number: 1 What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit 2 Type what you would like to remove: 0 Chips was removed from the bag. The bag and the receipt numbers are now the following: The bag contains 1 items: Salsa is assigned with receipt: 0 What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit This program will allow you to add/remove items into a bag. What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit Type what you would like to insert: Chips Chips was added to the bag. Chips has been assigned with receipt number: 0 What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit Type what you would like to insert: Salsa Salsa was added to the bag. Salsa has been assigned with receipt number: 1 What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit 3 The_bag contains 2 items: Chips is assigned with receipt: 0 Salsa is assigned with receipt: 1 What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit Type what you would like to remove: 1 Salsa was removed from the bag. The bag and the receipt numbers are now the following: The bag contains 1 items: Chips is assigned with receipt: 0 What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit This program will allow you to add/remove items into a bag. What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit Type what you would like to insert: Chips Chips was added to the bag. Chips has been assigned with receipt number: 0 What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit 1 Type what you would like to insert: Salsa Salsa was added to the bag. Salsa has been assigned with receipt number: 1 What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit 2 Type what you would like to remove: 0 Chips was removed from the bag. The bag and the receipt numbers are now the following: The bag contains 1 items: Salsa is assigned with receipt: 0 What would you like to do? 1. Insert 2. Remove 3. Display 4. Exit

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions