CS 1711 Lab 4 - October 13 and 15, 1998
Functions and Real Numbers
Watching Program Variables
It is often useful to
set a ``Watch'' on program variables, to see if their values change
during a program run. Watching a variable can be a productive way
of uncovering bugs in a program.
- Move the cursor on top of any occurrence of the
variable you want to watch.
- Choose ``Add Watch'' from the Debug menu. You should be
given a dialog box with the variable name already in the box.
- Press Enter to set up a watch on the variable.
(you can repeat this for other variables.)
- Move the Watch window to the right.
- Now step through the program,
keeping an eye on the Watch window.
You should see the values of of the watched variable change during
a run of the program.
Euclid's Algorithm
In the file GCD.CPP (in the usual directory) is the program
GCD, which will output the greatest common divisor (GCD)
of the two positive integers entered by the user. The GCD of two
integers is the largest integer that divides both of them.
The program is based on Euclid's algorithm for
finding the GCD of two positive integers m and n, i.e. GCD(m,n).
This algorithm says that:
-
[a.] The GCD is n if n is the smaller number and n
divides m.
- [b.] If m is the smaller number, then the GCD
determination should be performed with the arguments transposed (switched).
- [c.] If n does not divide m, the answer is obtained by
finding the GCD of n and the remainder of m divided by n.
For example, to find GCD(28,48):
apply |
to get: |
b |
GCD(48,28) |
c |
GCD(28, remainder of 48 divided by 28) = GCD(28,20) |
c |
GCD(20, remainder of 28 divided by 20) = GCD(20,8)
|
c |
GCD(8, remainder of 20 divided by 8) = GCD(8,4) |
a |
4 |
The program has a bug in it. Although you may be
able to identify the bug without the use of your debugging tools,
you should still follow the steps listed here, so that you can
practice using the tools. The true value of the tools is
discovered when you have a large program, thousands of lines
long, that is hiding a small bug.
- Obtain the program from the file GCD.CPP.
- Test the program. What does it output as the GCD of
42 and 18 (enter 42 for the first number and 18 for the second
number)? Is that correct?
- Test the program again. What does it output as the
GCD of 18 and 42? Is that correct?
- Use the debugging tools to set up a Watch of the
variables m and n. You want to determine each value that m becomes
during an execution of the program. Run the program again with
the input values of 48 and 28. Show the sequence of values for m
and n:
m:
n:
- Compare your trace of the values of m and n, to the
values shown in the table on the previous page. Study the program and
determine the error. What line of the program needs to be changed?
- Fix the bug in the program and save the program. What is the
GCD of 16380 and 1911? Before leaving the lab, you will be asked to
load this program and point out the bug that was fixed.
- Use your debugger to answer the following questions
about the corrected program:
[a.] Write down the sequence of values for variable m if the
input is 5913 and 7592.
- [b.] How many times is the then part of the if-then-else
statement executed if the input
is 5913 and 7592?
Accuracy
The section is really an experiment in which you are
given the basic programs you need, and you are asked specific
questions about the results. The objective of the experiment is
to establish how many decimal places you can view of a
real number on your system.
- Obtain the file TESTREAL.CPP.
Look it over. You will see a new loop construct known as a `do-while'
loop, it is described in Chapter 7 of the text if you are interested but
essentially it works the same as a `while' loop.
- This program will output the value
2/3 repeatedly, each time using a higher value for the
number of decimal places to display. The sleep(1) function
causes the program to pause for 1 second before proceeding so you
can view the output.
- Run the program and determine the maximum number of
decimal places that your system reports to you.
- How many decimal places can you use?
- Turbo C++provides provides a few alternative types for
representing real numbers.
Now change the type `float' to be one of the
following: double, and long double. How many decimal places can you
use with each of these types?
- A theorem states:
the limit as x approaches zero of sinx divided by x
equals 1.
- The program in file
TESTSINE.CPP investigates the computer's evaluation
and reporting of this
formula. Given an initial number of radians, the program reports
sinx divided by x for that number,
then for that number divided by 2,
then divided by 4, then 8, and so on.
The program reports the value in both a narrow format and a
broad format. Obtain the file and study
it. Edit the program and set the
value of the constant numdecs on line 9 to whatever answer you
got for question number 5 above (with the double type).
This way, when you run the
program, it will report as much information as possible on your
system.
- Answer the following questions about the program (the lab assistant will ask for your answers):
- [a.] What is the purpose of the if statement on
line 55?
- [b.] On what line is the radian number cut in half?
- [c.] On what line is the formula evaluated?
- Run the program. Use an initial value for the
radians of 99.
- [a.] At what count does the computer show in the
narrow format that the value of sinx/x is 1?
- [b.] At what count does the computer show in the
broad format that the value of sinx/x is 1?
- [c.] What is the value of the radians at that point,
i.e., when the value of sinx/x is 1 in the broad format.
- [d.] At what count does the computer show that the
value of the Boolean expression sinx/x = 1.0 is true?
- [e.] At what count does the computer report the value of the
radians as zero?
-
Now open the file SAME.CPP.
Notice that a is set to 27 and b is set to 9.
The variable x then is set to a2 and y is set to
b3; clearly a2 and b3 are the same value.
Run the program to see what output you get.
- Now note that if a were 1/27 and b were 1/9 then the relation that a2
equals b3 is still correct. Change the values of a and b in the
program then run it again.
What happens?
Purpose of Above Excercise
When dealing with real numbers (float or double):
- You can't always trust what you see. Just because it looks like
a value of 1 doesn't mean it actually is.
- Even with all digits of accuracy switched on, 1 doesn't even equal
1! This problem will arise in a later assignment; it is very difficult to
check two real numbers for equality.
- Keep in mind that the answer you see on the screen for sinx
divided by x should
have never been 1.
Question: Its obvious that if you want more accuracy, you would use double
or long double. Why would you ever want to use the type float?