Question: #include using namespace std; /* The function binarySearch accepts a sorted array data with no duplicates, and the range within that array to search, defined

#include

using namespace std;

/*

The function binarySearch accepts a sorted array data with no duplicates,

and the range within that array to search, defined by first and last.

Finally, goal is the value that is searched for within the array.

If the goal can be found within the array, the function returns the

index position of the goal value in the array. If the goal value does

not exist in the array, the function returns -1.

*/

int binarySearch(int data[], int first, int last, int goal)

{

cout << "first: " << first << ", last: " << last << endl;

// YOU CAN ONLY ADD OR CHANGE CODE BELOW THIS COMMENT

return -1;

// YOU CAN ONLY ADD OR CHANGE CODE ABOVE THIS COMMENT

}

int main()

{

const int ARRAY_SIZE = 20;

int searchValue;

/* generates an array data that contains:

0, 10, 20, 30, .... 170, 180, 190

*/

int data[ARRAY_SIZE];

for(int i = 0; i < ARRAY_SIZE; i++)

data[i] = i * 10;

cout << "Enter Search Value: ";

cin >> searchValue;

cout << "Answer: " << binarySearch(data, 0, ARRAY_SIZE-1, searchValue) << endl;

return true;

}

output:

Enter Search Value: 165 first: 0, last: 19 first: 10, last: 19 first: 15, last: 19 first: 15, last: 16 first: 16, last: 16 first: 17, last: 16 Answer: -1 ./a.out Enter Search Value: 40 first: 0, last: 19 first: 0, last: 8 Answer: 4 ./a.out Enter Search Value: 10 first: 0, last: 19 first: 0, last: 8 first: 0, last: 3 Answer: 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!