text cover

Data Abstraction and Problem Solving with C++

Walls and Mirrors

by Frank M. Carrano

Addison Wesley Logo

c02p118.cpp

Go to the documentation of this file.
00001 
00015 // Exercise 12
00016 
00017 int search(int first, int last, int n);
00018 int mystery(int n);
00019 
00020 int main()
00021 {
00022    cout << mystery(30) << endl;
00023    return 0;
00024 }  // end main
00025 
00026 int search(int first, int last, int n)
00027 {
00028    int returnValue;
00029 
00030    cout << "Enter: first = " << first << " last = "
00031    << last << endl;
00032 
00033    int mid = (first + last)/2;
00034 
00035    if ( (mid * mid <= n) && (n < (mid+1) * (mid+1)) )
00036       returnValue = mid;
00037    else if (mid * mid > n)
00038       returnValue = search(first, mid-1, n);
00039    else
00040       returnValue = search(mid+1, last, n);
00041 
00042    cout << "Leave: first = " << first << " last = "
00043    << last << endl;
00044 
00045    return returnValue;
00046 }  // end search
00047 
00048 int mystery(int n)
00049 {
00050    return search(1, n, n);
00051 }  // end mystery

Generated on Sun Aug 27 11:05:08 2006 for AWLogo by  doxygen 1.4.6