CMSC 104: Peter Olsen, Fall 99 Lecture 9:1
Algorithms III
Representing Algorithms
with
pseudo-code
CMSC 104: Peter Olsen, Fall 99 Lecture 9:2
Goal
A method for representing algorithms that:o Is easy to use (or it won’t be)o Clear and precise (or why bother?)o Understandable by others without
special training.
CMSC 104: Peter Olsen, Fall 99 Lecture 9:3
Algorithm Definition
• An algorithm for a problem is a sequence of finite number of steps arranged in a specific logical order which, when executed, will produce a correct solution for a specific problem.
“An algorithm is any finite sequence of steps connecting a problem with its solution.”
CMSC 104: Peter Olsen, Fall 99 Lecture 9:4
Requirements for a Solution Algorithm
– Unambiguous– Finite– Defined Input– Desired Output– Generality– Correctness– Efficiency
CMSC 104: Peter Olsen, Fall 99 Lecture 9:5
Algorithm Representations
• English• Pseudo-code (“Structured English”)• Flow chart
CMSC 104: Peter Olsen, Fall 99 Lecture 9:6
Algorithms in English
To find a zero using Newton’s Methodo Begin the procedure by letting x be the current
point.o Using their definitions, calculate f(x) and f’(x).o If f’(x) is less than an error tolerance, e, quit.o If it is not, calculate the next point:
y = x - f(x)/f’(x)o Continue until f(x) is “close enough” to zero. x is
the zero of f.
CMSC 104: Peter Olsen, Fall 99 Lecture 9:7
Algorithms in English
Easy to understand. Easy to use. Not very clear or precise.
CMSC 104: Peter Olsen, Fall 99 Lecture 9:8
Pseudo-code
• English like• Semi-formal• Limited vocabulary • Intended specifically to design and describe
algorithms.
CMSC 104: Peter Olsen, Fall 99 Lecture 9:9
Pseudo-code
• The main purpose of a pseudo-code is to define the procedural logic of an algorithm in a simple, easy-to-understand manner for readers, who may or may not be proficient in computer programming.
CMSC 104: Peter Olsen, Fall 99 Lecture 9:10
Pseudo-code
• Used in designing algorithms.• Used in communicating to users.• Used in implementing algorithms as
programs.• Used in debugging logic errors in programs.• Used in documenting programs for future
maintenance and expansion purposes.
CMSC 104: Peter Olsen, Fall 99 Lecture 9:11
Pseudo-code
• Must have a limited vocabulary.• Must be easy to learn.• Must produce simple, English-like narrative
notation.• Must be capable of describing all
algorithms, regardless of their complexity.
CMSC 104: Peter Olsen, Fall 99 Lecture 9:12
Control Structures of All Computer Programs
• Sequence• Selection• Repetition
CMSC 104: Peter Olsen, Fall 99 Lecture 9:13
Sequence
• Series of steps or statements that are executed in the order they are written.
• Example:Read taxable incomeRead filing statusCompute income taxPrint income tax
CMSC 104: Peter Olsen, Fall 99 Lecture 9:14
Sequence
• A sequence can be thought of as one logical step.
In OutSequence
CMSC 104: Peter Olsen, Fall 99 Lecture 9:15
Selection
• Defines one or two courses of action depending on the evaluation of a condition.
• A condition is an expression that is either true or false.
• Example if condition (is true) then-partelse else-partend_if
CMSC 104: Peter Olsen, Fall 99 Lecture 9:16
Nested Selection
if status is equal to 1 print “Single”else if status is equal to 2 print “Married filing jointly” else if status is equal to 3 print “Married filing separately end_if end_ifend_if
CMSC 104: Peter Olsen, Fall 99 Lecture 9:17
Nested Selection
SINGLE = 1; MARRIEDJOINT = 2; MARRIEDSEP = 3if status is equal to SINGLE print “Single”
else if status is equal to MARRIEDJOINT print “Married filing jointly” else if status is equal to MARRIEDSEP print “Married filing separately end_if end_ifend_if
CMSC 104: Peter Olsen, Fall 99 Lecture 9:18
Nested Selection
Defines a logical block with one entrance, one exit, and two or more paths inside.
If
Endif
Input
Output
CMSC 104: Peter Olsen, Fall 99 Lecture 9:19
Repetition
• Specifies a block of one or more statements that are repeatedly executed until a condition is satisfied.
• while condition loop-bodyend_while
CMSC 104: Peter Olsen, Fall 99 Lecture 9:20
Repetition
• Whilewhile m is less than m, … end while
• For loopfor m from 1 to n, … end for
• Untildo … until m is less than n, end do
CMSC 104: Peter Olsen, Fall 99 Lecture 9:21
Repetition
A logical block with one input, one output, and one return loop.
While
EndWhile
CMSC 104: Peter Olsen, Fall 99 Lecture 9:22
Conventions
• Each pseudo-code statement consists of keywords that describe operations and some appropriate, English-like description of operands.
• Each statement should be written on a separate line. Continuation lines should be indented.
• Goal is clarity
CMSC 104: Peter Olsen, Fall 99 Lecture 9:23
Conventions (II)
• Sequence statements should begin with unambiguous words (verbs -- compute, set, initialize).
• Selection statements then-part and else-part should be indented.
• Selection statements end with the keyword end_if.
CMSC 104: Peter Olsen, Fall 99 Lecture 9:24
Conventions (III)
• Repetition statements end with end_while.• Loop-bodies are indented.• All words in a pseudo-code statement must
be chosen to be unambiguous, and as easy as possible to understand by non-programmers.
• Enclose comments between /* and */
CMSC 104: Peter Olsen, Fall 99 Lecture 9:25
Example
Set correctIncomeInput to ‘n’
while correctIncomeInput is equal to ‘n’
print “Enter taxable income: Should not be less than 50000 read income
if income is equal to or greater than 50000 set correctIncomeInput to ‘y’ end_if
end_while
CMSC 104: Peter Olsen, Fall 99 Lecture 9:26
Flowcharting
• Flowcharting is another technique used in designing and representing algorithms.
• A flowchart is a graph consisting of geometric shapes that are connected by flow lines.
CMSC 104: Peter Olsen, Fall 99 Lecture 9:27
Symbols
Terminal symbol
Process symbol
Input-Output symbol
CMSC 104: Peter Olsen, Fall 99 Lecture 9:28
Symbols (III)
Flow Lines indicating the logical sequence of statements
CMSC 104: Peter Olsen, Fall 99 Lecture 9:29
Flowchart
If
Keyboard
Process