Chapter 1 Algorithms
PEOPLE LIKE DEFINITE DECISIONS, TIDY ANSWERS, ALL THE
LITTLE RAVELLINGS SNIPPED OFF, THE LINT REMOVED....
Gwendolyn Brooks, Annie Allen, 1949
OBJECTIVES
To acquire a better sense of the process of algorithm development by investigating
several algorithms from classical mathematics.
To recognize that most problems can be solved using any of several different
algorithms.
To understand the considerations involved in choosing between alternative
algorithms.
To gain a sense of how to establish that an algorithm is correct.
To understand and be able to apply the concept of a brute-force algorithm.
To be able to write programs that use the technique of successive approximation.
To learn how to use the Error function to report error conditions to the user.
To be able to write programs that perform series expansions.
Functions are important to programming in part because they provide a basis for the
implementation of algorithms. The algorithm itself is the abstract strategy and is often
expressed in English. The function is the concrete realization of that algorithm in the
context of a programming language. When you want to implement an algorithm as part of
a program, you will usually write a function—which may in turn call other functions to
handle part of its work—to carry out that algorithm.
You have seen several simple algorithms implemented in the context of the sample
programs, but you have not had a chance to focus on the nature of the algorithmic process
itself. Most of the programming problems you have seen so far are simple enough that the
appropriate solution technique springs immediately to mind. As problems become more
complex, however, the solution strategies require more thought, and you will need to
consider more than one strategy before writing the final program.
This chapter illustrates how algorithmic strategies take shape by solving several
problems from classical mathematics, each of which can be approached in a variety of
ways. By looking at more than one solution to each problem, you can get a sense of how to
compare different strategies and choose among them.