Ch1

advertisement
Chapter 1
Program design
Objectives




To describe the steps in the program development process
To explain structured programming
To introduce algorithms and pseudocode
To describe program data
Detailed Lecture Notes
Programming is more than just math skills and an unusually high tolerance for sitting in
front of a computer. It also requires preparation and a methodical approach to the task at
hand. This methodical approach minimizes backtracking and reworking of code.
Savvy programmers using the following seven steps when developing a program:
1. Define the problem
 Inputs
 Outputs
 The processing steps involved
Quick Quiz: Give the inputs, outputs, and processing steps for the following problems:
a. You want to effectively study for an exam. Your roommate is running the
stereo at a very high volume. What are the inputs, outputs, and processing steps
for this problem?
b. You are sitting in your car at a gas station. One attendant is on duty. This
person is busily helping customers. You would like to ask this person for
directions to your destination. What are the inputs, outputs, and processing steps
for this problem?
2. Outline the solution
 Major processing steps involved
 Major subtasks
 Major control structures
 Major variables and record structures
 Mainline logic
Discussion Topic: Many students are tempted to skip Step 2 of the development process
and move right into developing an outline for an algorithm. Explain why this
would not be a good idea and the problems that this approach may create.
3. Develop the outline into an algorithm
 Expand the outline solution into an algorithm
4. Test the algorithm for correctness
 One of the most important steps
 Programmer plays the role of the computer
5. Code the algorithm into a specific programming language
6. Run the program on the computer
 Syntax errors
 Logic errors
7. Document and maintain the program
 Though listed last, actually occurs throughout the development process
 External documentation
 Internal documentation
 Self-documentation
In the top-down development approach, the programmer begins by defining a general
solution to the problem. Then the programmer breaks the solution down into smaller and
smaller steps. In the modular design approach, the programmer groups together tasks
that perform the same function. In the Structure Theorem approach, the programmer
writes the computer program by using only the following control structures: sequence,
selection, and repetition.
An algorithm lists the steps involved in accomplishing a task. Programmers write an
algorithm in simple English. Though an algorithm is not a formal document, it should
still be precise, contain the correct solution, and have an ending.
Quick Quiz: What is the purpose of indentation in an algorithm?
Programmers use pseudocode to represent an algorithm because pseudocode is easy to
write. Pseudocode is structured English that has been formalized and abbreviated to
resemble high-level computer languages. Pseudocode usually has the following
characteristics:
 Statements are written in simple English.
 Each instruction is written on a separate line.
 Keywords and indentation are used to signify particular control structures.
 Each set of instructions has only one entry and one exit.
 Groups of statements may be formed into modules.
To write effective programs, programmers must understand the nature and structure of
the data being processed by the program and the terms used in conjunction with data:



A variable is the name given to a collection of memory cells that is
designed to store a particular data item. The variable may change or vary
as the program executes.
The name and value of a constant remain the same during the execution of
a program.
A constant whose name is the written representation of its value is a
literal.
An elementary data item is one containing a single variable that is always treated as a
unit. Data items are usually classified into these data types:
 Integer
 Real
 Character
 Boolean
A data structure is an aggregate of other data items. In a data structure, data is grouped in
a deliberate way. These are the most common data structures:
 Record: a collection of data items or fields
 File: a collection of records
 Array: contains variables or data items that have the same data type and
that are accessed by the same name
 String: a collection of characters
Group Activities
1. Choose a partner for this exercise. Write an algorithm that lists the steps that you
would take to walk from your classroom to the nearest vending machine. Have your
partner create the same algorithm. Compare your algorithms. Are they identical? If
they are identical, explain why. If they are not identical, why do you think this is so?
2. In programming, a record is a collection of data items or fields that bear some
relationship to one another. For instance, a record may contain a student’s ID
number, name, address, and telephone number. With a partner, create a list of five
record types that may exist within a university system. For each record type, list the
typical data items or fields that the record may contain.
Solutions
There are no solutions for this chapter; this chapter did not contain programming
problems.
Download