Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

what would be the missing code to run this program? please follow the zybook format 15.14 LAB: Binary search Binary search can be implemented as

what would be the missing code to run this program? please follow the zybook format

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
15.14 LAB: Binary search 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 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 -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 lower to index - 1 The vector must be ordered, but duplicates are allowed. Once the search algorithm works correctly, add the following to BinarySearch(): 4. Count the number of calls to BinarySearch().. 5. Count the number of times when the target is compared to an element of the vector. Note: lower == upper should not be counted. Hint: Use a global variable to count calls and comparisons. The input of the program consists of: 1. the number of integers in the vector 2. the integers in the vector 3. the target to be located CS ExI tha ingit is CamScanner 9 4 56 789Ex: If the input is: 9 1 2 3 4 5 6 7 8 9 the output is: index: 1, recursions: 2, comparisons : 3 464730.321487 4.qx3zay> Instructor note: Line 54 here uses notation from C, as opposed to C++ - printf is the C equivalent of using the cout function in C++. The "translated" line would look like: cout 2 #include 3 #include 4 using namespace std; 5 // Read integers from input and store them in a vector. 7 / / Return the vector. vector ReadIntegers( ) { 9 int size; 10 cin >> size; 11 vector integers(size) ; 12 for (int i = 0; i > integers . at(i); CS 15 se . : (integers . begir () interestno)canner 16 return integers; 17 18LAB ACTIVITY 15.14.1: LAB: Binary search 8 / 10 main.cpp Load default template... 1 #include 2 #include 3 #include using namespace std; 5 6 / / Read integers from input and store them in a vector. 7 // Return the vector. 8 vector ReadIntegers( ) { 9 int size; 10 cin >> size; 11 vector integers(size) ; 12 for (int i = 0; i > integers . at (i) ; 14 15 sort (integers . begin( ); integers . end( )) ; 16 return integers; 17 } 18 Run your program as often as you'd like, before submitting for grading. Below, type any needed Develop mode Submit mode input values in the first box, then click Run program and observe the program's output in the second box. Enter program input (optional) If your code requires input values, provide them here. main.cpp Run program Input (from above) Output (shown below) (Your program) Program output displayed here CS Scant th CamScannerLAB 8 / 10 ACTIVITY 15.14.1: LAB: Binary search main.cpp Load default template... RT 19 int BinarySearch(int target, vector integers, int lower, int upper ) { 20 /* Type your code here. 21 22 23 int main( ) { 24 int target ; 25 int index; 26 27 vector integers = ReadIntegers( ) ; 28 29 cin >> target; 30 31 index = BinarySearch(target, integers, 0, integers size( ) - 1) ; 32 printf( "index: %d, recursions: %d, comparisons: %d\ ", 33 index, recursions, comparisons ) ; 34 35 return 0; 36 3 Run your program as often as you'd like, before submitting for grading. Below, type any needed Develop mode Submit mode input values in the first box, then click Run program and observe the program's output in the second box. Enter program input (optional) If your code requires input values, provide them here, CS Scann Input (f cm above ) . .. main.cpp Output (shown below) (Your program)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions