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...

  • 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...

  • 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;                                ...

  • 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,...

  • Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch...

    Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...

  • Please help with my C++ homework! 1. The following function is supposed to perform binary search....

    Please help with my C++ homework! 1. The following function is supposed to perform binary search. It has no errors and will execute correctly. int binarySearch(int array[], int size, int value) {    int first = 0,             // First array element        last = size - 1,       // Last array element        middle,                // Mid point of search        position = -1;         // Position of search value    bool found = false;        // Flag    middle = (first + last)...

  • Show the steps of the binary search algorithm (pseudocode is given below); low = index of...

    Show the steps of the binary search algorithm (pseudocode is given below); low = index of first item in list high = index of last item in list while low is less than or equal to high mid = index halfway between low and high if item is less than list[mid] high = mid - 1 else if item is greater than list[mid] low = mid + 1 else return mid end while return not found For each step of...

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
ADVERTISEMENT