Latest Revision: 09/16/01

Directions for Lab #3

Read and study all these directions before the day of the lab. Try to rehearse mentally what you will be doing. Better still, if you have time, get on line and actually rehearse some of the steps.

The goal of this exercise is to study functions, operator precedence, and the truncation effect of integer division. The following formulas convert a temperature in Fahrenheit (F) to its equivalent in Celsius (C) and vice versa.


     5
C = --- (F - 32)
     9
    
     9
F = --- C + 32
     5
For example, 77 degrees F is 25 degrees C, as shown:

     5               5
C = --- (77 - 32) = --- (45) = 25
     9               9
Using the second formula, we see that 25 degrees C is 77 degrees F:

     9
F = --- (25) + 32 = 45 + 32 = 77
     5

Individual steps:

  1. Write the following assignment statment on paper in C++ code, so that the division will be performed first:
    
         5
    C = --- (F - 32)
         9
    
  2. In integer divsion, what is 5/9?
  3. In view of your answer to step 2, what can you conclude about the assignment statement you constructed in step 1? What value does it assign to C?
  4. To minimize the effect of division truncation, rearrange the expression on the right hand side of the assignment operator so that multiplication occurs before division.
  5. Make a file called lab031.cpp from the program you see when you click here.
  6. Program lab031.cpp reads a temperature in Fahrenheit and calculates the corresponding integer Celsius temperature. Fill in the body of function F_to_C() with C++ code to make it do the correct thing. Don't forget that you need to have a return statement at the end of F_to_C(). Without the return statement, F_to_C() cannot give its result to its caller.
  7. Test program lab031.cpp by running it several times, giving it a different input each time, and checking the answers it gives either with an electronic calculator or with hand calculations.
  8. Think about how you would write this assignment statement in C++ to minimize the effect of division truncation:
    
         9
    F = --- C + 32
         5
    
  9. Add to the program lab031.cpp a prototype and definition for a function C_to_F() that inputs an integer parameter Celsius and returns the corresponding integer Fahrenheit temperature. Base the code in the body of this function on the C++ assignment you worked up in step 8.
  10. Add statements in main() to read a measurement in Celsius, call C_to_F() to make the conversion to Fahrenheit, and print the Fahrenheit temperature.
  11. As you did in step 7, test the program now with several values and check the results.
  12. When you are sure everything is working correctly, make a script showing the program working on all the input values of step 11.
  13. E-mail me a copy of the program source code (the file lab031.cpp) with subject line "cs1500,lab03,source". (Refer to the directions of the "Hello World!" assignment if you need help with the directions for sending the e-mail.)
  14. E-mail me a copy of the script with subject line "cs1500,lab03,script".



I'd like everyone to get finished and e-mail the source and script by the end of the 50 minutes alloted for the lab.