#include // Consider the following piecewise-defined function: // // / 0, x <= -1, // | // | -1, -1 < x < 0, // | // square(x) = < 0, x = 0, // | // | 1, 0 < x < 1, // | // \ 0, x >= 1. // // Its graph looks like: // // // | // 1 o-------o // | // | // --------------o------o------o-------------- x // -1 0 1 // o-------o -1 // | // // // The open circles indicate that the corresponding // endpoint is not included, while the closed circles // indicate that the endpoint is included. // // For example: // // x = -2.0 -> square(x) = 0 // x = -1.0 -> square(x) = 0 // x = -0.4 -> square(x) = -1 // x = 0.0 -> square(x) = 0 // x = 0.6 -> square(x) = 1 // x = 1.0 -> square(x) = 0 // x = 3.2 -> square(x) = 0 // // The following program attempts to implement this // function using a cascading conditional statement. // // It compiles, but it does not always produce the // correct result. // // Determine what is wrong and fix the program. // Function declarations int main(); // Function definitions int main() { double x{}; std::cout << "Enter a number 'x': "; std::cin >> x; /////////////////////////////////////////// // Exercise: This does not work. Fix it. // /////////////////////////////////////////// if ( x < -1.0 ) { std::cout << "square(x) = " << 0.0 << std::endl; } else if ( x < 0.0 ) { std::cout << "square(x) = " << -1.0 << std::endl; } else if ( x > 0.0 ) { std::cout << "square(x) = " << 1.0 << std::endl; } else if ( x > 1.0 ) { std::cout << "square(x) = " << 0.0 << std::endl; } else { std::cout << "square(x) = " << 0.0 << std::endl; } return 0; } // Hint: // // Test the program using values from each of the five // cases in the definition. // // In particular, test the boundary values: // // x = -1 // x = 0 // x = 1 // // Also test values strictly between and outside these // boundaries. // // For each condition, ask: // // If this condition is false, what do I already // know about 'x' when I reach the next condition? // One problem occurs here: // // } else if ( x > 0.0 ) { // // This condition includes every positive value of 'x', // including values greater than or equal to 1. // // Consequently, if x = 2, this condition is true and // the program prints 1. The later test // // } else if ( x > 1.0 ) { // // can therefore never be reached when its condition // would be true. // // A cascading conditional must be arranged so that // each branch correctly eliminates values before the // next branch is considered. // Here is one solution: /* int main() { double x{}; std::cout << "Enter a number 'x': "; std::cin >> x; if ( x <= -1.0 ) { // Here, x <= -1. std::cout << "square(x) = " << 0.0 << std::endl; } else if ( x < 0.0 ) { // The first condition was false, so x > -1. // Thus, we now know // // -1 < x < 0. std::cout << "square(x) = " << -1.0 << std::endl; } else if ( x == 0.0 ) { // The first two conditions were false and // this condition tells us x = 0. std::cout << "square(x) = " << 0.0 << std::endl; } else if ( x < 1.0 ) { // We already know that x > 0. Together with // the current condition, this gives // // 0 < x < 1. std::cout << "square(x) = " << 1.0 << std::endl; } else { // All previous conditions were false, so // // x >= 1. std::cout << "square(x) = " << 0.0 << std::endl; } return 0; } */