#include using std::cout; using std::endl; using std::ostream; int bin_srch(int ar[], int arsize, int desired_value); int main() { int ar[10] = {10, 20, 30, 40, 50, 60, 70}; // rest unused int arsize = 7; // number of entries in use int result; result = bin_srch(ar, 7, 30); if (result == -1) cout << "30 is not in array" << endl; else cout << "30 is at slot " << result << endl; result = bin_srch(ar, 7, 32); if (result == -1) cout << "32 is not in array" << endl; else cout << "32 is at slot " << result << endl; result = bin_srch(ar, 7, 10); if (result == -1) cout << "10 is not in array" << endl; else cout << "10 is at slot " << result << endl; result = bin_srch(ar, 7, 70); if (result == -1) cout << "70 is not in array" << endl; else cout << "70 is at slot " << result << endl; result = bin_srch(ar, 7, 72); if (result == -1) cout << "72 is not in array" << endl; else cout << "72 is at slot " << result << endl; return 0; } // *********************************** int bin_srch(int ar[], int arsize, int desired_value) { int lo, mid, hi; lo = 0; hi = arsize - 1; while (lo <= hi) { mid = (lo+hi)/2; if (ar[mid] == desired_value) return(mid); if (desired_value < ar[mid]) hi = mid - 1; else lo = mid + 1; } return(-1); }