( Latest Revision: February 18, 2015 )

Directions for Lab #3



I expect all teams to finish the lab by the end of the 50-minute time period, including the e-mailing of the source and script.




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 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 equivalent to 25 degrees C, as shown:

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

       9
F = ( --- (30) ) + 32 = 54 + 32 = 86
       5

Individual steps:

  1. The "assignment statement" below is not written in correct C++ syntax. Write the assignment statment on paper, using proper C++ syntax, in a manner that will cause the division of 5 by 9 to be performed before the multiplication by (F - 32):
    
         5
    C = --- (F - 32)
         9
    
  2. Using C++ integer division, what is the value of 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 the variable C?

  4. To reduce the error caused by integer division, rearrange the expression on the right hand side of the assignment operator so that the multiplication of 5 times (F - 32) occurs before division by 9.

  5. Make a file called lab03.cpp from the skeleton program you see when you click here.

  6. One of the purposes of program lab03.cpp is to read an integer Fahrenheit temperature and to calculate an integer estimate of the equivalent Celsius temperature. Fill in the right hand side (RHS) of the assignment statement with a C++ expression that will make the assigment statement do the correct thing. Use the idea from step 4. Use only int variables and constants. (No constants with decimal points or variables of type double or float.)

  7. Test program lab03.cpp by running it several times (more than twice), giving it a different input each time, and checking the answers it gives either with an electronic calculator or with hand calculations. The answers you get from your program should not be wrong by more than one degree.

  8. Now that you have finished making the conversion of Farhenheit to Celsius work as well as possible, let's think about the inverse of that problem. The assignment statement below represents the conversion of Celsius to Fahrenheit. How would you reorder it and write it (using correct C++ syntax) so it will minimize the error caused by integer division?
    
           9
    F = ( --- C ) + 32 
           5
    
  9. Near the end of the body of the main() function (but before the return statement) add statements that do the following things, in the order they appear:


    Base the assignment statement you write on the idea you worked up in step 8. In this assignment statement too, there must be only int variables and constants. (No constants with decimal points or variables of type double or float.)

  10. As you did in step 7, test the program by running the program at least twice. Don't repeat any of the numbers you input. Check all the answers the program gives. You can look at an example of one program run here.

  11. When you are sure everything is working correctly, make a script showing the program working on all the input values you tested when you performed step 10. Remember to clean up the script using the filtering technique you learned.

  12. E-mail me a copy of the program source code (your completed file lab03.cpp) with subject line:

    CS1500Lab03Source

    E-mail me a copy of the filtered script with subject line:

    CS1500Lab03Script

    (A word about the subject lines - it is very important that you use the exact subject lines I specify. This will assure that I am able to find your message among the many I receive each day. You will lose a significant amount of credit for this assignment if you don't use the correct subject line. The easiest way to make sure you are using the correct subject line is to just select, copy, and paste it into the appropriate location when you are composing the e-mail. )

    You may refer to the directions of the "Hello World!" assignment if you need help with sending the e-mail. Also feel free to just ask someone - including me.



I expect all teams to finish the lab by the end of the 50-minute time period, including the e-mailing of the source and script.