/////////////////////////////////////////////////////////////// // // assertDemo.cpp // // Demonstrates the use of assertions to enforce // preconditions. // // To disable the assert statements (which you would do // before shipping the release version of your program), // you need to #define the symbol NDEBUG (in this program, // just uncomment the #define line). // // You can also induce the compiler to define NDEBUG from // the command line, like so: // // g++ -Wall -D NDEBUG -o assertDemo assertDemo.cpp // // // // REVISION HISTORY: // 4/6/01 (Jeff Ondich) Original version written. // /////////////////////////////////////////////////////////////// //#define NDEBUG #include #include int fib( int n ); int main() { for( int i=1; i <= 10; i++ ) cout << i << " " << fib( i ) << endl; cout << fib( -17 ) << endl; return 0; } /////////////////////////////////////////////////////////////// // // Pre-condition: n >= 1 // // Post-condition: fib returns the nth Fibonacci number. // // Note that this recursive approach is extraordinarily // inefficient, and is here just for the purposes of // demonstrating assert(). // /////////////////////////////////////////////////////////////// int fib( int n ) { assert( n >= 1 ); if( n == 1 || n == 2 ) return 1; else return fib( n - 1 ) + fib( n - 2 ); }