Question

Write in C++: Please create a shopping list using the STL list container, then follow the...

Write in C++:

Please create a shopping list using the STL list container, then follow the following instructions.

  1. Create an empty list.
  2. Append the items, "eggs," "milk," "sugar," "chocolate," and "flour" to the list. Print the list.
  3. Remove the first element from the list. Print the list.
  4. Insert the item, "coffee" at the beginning of the list. Print the list.
  5. Find the item, "sugar" and replace it with "honey." Print the list.
  6. Insert the item, "baking powder" before "milk" in the list. Print the list.

======================================

Next, here is the pseudocode for a function that performs a binary search on an array:

*********************

Set first index to 0.

Set last index to the last subscript in the array.

Set found to false.

Set position to −1.

While found is not true and first is less than or equal to last

Set middle to the subscript halfway between array[first] and array[last].

If array[middle] equals the desired value

Set found to true.

Set position to middle.

Else If array[middle] is greater than the desired value

Set last to middle − 1.

Else

Set first to middle + 1.

End If.

End While.

Return position.

*********************

You may use the following numbers for your binary search.

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Shopping_list.cpp

#include<iostream>
#include <list>
#include <iterator>
#include <string>
#include <algorithm>
using namespace std;
int main(){
   list <string> slist;
   slist.push_back("eggs");
   slist.push_back("milk");
   slist.push_back("sugar");
   slist.push_back("chocolate");
   slist.push_back("flour");
   list <string> :: iterator it;
for(it = slist.begin(); it != slist.end(); ++it)
cout << *it << " ";
slist.pop_front();
cout<<endl<<"list after removing first element"<<endl;
for(it = slist.begin(); it != slist.end(); ++it)
cout << *it << " ";
slist.push_front("coffee");
for(it = slist.begin(); it != slist.end(); ++it)
cout << *it << " ";
cout<<endl;
for(it = slist.begin(); it != slist.end(); ++it){
       if(*it=="sugar"){
           *it="honey";
           break;
}
}
   for(it = slist.begin(); it != slist.end(); ++it)
cout << *it << " ";
cout<<endl;
it = std::find(slist.begin(),slist.end(), "milk");
slist.insert(it,"baking powder");
for(it = slist.begin(); it != slist.end(); ++it)
cout << *it << " ";
cout<<endl;
  
  
}

Binary_search.cpp

#include<iostream>
using namespace std;
int binarysearch(int array[],int first,int last,int value,int position){
   bool found =false;
   while(found!=true && first<=last){
       int middle = (first+last)/2;
      
       if(array[middle]==value){
           found=true;
           position=middle;
       }
       else if(array[middle]>value){
           last = middle-1;
       }
       else{
           first= middle+1;
       }
       cout<<middle<<" " <<first<<" "<<last<<endl;
   }
   return position+1;
}
int main(){
   int arr[]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
   int size = sizeof(arr) / sizeof(arr[0]);
   int first=0,last =size;
   int position =-1;
   int value =0;
   cout<<binarysearch(arr,first,last,value,position);
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write in C++: Please create a shopping list using the STL list container, then follow the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write in c++ please Imagine you are writing a program to manage a shopping list. Each...

    Write in c++ please 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: 1. Create...

  • Write a Java program that will create a random list (array) randomize a  search item to be...

    Write a Java program that will create a random list (array) randomize a  search item to be found in the list sequentially search the array for that item. You must code the search and not use a search function. Display each comparison in the array; the element contents and the index. display whether the item was found or not. Now take that code and alter it to have two lists. Both lists randomize between 1 and 50. While traversing one list,...

  • The binary search algorithm from Chapter 9 is a very efficient algorithm for searching an ordered...

    The binary search algorithm from Chapter 9 is a very efficient algorithm for searching an ordered list. The algorithm (in pseudocode) is as follows: highIndex - the maximum index of the part of the list being searched lowIndex - the minimum index of the part of the list being searched target -- the item being searched for //look in the middle middleIndex = (highIndex + lowIndex) / 2 if the list element at the middleIndex is the target return the...

  • The code below is already completed using C++. Could you please explain why the input is...

    The code below is already completed using C++. Could you please explain why the input is 3 : 2 : 3 : 6 : 4 : -1 : 3 : 1 : 0 :? Please let me know really short explanation. #include <iostream> using namespace std ; int binarySearch(const int array[], int numElems, int value) { int first = 0, // First array element last = numElems - 1, // Last array element middle, // Midpoint of search position =...

  • Language: C++ (Please show output) Part 1 - LIST Create an unsorted LIST class. Each list...

    Language: C++ (Please show output) Part 1 - LIST Create an unsorted LIST class. Each list should be able to store 100 names. Part 2 - Create a Class ArrayListClass It will contain an array of 27 "list" classes. Next, create a Class in which is composed a array of 27 list classes. Ignore index 0... Indexes 1...26 correspond to the first letter of a Last name. Again - ignore index 0. index 1 is names starting with A, index...

  • Language = c++ Write a program to find the number of comparisons using the binary search...

    Language = c++ Write a program to find the number of comparisons using the binary search and sequential search algorithms as follows: o Suppose list is an array of 1000 elements. o Use a random number generator to fill the list. o Use the function insertOrd to initially insert all the elements in the list. o You may use the following function to fill the list: void fill(orderedArrayListType& list) {       int seed = 47; int multiplier = 2743;                                ...

  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

    Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> {      /**       * Append an item to the end of the list       *       * @param item – item to be appended       */ public void append(E item);      /**       * Insert an item at a specified index position       *       * @param item – item to be...

  • C# is the language Create a generic class called “LinkedList” and implement the singly linked list....

    C# is the language Create a generic class called “LinkedList” and implement the singly linked list. Implement the following functions: - In C# Function Name Function Parameters Description Empty - Return true if empty else return false. Size - Return the current size of the list. Insert Value Insert the Value at the end of the list. Insert Index, Value Insert the Value at the specified Index. If the Index is out of range then insert the Value at the...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • Searching/sorting tasks and efficiency analysis - Binary Search Search for the character S using the binary...

    Searching/sorting tasks and efficiency analysis - Binary Search Search for the character S using the binary search algorithm on the following array of characters: A E G K M O R S Z. For each iteration of binary search use a table similar to the table below to list: (a) the left index and (b) the right index of the array that denote the region of the array that is still being searched, (c) the middle point of the array,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT