String inquiredItem and integer numData are read from input. Then, numData alphabetically sorted strings are read from input and each string is appended to a vector. Complete the FindMiddle() function: If inquiredItem is alphabetically before middleValue (the element at index middleIndex of the vector), output "Search lower half" and recursively call FindMiddle() to find inquiredItem in the lower half of the range. Otherwise, output "Search upper half" and recursively call FindMiddle() to find inquiredItem in the upper half of the range. End each output with a newline.   Click here for example Ex: If the input is:   ill 8 gif gum ill old pen pit pun ten then the output is: Search lower half Search upper half ill is found at index 2 Note: string1 < string2 returns true if string1 is alphabetically before string2, and returns false otherwise.You can only change the line "Your code goes here". '' #include <iostream>#include <vector>#include <string>using namespace std; void FindMiddle(vector<string> allWords, string inquiredItem, int startIndex, int endIndex) {   int middleIndex;   int rangeSize;   string middleValue;    rangeSize = (endIndex - startIndex) + 1;   middleIndex = (startIndex + endIndex) / 2;   middleValue = allWords.at(middleIndex);    if (inquiredItem == middleValue) {      cout << inquiredItem << " is found at index " << middleIndex << endl;   }   else if (rangeSize == 1) {      cout << inquiredItem << " is not in the list" << endl;   }   else {       /* Your code goes here */    }} int main() {   string inquiredItem;   vector<string> dataList;   int numData;   int i;   string item;    cin >> inquiredItem;   cin >> numData;   for (i = 0; i < numData; ++i) {      cin >> item;      dataList.push_back(item);   }   FindMiddle(dataList, inquiredItem, 0, dataList.size() - 1);    return 0;}'''

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 6PE
icon
Related questions
Question

String inquiredItem and integer numData are read from input. Then, numData alphabetically sorted strings are read from input and each string is appended to a vector. Complete the FindMiddle() function:

  • If inquiredItem is alphabetically before middleValue (the element at index middleIndex of the vector), output "Search lower half" and recursively call FindMiddle() to find inquiredItem in the lower half of the range.
  • Otherwise, output "Search upper half" and recursively call FindMiddle() to find inquiredItem in the upper half of the range.

End each output with a newline.

 

Click here for example Ex: If the input is:

 

ill 8 gif gum ill old pen pit pun ten

then the output is:

Search lower half Search upper half ill is found at index 2

Note: string1 < string2 returns true if string1 is alphabetically before string2, and returns false otherwise.

You can only change the line "Your code goes here".


''

#include <iostream>
#include <vector>
#include <string>
using namespace std;

void FindMiddle(vector<string> allWords, string inquiredItem, int startIndex, int endIndex) {
   int middleIndex;
   int rangeSize;
   string middleValue;

   rangeSize = (endIndex - startIndex) + 1;
   middleIndex = (startIndex + endIndex) / 2;
   middleValue = allWords.at(middleIndex);

   if (inquiredItem == middleValue) {
      cout << inquiredItem << " is found at index " << middleIndex << endl;
   }
   else if (rangeSize == 1) {
      cout << inquiredItem << " is not in the list" << endl;
   }
   else {

      /* Your code goes here */

   }
}

int main() {
   string inquiredItem;
   vector<string> dataList;
   int numData;
   int i;
   string item;

   cin >> inquiredItem;
   cin >> numData;
   for (i = 0; i < numData; ++i) {
      cin >> item;
      dataList.push_back(item);
   }
   FindMiddle(dataList, inquiredItem, 0, dataList.size() - 1);

   return 0;
}
'''

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr