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 {
- Open your preferred C++ IDE, and create new console project.
- 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; } |
- Create a header file named BinarySearch.h which contains BinarySearch code.
Show the BinarySearch.h program code.
- Show the main.cpp file content.
- Show the program output.
- Add a new code to search number 88. Show your code and output.
- Briefly explain what happen in function find().
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started