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():
![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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fe469835a-7f6b-4a27-ae36-37611d103d0e%2F372274fa-26e5-470a-b5a9-eebf846c3e6f%2Furr7219_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
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 <
#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;
}
so. I input:
#include <iostream>
#include <
#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](https://content.bartleby.com/qna-images/question/514efe44-53c7-46d6-9d92-bcb4328229c6/1709a51d-4bab-474d-8ccf-8c7c7017bb5d/z3bwj5q_thumbnail.png)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)