Question: Open your preferred C++ IDE, and create new console project. A BinarySearch program code is given as below: #include using namespace std; class BinarySearch {

  1. Open your preferred C++ IDE, and create new console project.
  2. A BinarySearch program code is given as below:

#include

using namespace std;

class BinarySearch

{

private:

int *data;

int numData; // Number of data

public:

BinarySearch(int dataVal[], int size)

{

numData = size;

data = new int[size];

for (int i = 0; i

{

data[i] = dataVal[i];

}

}

int find(int x){

int top, bottom, middle, position;

bool exist;

exist = false;

bottom = 0;

top = numData - 1;

while (top >= bottom)

{

middle = (top + bottom) / 2;

if (x > data[middle])

bottom = middle + 1;

else

if (x < data[middle])

top = middle - 1;

else

{

exist = true; // Found

position = middle;

bottom = top + 1; // Terminate loop

}

}

if (!exist)

position = -1;

return position;

}

void display()

{

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

cout<< data[i] << ' ';

cout << endl;

}

};

int main()

{

// Data must be ordered

int data[] = {3, 21, 34, 52, 54, 67, 68, 88, 89, 90};

BinarySearch list(data, 10);

cout<< "The contents of array: ";

list.display();

int toBeSearch = 21;

int position = list.find(toBeSearch);

if (position != -1)

cout << "Data " << toBeSearch

<< " is found at position : "

<< position <

else

cout << "Data " << toBeSearch

<< " is not found." << endl;

return 0;

}

  1. Create a header file named BinarySearch.h which contains BinarySearch code.

Show the BinarySearch.h program code.

  1. Show the main.cpp file content.

  1. Show the program output.

  1. Add a new code to search number 88. Show your code and output.

  1. Briefly explain what happen in function find().

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!