Attachment

Submitted by: Submitted by

Views: 10

Words: 14968

Pages: 60

Category: Other Topics

Date Submitted: 04/18/2016 05:56 AM

Report This Essay

Chapter 3

Designing Programs Top Down

As program tasks become more complex, it is easier to think about the problem and design the

algorithm for the task at hand by breaking the complex task into smaller and simpler subtasks

and then solve each of the subtasks independently. We do this all the time in everyday life, for

example, suppose you need milk for your kid's dinner. A complete algorithm for solving this

problem might begin:

find the car keys

go to the garage

get in the car

put the key in the ignition

start the car

back the car out of the driveway

...

However when we are worried about feeding the kids, we do not plan our algorithm in such detail.

Instead our algorithm might be:

drive to the store

buy milk

drive home

where each of the steps in this algorithm is a subtask that may involve many steps itself.

We can do the same kind of modular design for our programming tasks: begin by thinking at a

more abstract level about the major steps to be done, and then for each of these subtasks, design a

separate algorithm to solve it. Each program subtask may then be implemented either by a set of

statements or by a separate function. The advantages of a function are that it hides details of the

actual computations from the main body of the code, and it can even be called upon to perform

a subtask repeatedly by one or more other functions. In particular, well designed functions can

89

CHAPTER 3. DESIGNING PROGRAMS TOP DOWN

90

Payroll

Task

?

?

print

pay disbursed

(loop)

proc 1 empl.

?

read

data

?

calculate

pay

?

update

cum. total

?

print

pay

Figure 3.1: Structural Diagram for Payroll Task

be used in a variety of programs. (An example from the above might be driving it is the same

operation in the rst and last steps of our algorithm only the start and destination are di erent).

In this chapter we will discuss this method of modular design of algorithms and the programs

that implement them. We will see how functions may be used in a C program, and how...