This chapter provides many examples of recursive procedures and functions. Studying them should give you some appreciation of the power of recursion as a problem-solving and programming tool and should provide you with valuable insight regarding its use. It may take some time to feel comfortable thinking in this new way about programming, but it is certainly worth the effort.
Storage_Error
?
IF condition THEN Perform recursive step END IF;
IF
statement.
1 + 1/1!+ 1/2!+ ...+1/N!
until additional terms do not affect the approximation.
PROCEDURE ELog IS ENL: Float; Delta: Float; Fact: Float; N: Float; BEGIN -- Elog ENL := 1.0; N := 1.0; Fact := 1.0; Delta := 1.0; LOOP ENL := ENL + Delta; N := N + 1.0; Fact := Fact * N; Delta := 1.0 / Fact; EXIT WHEN ENL = (ENL + Delta); Ada.Text_IO.Put(Item => "The value of e is "); My_Flt_IO.Put(Item => ENL, Fore => 3, Aft => 15, Exp => 0); END Elog;
Grid
(see
Fig. 13.9). The first character of row 1
corresponds to Grid(1,1)
, the second character to
Grid(1,2)
, and so on. Set the element value to Empty
if the character is blank; otherwise, set it to Filled
. The number
of rows in the array should be read first. Use this procedure in a program that
reads in cell coordinates and prints the number of cells in the blob containing
each coordinate pair.
n! C(n, r) = ----------- r!(n - r)!
Write and test a function for computing C(n, r) given that n! is the factorial of n.
F(X, Y) = X - Y if X or Y < 0
F(X, Y) = F(X - 1, Y) + F(X, Y - 1) otherwise
('A', 'C', 'E', 'G') => ('A', 'C'), ('A', 'E'), ('A', 'G'), ('C', 'E'), ('C', 'G'), ('E', 'G')
'X'
or a
blank. Starting at position (1, 1), list any path through the maze to get to
location (8, 8). Only horizontal and vertical moves are allowed (no diagonal
moves). If no path exists, write a message indicating this. Moves can be made
only to locations that contain a blank. If an 'X'
is encountered,
that path is blocked and another must be chosen. Use recursion.
For a set of values of X to bracket a root the value of the function for one X must be negative and the other must be positive. This is illustrated in Figure 13.12, which plots f(X) for values of X between X1 and X2.
Figure 13.12
Diagram for Bisection Method
The algorithm requires that the midpoint between the left X and the right X be evaluated in the function. If the midpoint equals zero, the root is found; otherwise, the left X (X1) or right X (X2) is set to this midpoint. To determine whether to replace either X1 or X2, the sign of the midpoint is compared against the signs of the values of f(X1) and f(X2). The midpoint replaces the X (X1 or X2) whose function value has the same sign as the function value at the midpoint.
This routine can be written recursively. The terminating conditions are true when either the midpoint evaluated in the function is zero or the absolute value of the left minus the right X is less than some small predetermined value (e.g., 0.0005). If the second condition occurs, the root is said to be approximately equal to the midpoint of the last set of left and right Xs.
MergeSort
procedure.
Figure
13.13
Merge Sort
Ada.Characters.Handling
to write a function which
takes a string S
as a parameter and returns a string representing
S
with all letters converted to uppercase, as well as blanks and
punctuation removed. "Madam, I'm Adam"
would be converted to
"MADAMIMADAM"
. Use the result of calling this function as the
input to StringReverse
.)
BinarySearch
into a procedure with two output
parameters: the location of the key if found and a flag indicating whether or
not the key was found.
BinarySearch
of Programming Project 9 as
a starting point, write a generic binary search procedure that can handle any
array with any nonlimited element type and any index type.
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.