Question: C + + Binary search array input I want to be able to input an array of 1 or 2 5 or 1 0 0

C++ Binary search array input I want to be able to input an array of 1 or 25 or 100 is their a better way then how I did it? cout << "Please input a number to find: ";
cin >> n;
auto start = chrono::steady_clock::now();
int binarySearch(int arr[], int l, int r, int n);
// Arrays are shorted 1,25,100
//int arr[]={2};
//int arr[]={0,2,4,6,8,10,12,14,16,18,20,22,24,26,27,28,29,30,31,32,33,34,35,36,38,40,42,};
int arr[]={0,2,4,6,8,10,12,14,16,18,20,22,24,26,27,28,29,30,31,32,33,34,35,36,38,40,42,
50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,89,90,91,92,93,94,95,96,97,
98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143};
int x = sizeof(arr)/ sizeof(arr[0]);
int result = binarySearch(arr,0, x -1, n);
if (result ==-1){
cout << "Element is not present in array" << endl;
}
else {
cout << "Element is present at index "<< result << endl;
}
auto end = chrono::steady_clock::now();
double elapsed_time_ns = double(chrono::duration_cast (end - start).count());
cout << "Elapsed Time(s)"<< elapsed_time_ns /1e9<<" nanoseconds "<< endl;

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!