Notes On Algorithms Pseudocode and Flowc
Notes On Algorithms Pseudocode and Flowc
Introduction
Do you like hot sauce? Here is an ‘algorithm’ for how to make a good one:
Volcanic Hot Sauce (from: http://recipeland.com/recipe/v/Volcanic-Hot-Sauce-1125)
10-12 scotch bonnets or Habanero, serrano, jalapeno
6 cloves Garlic, peeled and chopped
1/ 3 c Fresh lime juice
1/ 3 c Distilled white vinegar
2 tbl Dijon style mustard
2 tbl Olive oil
1 tsp Molasses
1/ 2 tsp Turmeric
1 tbl Salt or to taste
1. Combine the pepper, garlic, lime juice, vinegar, mustard, oil, molasses, turmeric, and salt in a
blender and puree until smooth. Correct the seasoning, adding more salt or molasses to taste.
2. Transfer the sauce to a clean bottle. You can use it right away, but the flavor will improve if you
let it age for a few days. Volcanic Hot Sauce will keep almost indefinitely, refrigerated or at room
temperature. Just give it a good shake before using.
As you can see, this ‘algorithm’ is a really a recipe, that is, a set of step-by-step instructions that
takes raw ingredients and produces a tasty result. In general, an algorithm can be described as a
procedure to solve a problem.
In the context of computer programming, an algorithm, is defined as a:
“well-ordered collection of unambiguous and effectively computable operations, that
when executed, produces a result and halts in a finite amount of time.”1
Characteristics of an Algorithm
à Well-ordered: the steps are in a clear order
à Unambiguous: the operations described are understood by a computing agent without
further simplification
à Effectively computable: the computing agent can actually carry out the operation
1
definition from: An Invitation to Computer Science (Gersting/Schneider) via
http://www.cs.xu.edu/csci170/08f/sect01/Overheads/WhatIsAnAlgorithm.html (visited 19JUN2009)
2
Corrado; B. and Jacopini, G. (May 1966). "Flow Diagrams, Turing Machines and Languages with Only Two
Formation Rules". Communications of the ACM 9 (5): 366–371.
3
Some programmers also include data declarations in their pseudocode. I think this is a good idea, because it helps
you keep track of the variables that you will need in the algorithm and can help you think through what data types
are needed.
4
Some programmers will add an ending ‘keyword’ on a separate line to make it explicit where a selection or
repetition structure ends, for example: ENDI F, ENDWHI LE, etc. On the one hand, this is good because it makes it
clear where the selection or repetition block ends, but on the other hand it adds to the length of the pseudocode, and
such statements will not ‘translate’ into an actual line of code in C. In a language like Pascal however, they will.
5
See “Pseudocode Standard” at http://users.csc.calpoly.edu/~jdalbey/SWE/pdl_std.html for more examples.
Pseudocode Example
Express an algorithm to get two numbers from the user (dividend and divisor), testing to
make sure that the divisor number is not zero, and displaying their quotient using pseudocode
1. Declare variables: dividend, divisor, quotient
2. Prompt user to enter dividend and divisor
3. Get dividend and divisor
4. IF divisor is equal to zero, THEN
4.1. DO
4.1.1. Display error message, “divisor must be non-zero”
4.1.2. Prompt user to enter divisor
4.1.3. Get divisor
4.2. WHILE divisor is equal to zero
5. ENDIF
6. Display dividend and divisor
7. Calculate quotient as dividend/divisor
8. Display quotient
Flowcharts
à A graphical tool that diagrammatically depicts the steps and structure of an algorithm or
program
à Symbols6,7 (the most commonly used ones)
6
For a comprehensive tutorial on flowcharting, see: Chapin, N. (1970). Flowcharting With the ANSI Standard: A
Tutorial, ACM Computing Surveys (CSUR), vol. 2, issue 2, pp. 119 – 146.
7
MS Word incorporates flowchart symbols in the Draw toolbar. After drawing a symbol, right click to pop-up and
select, ‘Add text’, to easily insert a text expression into the symbol.
Start
Prompt user to
enter dividend
and divisor
Display dividend
and divisor
If Yes
divisor = 0
Display error
message, “divisor
No
must be non-zero”
Prompt user to
Calculate quotient as enter divisor
dividend/divisor
Stop