#include // Function declarations int main(); double my_max( double x, double y ); double my_min( double x, double y ); // Function definitions int main() { double x{}; std::cout << "Enter a number: "; std::cin >> x; double y{}; std::cout << "Enter another number: "; std::cin >> y; std::cout << "my_max(" << x << ", " << y << ") = " << my_max( x, y ) << std::endl; std::cout << "my_min(" << x << ", " << y << ") = " << my_min( x, y ) << std::endl; return 0; } // double my_max( ... ) // // @author Douglas Wilhelm Harder // @date 2020-09-09 // @version 1.0 // // @param x a real number // @param y a real number // // @returns the maximum of 'x' and 'y' // // @algorithm // If 'x >= y', return x; // otherwise, return y. // // Exercises: // 1. Is there any difference if we use the following // implementation? Why or why not? // // double my_max( double x, double y ) { // if ( x > y ) { // return x; // } else { // return y; // } // } // // For ordinary unequal finite values, both // implementations return the same value. If // 'x == y', however, the original implementation // returns 'x', while this implementation returns // 'y'. Usually this makes no observable difference, // but floating-point values include special cases // for which the distinction may matter. // // 2. Implement and document the my_min function. // // 3. Author two new functions, including both the // function declarations and function definitions, // that calculate // // my_max( x, y, z ) // // and // // my_min( x, y, z ). // // These functions should also be called 'my_max' // and 'my_min', respectively. // // 4. Modify main() to test your new functions. // // 5. Is it valid to declare two functions with the // same name but different numbers of parameters? // Can the compiler determine which function should // be called? // // Answer: Yes. This is called function overloading. // The compiler determines which overloaded function // to call from the arguments supplied in the // function call. double my_max( double x, double y ) { if ( x >= y ) { return x; } else { return y; } } // double my_min( ... ) // // @param x a real number // @param y a real number // // @returns the minimum of 'x' and 'y' // // @algorithm // Exercise: Complete this function. double my_min( double x, double y ) { return 0.0; }