(
Latest Revision:
October, 2007
)
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:
- 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 operation to be performed first:
5
C = --- (F - 32)
9
- Using C++ integer division, what is the value of 5/9?
- 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?
- 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.
- Make a file called lab03.cpp from the skeleton program you see
when you click here.
- The purpose 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 body of function F_to_C() with
C++ code to make it do the correct thing. Use the idea from step 4.
Remember you need a return statement at the end of
F_to_C(). Without the return statement, F_to_C()
cannot give its result to its caller.
- Test program lab03.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.
- Think about how you would write this assignment statement in proper C++
syntax, and also to minimize the error caused by integer division:
9
F = --- C + 32
5
- Add to the program lab03.cpp a prototype and definition for a function
C_to_F() that inputs an integer parameter Celsius and returns an
integer estimate of the equivalent Fahrenheit temperature. Base the code
in the body of this function on the C++ assignment you worked up in step
8. What hints do you have about where to put the new prototype and the
new function definition?
- Add statements in main() to prompt for and read an integer
Celsius temperature, call C_to_F() to estimate a Fahrenheit
equivalent, and print the Fahrenheit temperature. As a guide, use the
existing statements that convert from Fahrenheit to Celsius. You can
just copy those statements and then make the required changes to
transform it into code that converts from Celsius to Fahrenheit.
- As you did in step 7, test the program now with several values and
check the results.
- When you are sure everything is working correctly, make a script
showing the program working on all the input values of step 11.
- E-mail me a copy of the program source code (your completed file
lab03.cpp) with subject line:
CS1500Lab03Source
E-mail me a (filtered) copy of the script with subject line:
CS1500Lab03Script
(A word about the subject lines - it is really extremely important that
you use the exact subject lines I specify. This will assure that I am
able to find your message among the hundreds and hundreds that 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'd like everyone to get finished and e-mail the source and script
by the end of the 50 minutes alloted for the lab.