Instructor's Manual for

M.B. Feldman and E.B. Koffman, Ada 95 Problem Solving and Program Design, 3rd edition. Copyright 1999, Addison-Wesley Publishing Company. All Rights Reserved

Questions and comments to mfeldman@seas.gwu.edu


Chapter 2 Introducing Algorithms: the Spider

last revised March 1999

Chapter Objectives

The student will

  1. learn to use a book-supplied package
  2. learn the basic sequential, repetition, and selection control structures
  3. study and develop some simple algorithms using the basic control structures

New Terms

Section 2.1

standard package

book-provoded package

encapsulation

interface

implementation

operations or methods 

Section 2.2

straight-line algorithm

procedure

enumeration type

parameter

precondition

postcondition

Section 2.3

simple loop

loop body

loop counter

bracket

counter

random value

Section 2.4

nested loop

spiral pattern

Section 2.5

conditional execution

exception

Boolean operation

general loop structure

EXIT statement

Section 2.6

random walk

nonterminating program

Notes and Suggestions

General Suggestions

This chapter is optional; subsequent chapters (except for a few equally optional Spider sections) do not refer to it or depend upon its content.

The chapter responds to the desire of some instructors to give an early, quick introduction to the basics of control structures and algorithms that use these. This allows students to write more realistic and "fun" programs without having to wait for the full treatment of control structures in chapters 5 through 8.

As an experiment, I inserted this material into my own course during the Spring 1999 semester. Based on this experience, I've come to agree that it's a useful approach and will definitely continue it. As we all discovered years ago with LOGO and similar tools, many students respond well to a visual, "low overhead" approach to learning about algorithms. The immediate feedback provided by an obvious on-screen animation seems to be very attractive to them. They can experiment to get the spider do do what they want it to do, without having to cope with the other overhead of writing full-scale programs with variable declarations, explicit input/output, and so on.

The Spider environment presented in this chapter uses a very simple 24 by 80 terminal screen controller, which has the virtues of simplicity and portability. Instructors who wish to use a high-resolution color version of the spider will find details of such an environment described in Appendix A of the text; the necessary software is included on the CD-ROM and available on the Internet as well.

Section 2.1

The spider package is a simplified "turtle graphics" facility. Its specification is simple and intuitive. Like all such simple drawing packages, it provides instant feedback: A student writing and running a spider program sees its results in a drawing pattern that is easily seen to be correct or not.

Even a simple, 24 x 80 character package like this is fun and instructive. In my course, I often require the students to write a small spider program in addition to that week's full project. For example, in the week this chapter is covered, it is useful for the students to try to write a program that draws a "yield" sign, i.e.,

XXXXXXX
 X   X
  X X
   X

Since they have not learned control structures yet, this is straightforward but cumbersome. Getting the spider from the end of the first row to a correctly drawn second row makes the students focus carefully on the available operations.

Another benefit of the spider package is that its body is simple enough to be presented in Chapter 8 of this book, by which time most students will be able to understand the individual operations. I believe this approach is better than giving students a very elaborate graphics package, whose pictures may be prettier than the spider's, but whose implementation is well beyond a first-year student.

One student in my class asked me, during this unit, whether it was possible to do "graphics" in Ada. I responded that in fact the spider package is graphics. It happens to be low-resolution monochrome instead of high-resolution color, but many of the principles are the same. I explained the value of having portable, if somewhat crude, graphics, mentioned that the class would discuss the "innards" of the spider package along with Chapter 8.

Section 2.3

This section introduces simple FOR structures and also the methods RandomColor and RandomDirection. It is very instructive for students to run Program 2.7 several times (perhaps with the debug switch on) to observe the random nature of these methods.

Section 2.4

As every teacher or student of Logo learns very quickly, nested loops, combined with simple drawing commands, can be used to produce very nice patterns. This section introduces several such patterns; you might assign the students to write one or two more. One might be to use nested loops to draw a "checkerboard" such as

X X X X
 X X X X 
X X X X
 X X X X

Section 2.5

In this section, conditional execution is used to prevent the spider from crashing into a wall. It is a very simple form of robust program design; robustness is a recurring theme throughout the book.

Section 2.6

The Drunken Spider program is a simple example of a random walk. It is also a nonterminating program, which runs continuously until stopped by a ctrl-C or other external abort. This is a good opportunity to discuss applications of nonterminating programs. In this era of very heavy use of embedded computers and real-time programs, we should start early to foster the idea that programs should terminate when they are designed to do so, and not terminate when they are designed not to do so!