Lab 9: Some programming examples with loops and functions
The first part of the lab was devoted to a variety of loop forms,
in particular to infinite loops and loops that are infinite
mathematically, but aren't such due to finiteness of computer
arithmetics.
The second part introduced the so called function wraps and stubs.
By mistake, the prefix lab7 was used for program names.
It should have been lab9. The links below use the correct
number 9.
Part I
The following Thursday programs (b,c) employ incremental
counters, and their Friday counterparts (e,f) use countdown.
The first program lab9a.c
is just a demonstration of an infinite while-loop
with a nonzero numeric constant as loop condition.
Replacing while(5) to
while(0) makes the program
never enter the loop instead of continuing indefinitely.
A possibility to use a number as loop condition
is exploited in countdown version lab9e.c.
Here the loop header is short and elegant
while(i).
-
About the line
i-=2; /* leads to infinite loop */
-
Such a decremental loop can be infinite if the decrement
is greater than 1: once you missed to stop at i==0, you never stop.
lab9b.c: Some for-loops.
The final version is a puzzle. The initial value of the
loop counter i is 0. Every time in the loop i
gets incremented and the condition is i>=0. And
yet the loop terminates!
(For a hint see end of comments to
Lab 4.)
The effect cannot be observed, if printf is kept
in the loop body: reaching a nonpositive value of i
takes too much time. [For those philosophically inclined:
This is an example of "quantum effect": measurment
(printing a message from inside the loop) interferes
with effect we want to observe (finiteness of the loop).]
lab9f.c:
Finiteness of a mathematically infinite loop is demonstrated
with a decremental while-loop.
Nested loops:
lab9c.c (with incremental counting),
lab9g.c (with decremental counting).
The programs print current values of both counters and
the total number of runs of the fastest (internal) loop.
Stub and wrap functions
are a sort of complementary to each other:
the former have the same interface as the intended
underlying (or targeted) functions, but different
functionality. The latter have the same basic
functionality but alter the interface.
- A stub function "fakes" functionality
(see Text, p.224). In most cases, stubs are temporary
implementations, which you use can use
for debugging purposes (to check data flow in your
program - to/from functions, to substitute calculations
in a function by an artificially assigned convenient
value, etc.) See Programming Note "Isolation
Testing" on p.225 of the Text.
Stubs allow you to concentrate on one thing a
time, rather than disperse attention to different
parts of the program.
In this example,
the final goal is (supposedly) to implement some mathematical
algorithm for computing mysin(x), but this is complicated
(unless using <math.h>'s function is acceptable in the
project). The programmer may first to organize all other
things in the program using a stub for mysin. It would
provide much more comfort for subsequent (hypothetical)
hard math.
-
A wrap (or wrapper) function alters the format of function
call (the function signature) -- such things as function name, order
and number of parameters, sometimes their types, etc.
It may also alter functionality of the underlying
function -- usually adding relatively minor features,
such as printing out function's arguments,
fixing values of some parameters etc.
The main application of wraps is coordination of
codes written by different programmers and or under
differerent assumptions (format, generality).
Here is an example.