(
Latest Revision:
September 25, 2011
)
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 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 of 5 by 9 to be
performed before the multiplication by (F -
32):
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.
- 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.)
- 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.
- Now think about how you would write the assignment statement below in
proper C++ syntax. Think about how to do it in a way that will minimize
the error caused by integer division:
9
F = --- C + 32
5
- Near the end of the body of the main() function, but before the
return statement, add statements that do the following things
(in the same order listed below):
- prompt for an integer Celsius temperature,
- read the integer into the variable named Celsius,
- assign to the variable Fahrenheit the Fahrenheit
temperature corresponding to the Celsius temperature stored in the
variable Celsius, and
- print the Fahrenheit temperature.
Base the assignment on what 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.)
- As you did in step 7, test the program now with several
values and check the results. You can look at an example of one program
run here.
- When you are sure everything is working correctly, make a script showing
the program working on all the input values of step 10. (When you make
the script, you'll have to run the program several times -- so that you
can test several sets of inputs.) Remember to clean up the script using
the filtering technique you learned.
- 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.