(C++ exercise) 4. Write a recursive version of the binary search
algorithm. (Hint: There will be two recursive function calls in
the function.)
P/S: please seperate the main.cpp, header file and implement
file (if there is any header and implement file that may apply)
like this pattern as shown below:
main.cpp
Code
header.h
Code
implement.cpp
Code
(Thanks for helping me and I really do appriciate it!)
Solution
#include <iostream>
#include <cmath>
using namespace std;
// the function
int binarySearch(int L[],int x, int first, int last)
{
if (last >= first) //first > last)
{
//return -1;
int middle = (first + last) / 2;
if (x == L[middle])
return middle;
else if (x < L[middle])
return binarySearch(L, x, first, middle - 1);
else //if(x > L[middle])
return binarySearch(L, x, middle + 1, last);
}
else
return -1;//(first + 1); // failed to find key
}
int main()
{
/* int myList[size] = n;
int myfirst = 0;
int mylast = n - 1;
int findthis;*/
int myList[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
//int size;
//int myList[size];
//int n = myList[10];
int myfirst = 0;
int mylast = 10;//n - 1;
int findthis;
//int i = 0;
cout << "Please enter a number to find: " << endl;
cin >> findthis;
int resultloc = binarySearch(myList, findthis, myfirst, mylast);
if ( resultloc == -1 )
{
cout << "number not found" <<endl;
}
else
{
cout <<" number is found in position " << resultloc + 1 <<
endl;//binarySearch(myList, findthis, myfirst, mylast) <<endl;
}
/*if(resultloc == middle)
{
cout << findthis << " was found in position " << middle <<
endl;
}*/
return 0;
}

(C++ exercise) 4. Write a recursive version of the binary search alg.docx

  • 1.
    (C++ exercise) 4.Write a recursive version of the binary search algorithm. (Hint: There will be two recursive function calls in the function.) P/S: please seperate the main.cpp, header file and implement file (if there is any header and implement file that may apply) like this pattern as shown below: main.cpp Code header.h Code implement.cpp Code (Thanks for helping me and I really do appriciate it!) Solution #include <iostream> #include <cmath> using namespace std; // the function int binarySearch(int L[],int x, int first, int last) { if (last >= first) //first > last) { //return -1;
  • 2.
    int middle =(first + last) / 2; if (x == L[middle]) return middle; else if (x < L[middle]) return binarySearch(L, x, first, middle - 1); else //if(x > L[middle]) return binarySearch(L, x, middle + 1, last); } else return -1;//(first + 1); // failed to find key } int main() { /* int myList[size] = n; int myfirst = 0; int mylast = n - 1; int findthis;*/ int myList[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}; //int size; //int myList[size]; //int n = myList[10]; int myfirst = 0; int mylast = 10;//n - 1; int findthis;
  • 3.
    //int i =0; cout << "Please enter a number to find: " << endl; cin >> findthis; int resultloc = binarySearch(myList, findthis, myfirst, mylast); if ( resultloc == -1 ) { cout << "number not found" <<endl; } else { cout <<" number is found in position " << resultloc + 1 << endl;//binarySearch(myList, findthis, myfirst, mylast) <<endl; } /*if(resultloc == middle) { cout << findthis << " was found in position " << middle << endl; }*/ return 0; }