1.5.1. Exercises: Introduction to Functions

  1. Locate the code for Example 1.3 in the src tree and then

    • Build and test that program with a variety of input values, including some non-numeric values.

    • Determine the largest input value that can produce a valid output.

    • Can you change the program so that it can produce valid results for larger input values?

    • Modify the program so that it cannot produce invalid results.

    • Explore the effects of using the statement

       if (cin >> n)  {  ...  } 

      to enclose the processing of n in this program. In particular, try entering non-numeric data after the prompt. This is an example of the use of a conversion operator, which is discussed in more detail in Section 19.9.1.

    • Modify the program so that it can accept values from the user until the value 9999 is entered.

  2. Write definitions for the functions

      double toCelsius(double fahrenheitTemp);
      double toFahrenheit(double celsiusTemp);
    

    and then write a program that enables the user to obtain temperature conversions (in either direction) between Celsius and Fahrenheit.

[ fromfile: cppintro.xml id: x-intro-functions ]