Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Students also viewed these Databases questions

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago