Question

In: Computer Science

Imagine you are writing a program to manage a shopping list. Each shopping list item is...

Imagine you are writing a program to manage a shopping list. Each shopping list item is represented by a string stored in a container. Your design requires a print function that prints out the contents of the shopping list. Using a vector to hold the shopping list items, write a print function to print out the contents of a vector of strings. Test your print function with a main program that does the following:

Create an empty vector.
Append the items, "eggs," "milk," "sugar," "chocolate," and "flour" to the list. Print it.
Remove the last element from the vector. Print it.
Append the item, "coffee" to the vector. Print it.
Write a loop that searches for any item whether it is present in the vector or not.
Write a loop that searches for the item, "milk," and then remove it from the vector. (You are permitted to reorder the vector to accomplish the removal, if you want.) Print the vector.

Input:

sugar

Output:

eggs milk sugar chocolate flour

eggs milk sugar chocolate

eggs milk sugar chocolate coffee

present

eggs sugar chocolate coffee

Solutions

Expert Solution

Code:

#include <iostream>
#include<vector>
#include <string>
using namespace std;
void printShoppingList(vector<string> s)
{
    for (auto i = s.begin(); i != s.end(); ++i)   //iterate vector from start to end
        cout<< *i<<" ";                           //print each item from vector
}
int main()
{
    string str;
    cout<<"Enter what you want to search ?\n";   //get item from user for searching
    cin>>str;
    
    vector<string> s;  
    s.push_back("eggs");   //push items into vector
    s.push_back("milk");
    s.push_back("sugar");
    s.push_back("chocolate");
    s.push_back("flour");
    
    cout<<"\nInitial shopping list: \n";  //print initial shopping list
    printShoppingList(s);
    
    s.pop_back();                         //popo last item 
    cout<<"\n\nAfter removing last item from shopping list: \n";
    printShoppingList(s);
    
    s.push_back("coffee");               //append item
    cout<<"\n\nAfter appending coffee item in shopping list: \n";
    printShoppingList(s);
    
    int flag=0;                          
    cout<<"\n\nFinding "<<str<<" item in shopping list: \n";
    for (auto i = s.begin(); i != s.end(); ++i) {//iterate vector
        if(*i==str){
            flag=1;                    //item is found then set flag
            cout<<"present";
        }
    }
    if(flag==0)                     //if flag is not set then item is not found
        cout<<"Not present";
    
    cout<<"\n\nFind milk item and remove it from shopping list: \n";
    for (auto i = s.begin(); i != s.end(); ++i) {  //iterate vector
        if(*i=="milk")                          //find item is present in list
            s.erase(i);     //if present then remove it here erase() function will remove element from vector by its index
    }
    printShoppingList(s);
    
    return 0; 
}

Output:

.


Related Solutions

C++ code Inventory Item Stack You are writing an Inventory program that will allow the user...
C++ code Inventory Item Stack You are writing an Inventory program that will allow the user to enter a part into the inventory, take a part from the inventory, or quit. You are provided with the InvItem class (InvItem.h) and a partial Driver.cpp. You will be creating a DynamicStack class that should implement a Stack data structure. The DynamicClass should be implemented as a template class to allow any data type be added/removed from the stack. You will submit three...
A search for “car” on a shopping website gave a list of the following items: Item...
A search for “car” on a shopping website gave a list of the following items: Item Department Unit Price Average Reviews Hot Wheels 9-Car Gift Pack (Styles May Vary) Toy $9 5 Graco Nautilus 3-in-1 Car Seat, Matrix Baby $140 4 ... ... ... ... You can use the following example problem as a model. What are the “individuals” for this data set? 1-Kids 2-Customers 3-Items for sale on this website 4-Shopping website Which of the following statements is NOT...
You manage the shipping of item #A452 from your supplier. The shipments for this item are...
You manage the shipping of item #A452 from your supplier. The shipments for this item are delivered to the nearest port, and you have to transport them to the distribution center. You have two options, truck load (TL) and less than truck load (LTL). Your company needs 5000 units of #A452 per year and it is purchased at a price of $36 per unit. Each order costs $97. Your company uses a holding charge of 0.15, a cycle service level...
You manage the shipping of item #A452 from your supplier. The shipments for this item are...
You manage the shipping of item #A452 from your supplier. The shipments for this item are delivered to the nearest port, and you have to transport them to the distribution center. You have two options, truck load (TL) and less than truck load (LTL). Your company needs 5000 units of #A452 per year and it is purchased at a price of $36 per unit. Each order costs $90. Your company uses a holding charge of 0.1, a cycle service level...
You manage the shipping of item #A452 from your supplier. The shipments for this item are...
You manage the shipping of item #A452 from your supplier. The shipments for this item are delivered to the nearest port, and you have to transport them to the distribution center. You have two options, truck load (TL) and less than truck load (LTL). Your company needs 5000 units of #A452 per year and it is purchased at a price of $36 per unit. Each order costs $92. Your company uses a holding charge of 0.13, a cycle service level...
This is python: #Imagine you're writing a program to calculate the class #average from a gradebook....
This is python: #Imagine you're writing a program to calculate the class #average from a gradebook. The gradebook is a list of #instances of the Student object. # #You don't know everything that's inside the Student object, #but you know that it has a method called get_grade(). #get_grade() will return the average for the student #represented by a given instance of Student. # #You don't know if get_grade() is stored in memory or if #it's calculated when it's needed, but...
Imagine you are tasked with shopping for a space to open a new restaurant for your...
Imagine you are tasked with shopping for a space to open a new restaurant for your boss. You have found a target neighborhood for the restaurant with two options for location. One location is available for lease, and the other is available for purchase. How would you advise the owner of the business on the options for each restaurant location? What factors must be considered? Compare and contrast options for each location. Provide variables for the expectation of the future...
Imagine that you are an expert in Website Analytics, and you have been hired to manage...
Imagine that you are an expert in Website Analytics, and you have been hired to manage a company’s websites. A successful website is one where visitors come and spend time on the site. More time spent on the site is desirable because you can then convince advertisers to pay you to place their ads on your sites. You have been asked to analyze the performance of the following websites: 1. With 99% confidence, calculate an interval that estimates the true...
Imagine you are developing a software package for an online shopping site that requires users to...
Imagine you are developing a software package for an online shopping site that requires users to enter their own passwords. Your software requires that users' passwords meet the following criteria: The password should be at least six characters long. The password should contain at least one uppercase and at least one lowercase letter. The password should have at least one digit. Write a class that verifies that a password meets the stated criteria. Demonstrate the class in a program that...
LISP FUNCTIONS Imagine you are writing the AI for a simple game that will require an...
LISP FUNCTIONS Imagine you are writing the AI for a simple game that will require an adversarial search. The state of the game can be represented using a list of 3 elements, such as (B B B). The list representing the game state can contain any combination of 3 values: A, B, or C. It will always have exactly 3 items. (Let's call it the "state list"). With that in mind, write the following LISP functions: TERMINAL-TEST - this function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT