38.0k views
0 votes
Imagine you just left a store with a bag of groceries. You are concerned that the fragile items will not survive the trip home, so when you reach your car, you place those items into their own bag. Using the templated version of the Bag class from Chapter 1 (one file version with header and implementation combined that is posted on Blackboard) write C++ statements that remove all the fragile items (eggs and bread) from storeBag place them into fragileBag. Print the contents of both bags using a method named displayBagContents that you added to the Bag class.

Again suppose that a bag contains strings that represent various grocery items. Write a 2 argument C++ function that removes and counts all occurrences of a given string from such a bag. Your function should return the number of items removed. Handle the possibility that the given bag is either empty or does not contain any occurrences of the given string. Print the count returned and the contents of the bag after the function executes displayBagContents member function that you added to the Bag class.

User Riklund
by
6.1k points

2 Answers

5 votes

Final answer:

To remove fragile items from the store bag and place them in a fragile bag, use the templated Bag class in C++. Create two Bag instances, iterate through the items in the store bag, and move fragile items to the fragile bag. For removing and counting occurrences of a given string in a bag, create a 2-argument function. Iterate through the items, check for matches, remove them, and increment a count variable.

Step-by-step explanation:

To remove all the fragile items (eggs and bread) from the store bag and place them into the fragile bag, you can use the templated version of the Bag class from Chapter 1 in C++. First, create two instances of the Bag class, storeBag and fragileBag. Then, iterate through the items in the storeBag, checking if each item is fragile. If an item is fragile, remove it from the storeBag and insert it into the fragileBag. Finally, use the displayBagContents method to print the contents of both bags.

To remove and count all occurrences of a given string from a bag, you can create a 2-argument function. The function should take the bag and the string as parameters. Initialize a count variable to keep track of the number of items removed. Then, iterate through the items in the bag and check if each item matches the given string. If there is a match, remove the item from the bag and increment the count. After executing the function, use the displayBagContents method to print the count and the updated contents of the bag.

User Aman Bhardwaj
by
5.1k points
1 vote

Answer:

Step-by-step explanation:

#include <iostream>

using namespace std;

#include <iostream> // For cout and cin

#include <string> // For string objects

#include "Bag.h" // For ADT bag

using namespace std;

int main()

{

int y=0;

string clubs[] = { "eggs", "eggs", "eggs", "orange", "bread", "bread" };

Bag<string> grabBag;

Bag<string> fragileBag;

for (int x = 0; x < 6; x++) {

grabBag.add(clubs[x]);

}

int count = 0;

for (int q = 0; q < 6 ; q++)

{

if (clubs[y] == "eggs" || clubs[y] == "bread") {

fragileBag.add(clubs[y]);

if(grabBag.remove("eggs")){

++count;

}

}

y++;

}

cout << "fragile bag contains:" << endl;

fragileBag.displayBagContents();

cout << "grocery bag contains:" << endl;

grabBag.displayBagContents();

cout << "Number of items removed from grocery bag : " << count<<endl;

return 0;

};

User ThaJeztah
by
4.9k points