Sections 0101, 0102, 0103 and Honors, Fall 1995
Thursday September 221, 1995
Assigned Reading: 3.4 - 3.6
Handouts (available on-line): none
- Programs which show the "dangling else". Where there are
two if's and only one else, the else
is matched with the closest if.
First program and sample run.
Second program and sample run.
- Designed a program that computes the minimum, average and
sum of a list of numbers using the "loop unrolling" method.
First we wrote down a list of
statements that we want to accomplish. Then, we use
the repeating part as the body of a while loop.
The resulting program
may not be the tightest code, but this is a methodical
way to get started. The sample
run shows that the program handles empty lists and lists
with 1 or 2 elements correctly.
The trouble with integer variables is that they can only
store integer values within a certain range. The C compilers
on our UNIX system store integers in 4 bytes. These
variables can store numbers between -2,147,483,648
and 2,147,483,647. The program
which computes the powers of 2, shows that we can
exceed this range easily when working with large numbers
(see sample run).
When this happens, large positive numbers can become
large negative numbers. This can happen without error or
warning. It is the responsibility of the programmer to
make sure that the integer values do not exceed the available
range. Some compilers use 2 byte integers, which is
equivalent to short int on our UNIX C compiler.
In this case, we can get into trouble much sooner.
(See program and
- "99 Bottles of Beer on the Wall".
A program that demonstrates that you should not test
floating point numbers for equality. The culprit
is that 1/5 is not a terminating fractional number in base 2
(similar to 1/3 in base 10).
- A note on how to represent
fractions in base 2.
Thu Sep 21 14:17:54 EDT 1995
Richard Chang, firstname.lastname@example.org