#include #include // Function declarations int main(); double tent( double x ); // Function definitions int main() { for ( int k{ -25 }; k <= 25; ++k ) { double x{ 0.1*k }; std::cout << " tent( " << x << " )\t= " << tent( x ) << std::endl; } return 0; } // double tent( ... ) // // @author Douglas Wilhelm Harder // @date 2020-09-09 // @version 1.0 // // @param x a real number 'x' where 0 <= x <= pi/2 // // @returns The tent-function evaluated at this point // // @algorithm // // This function is defined as: // 0 if x <= -1 or x >= 1 // x + 1 if -1 < x <= 0 // 1 - x if 0 < x < 1 // // Exercise: // 1. What happens if you try to assert // assert( 0.0 < x < 1.0 ); // Does it still compile? Does it still work? // Can you determine why this may be the case? // // Hint: 0.0 < x < 1.0 works the same way as // a + b + c // That is, it calculates (a + b) + c. double tent( double x ) { if ( (x <= -1) || (x >= 1) ) { return 0.0; } else if ( x <= 0 ) { return x + 1.0; } else { // This assertion will fail only if someone makes a mistake // and incorrectly alters the first or second conditions above. assert( (x > 0.0) && (x < 1.0) ); return 1.0 - x; } }