(
Latest Revision:
October 9, 2016
)
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.
One of the major lessons learned is that the perfection of mathematics does not come true on computers.
The following mathematical formulas convert a temperature in Fahrenheit degrees to the equivalent in Celsius, and vice versa.
5
Celsius = --- (Fahrenheit - 32)
9
9
Fahrenheit = ( --- Celsius) + 32
5
For example, 77 degrees Fahrenheit is equivalent to 25 degrees Celsius, as shown:
5 5
Celsius = --- (77 - 32) = --- (45) = 25
9 9
Using the second formula, we see that 30 degrees Celsius is equivalent to
86 degrees Fahrenheit:
9
Fahrenheit = ( --- (30) ) + 32 = 54 + 32 = 86
5
Individual steps:
- The "assignment statement" below is not written in correct C++ syntax.
Write the assignment statement on paper, using proper C++ syntax, in a
manner that, when the program executes, will cause the
division of 5 by 9 to be performed before
the multiplication by (Fahrenheit - 32):
5
Celsius = --- (Fahrenheit - 32)
9
- Using C++ integer division, what
is the value of 5/9? (See the discussion in the text book about how things
work when one integer is divided by another integer.)
- 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 Celsius?
- 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
(Fahrenheit - 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
assignment 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 (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. (Mac computers
have a calculator application.) Some of the answers you get from your
program may be incorrect, but none should be off by more than one degree.
- 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
Fahrenheit = ( --- Celsius ) + 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 order they appear:
- 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 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.)
- 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, using a calculator
or hand calculations. 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 you tested when you
performed step 10. 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.