UMBC CMSC202, Computer Science II, Spring 1998,
Sections 0101, 0102, 0103, 0104 and Honors
Tuesday February 3, 1998
Assigned Reading:
Handouts (available on-line): none.
Topics Covered:
- Announcement: University Computing Services will be holding
training classes for those who need help getting started
with email, WWW browsers and UNIX.
Schedule.
-
I've set up the submit directories for this class. The class
name for this class is "cs202-01". Notice that there is a hyphen
in the class name, not an underscore. The project names are
"proj1", "proj2", "proj3", "proj4" and "proj5". A
transcript shows how to use
the submit, submitls and submitrm commands.
- Rehash some leftover issues with scanf() and I/O buffers.
- First, a program which shows that
strings printed using printf() may not make it to the
screen, if the program crashes abruptly. Sample run.
- With file I/O, it might even be the case that nothing is written
to the file, because the program crashed abruptly.
Program and
sample run.
- Flushing the buffer using a call to fflush(), makes
sure that the data in the buffer is written to disk.
Program and
sample run.
- Input buffers are less predictable. Flushing stdin
causes the characters up to the next carriage return to be
discarded. This is useful if there are "leftover" characters
after a call to scanf(n). A
program which uses fflush(stdin)
achieves about the same functionality as the from last lecture which uses
the wipeline() function. However, from the
sample run we see that
fflush(stdin) has no effect if the standard input is
is redirected a disk file.
- We start looking at recursion. The first few programs we
discuss on recursion are "mickey mouse" examples. You probably would
not use recursion to write these programs. But, soon we will see that
there are problems which requires us to think recursively.
- The first program using
recursion simply demonstrates that a function can call itself.
Sample run.
- The second program using
recursion simply demonstrates that syntactically correct
recursive functions may still contain an infinite loop Sample run.
- The third program using
recursion shows the effect of recursive calls on local
variables (which is "none"). Sample
run.
- We develop a recursive function to compute n! (read "n
factorial").
- The first program does not
use any recursion. The function iter_fact uses
a for loop to compute n!. Sample run.
- In the second program the function
rec_fact computes n! by returning the value
n * iter_fact(n-1) . If you believe that
iter_fact works correctly, you should also believe that
rec_fact works correctly. Sample
run.
- What if iter_fact does not work correctly? In the
third program we put a bug in
iter_fact so that it crashes if it is called with
a parameter > 7. However, since iter_fact still works
for parameters <= 7, the rec_fact function still works
for values <= 8. Sample run.
- Finally, we look at a program
with a recursive function that computes n!. To prove that this
function works correctly, we use mathematical induction. First,
we notice that the if statement guarantees that rec_fact
computes n! correctly for 0! and 1!. This then guarantees that
it computes 2! correctly, which in turn guarantees that 3! is
correct, and so on and so forth.
Sample run.
Last Modified:
22 Jul 2024 11:27:44 EDT
by
Richard Chang
Back up
to Spring 1998 CMSC 202 Section Homepage