Question: Exercise 1: Implement the binary search function from your textbook on p. 67 and add an additional referen ce parameter 'int&steps' which is counting up
Exercise 1: Implement the binary search function from your textbook on p. 67 and add an additional referen ce parameter 'int&steps' which is counting up the costs associated with the iterations needed to find the target value. When the target is found in the vector binary_search returns the index at which the target is stored; otherwise, the function returns value -1. Notice that we are using our own Vector data structure. So make sure to include "Vector.h" int binary_search (int x, const Vectorcint>& vec, int& steps) int low0 int high vec. size() - 1; steps0 while (low x) else return -1, // not found Test your function in an 'int main()' which builds up a vector of 25 arbitrary integer values in ascending order. For binary search to work, your vector must be sorted. The manner in which the binary_search function is written calls for the vector values to be in ascending order. Run binary_search for 5-10 target values, some included in the vector, some not included. Let the main() function report 1. 2. whether the value was found and at what "cost" (you get this information from the int& parameter)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
