1: // Created by Frank M. Carrano and Tim Henry. 2: // Copyright (c) 2013 __Pearson Education__. All rights reserved. 4: int search(int first, int last, int n); 5: int mystery(int n); 7: int main() 8: { 9: std::cout << "mystery(30) produces the following output: \n"; 10: int result = mystery(30); 11: std::cout << "mystery(30) = " << result << "; should be 5\n"; 12: return 0; 13: } // end main 15: int search(int first, int last, int n) 16: { 17: int returnValue = 0; 18: std::cout << "Enter: first = " << first << " last = " 19: << last << std::cendl; 20: 21: int mid = (first + last)/2; 22: if ( (mid * mid <= n) && (n < (mid+1) * (mid+1)) ) 23: returnValue = mid; 24: else if (mid * mid > n) 25: returnValue = search(first, mid-1, n); 26: else 27: returnValue = search(mid+1, last, n); 28: 29: std::cout << "Leave: first = " 30: << first << " last = " << last << std::endl; 31: return returnValue; 32: } // end search 34: int mystery(int n) 35: { 36: return search(1, n, n); 37: } // end mystery