Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#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

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

Step: 3

blur-text-image

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

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

Students also viewed these Databases questions