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():

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education