Binary search can be implemented as a recursive algorithm. Each call makes a recursive call on one-half of the list the call received as an argument. Complete the recursive function BinarySearch() with the following specifications: 1. Parameters: o a target integer o a vector of integers o lower and upper bounds within which the recursive call will search 2. Return value: o the index within the vector where the target is located -1 if target is not found The template provides the main program and a helper function that reads a vector from input. The algorithm begins by choosing an index midway between the lower and upper bounds. 1. If target == integers.at (index) return index 2. If lower == upper, return lower if target == integers.at (lower) else-1 to indicate not found 3. Otherwise call the function recursively on half the vector parameter: o If integers.at (index) < target, search the vector from index to upper • If integers.at (index) > target, search the vector from lower to index The vector must be ordered, but duplicates are allowed. Once the search algorithm works correctly, add the following to BinarySearch():

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 5PE
icon
Related questions
Question
Answer in c++ please
miming Assignment #14
1. Parameters:
o a target integer
o a vector of integers
o lower and upper bounds within which the recursive call will search.
2. Return value:
Binary search can be implemented as a recursive algorithm. Each call makes a recursive call on one-half of the list the call received as an
argument.
Complete the recursive function BinarySearch() with the following specifications:
o the index within the vector where the target is located
o-1 if target is not found
The template provides the main program and a helper function that reads a vector from input.
The algorithm begins by choosing an index midway between the lower and upper bounds.
1. If target == integers.at (index) return index
2. If lower == upper, return lower if target == integers.at (lower) else-1 to indicate not found
3. Otherwise call the function recursively on half the vector parameter:
• If integers.at (index) < target, search the vector from index to upper
o If integers.at (index) > target, search the vector from lower to index
D
The vector must be ordered, but duplicates are allowed.
Once the search algorithm works correctly, add the following to BinarySearch():
acer
OKS Catalog
Help/FAQ
Transcribed Image Text:miming Assignment #14 1. Parameters: o a target integer o a vector of integers o lower and upper bounds within which the recursive call will search. 2. Return value: Binary search can be implemented as a recursive algorithm. Each call makes a recursive call on one-half of the list the call received as an argument. Complete the recursive function BinarySearch() with the following specifications: o the index within the vector where the target is located o-1 if target is not found The template provides the main program and a helper function that reads a vector from input. The algorithm begins by choosing an index midway between the lower and upper bounds. 1. If target == integers.at (index) return index 2. If lower == upper, return lower if target == integers.at (lower) else-1 to indicate not found 3. Otherwise call the function recursively on half the vector parameter: • If integers.at (index) < target, search the vector from index to upper o If integers.at (index) > target, search the vector from lower to index D The vector must be ordered, but duplicates are allowed. Once the search algorithm works correctly, add the following to BinarySearch(): acer OKS Catalog Help/FAQ
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

ok That solved the comiler issue. Now I am having an issue where it is failing certain tests but not others. 

I used the code above but here it is again. I am not sure if I missed something when I corrected the code that maybe I am not seeing. also I attached a screen shot of th efailed tests. 

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

vector<int> Readintegers() {
   int size; cin >> size;
   vector<int> integers(size);
   for (int i = 0; i < size; ++i) {
       cin>>integers[i];
   } 
   sort(integers.begin(), integers.end()); return integers;
   }
int recursions = 1;
int comparisons = 1;
int BinarySearch(int target, vector<int> integers, int lower, int upper) {
   if(lower>upper)
   return -1;

   int index = lower + (upper - lower) / 2;
   comparisons+=1; 
   if(integers[index]==target){
      return index;
   } 
   else if (integers[index]>target) {
      recursions++;
      return BinarySearch(target,integers, lower, index-1);//fixed this line
      //coma is missing between target and integers
   }
   else{
      recursions++;
      return BinarySearch(target,integers, index+1,upper);
   }
}

int main(int argc, char* argv[]) {
   int target;
   int index;

   vector<int> integers = Readintegers();
   cin >> target;
   index = BinarySearch(target, integers, 0, integers.size() - 1);
   printf("index: %d, recursions: %d, comparisons: %d\n",
          index, recursions, comparisons);

   return 0;
}

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

so. I input:

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

vector<int> Readintegers() {
   int size; cin >> size;
   vector<int> integers(size);
   for (int i = 0; i < size; ++i) {
   } 
   sort(integers.begin(), integers.end()); return integers;
   }
int recursions = 1;
int comparisons = 1;
int BinarySearch(int target, vector<int> integers, int lower, int upper) {
   if(lower-upper)return -1;
   int index = lower + (upper - lower) / 2;
   comparisons+=1; 
   if(integers[index]==target){
      return index;
   } 
   else if (integers[index]>target) {
      recursions++;
      return BinarySearch(target integers, lower, index-1); 
   }
   else{
      recursions++;
      return BinarySearch(target,integers, index+1,upper);
   }
}

int main(int argc, char* argv[]) {
   int target;
   int index;

   vector<int> integers = Readintegers();
   cin >> target;
   index = BinarySearch(target, integers, 0, integers.size() - 1);
   printf("index: %d, recursions: %d, comparisons: %d\n",
          index, recursions, comparisons);

   return 0;
}

 

I am getting one error which I added as a screenshot 

main.cpp: In function 'int BinarySearch(int, std::vector<int>, int, int)':
main.cpp:24:33: error: expected ')' before 'integers'
24 |
return BinarySearch (target integers, lower, index-1);
)
main.cpp:24:44: error: could not convert 'lower' from 'int' to 'std::vector<int>'
24 |
return BinarySearch (target integers, lower, index-1);
I
int
Transcribed Image Text:main.cpp: In function 'int BinarySearch(int, std::vector<int>, int, int)': main.cpp:24:33: error: expected ')' before 'integers' 24 | return BinarySearch (target integers, lower, index-1); ) main.cpp:24:44: error: could not convert 'lower' from 'int' to 'std::vector<int>' 24 | return BinarySearch (target integers, lower, index-1); I int
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Quicksort
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