Comp 150 -
Algorithms & Pseudo-Code
(revised 01/11/2008)
Definition of Algorithm (after Al Kho-war-iz-mi a 9th century Persian mathematician) - an ordered sequence of unambiguous and well-defined instructions that performs some task and halts in finite time
Let's examine the four parts of this definition more closely
Algorithms
can be executed by a computing agent which is not necessarily a computer.
Three Catagories of Algorithmic Operations
Algorithmic operations are ordered in that there is a first instruction, a second instruction etc. However, this is not enough. An algorithm must have the ability to alter the order of its instructions. An instruction that alters the order of an algorithm is called a control structure
Three Categories of Algorithmic Operations:
Unfortunately not every problem or task has a "good" algorithmic solution. There are
How to represent algorithms?
Pseudo-Code is simply a numbered list of instructions to perform some task. In this course we will enforce three standards for good pseudo code
Pseudo-code
is best understood by looking at examples. Each example below demonstrates one
of the control structures used in algorithms :
sequential operations, conditional operations, and iterative operations.
We also list all variables used at the end of the pseudo-code.
Example #1 - Computing Sales Tax : Pseudo-code the task of computing the final price of an item after figuring in sales tax. Note the three types of instructions: input (get), process/calculate (=) and output (display)
1. get price of item
2. get sales tax rate
3. sales tax = price of time times sales tax rate
4 final prince = price of item plus sales tax
5. display final price
6. halt
Variables:
price of item, sales tax rate, sales tax, final price
Note that the operations are numbered and each operation is unambiguous and effectively computable. We also extract and list all variables used in our pseudo-code. This will be useful when translating pseudo-code into a programming language
Example #2 - Computing Weekly Wages: Gross pay depends on the pay rate and the number of hours worked per week. However, if you work more than 40 hours, you get paid time-and-a-half for all hours worked over 40. Pseudo-code the task of computing gross pay given pay rate and hours worked.
1. get hours worked
2. get pay rate
3. if hours worked ≤ 40 then
3.1 gross pay = pay rate times hours worked
4. else
4.1 gross pay = pay rate times 40 plus 1.5 times pay rate times (hours worked minus 40)
5. display gross pay
6. halt
variables: hours worked, ray rate, gross pay
This example introduces the conditional control structure. On the basis of the true/false question asked in line 3, we execute line 3.1 if the answer is True; otherwise if the answer is False we execute the lines subordinate to line 4 (i.e. line 4.1). In both cases we resume the pseudo-code at line 5.
Example #3 - Computing a Quiz Average: Pseudo-code a routine to calculate your quiz average.
1. get number of quizzes
2. sum = 0
3. count = 0
4. while count < number of quizzes
4.1 get quiz grade
4.2 sum = sum + quiz grade
4.3 count = count + 1
5. average = sum / number of quizzes
6. display average
7. halt
variables: number of quizzes, sum ,count, quiz grade, average
This example introduces an iterative control statement. As long as the condition in line 4 is True, we execute the subordinate operations 4.1 - 4.3. When the condition becomes False, we resume the pseudo-code at line 5.
This is an example of a top-test or while do iterative control structure. There is also a bottom-test or repeat until iterative control structure which executes a block of statements until the condition tested at the end of the block is False.
Pseudo-code is one important step in the process of writing a program.
Pseudo-code Language Constructions : A Summary
Computation/Assignment
set the value of
"variable" to :"arithmetic
expression" or
"variable" equals
"expression" or
"variable" = "expression"
Input/Output
get "variable",
"variable", ...
display
"variable", "variable", ...
Conditional (dot notation used for numbering subordinate statements)
6. if "condition"
6.1 (subordinate) statement 1
6.2 etc ...
7. else
7.1 (subordinate) statement 2
7.2 etc ...
Iterative (dot notation used for numbering subordinate statements)
9. while "condition"
9.1 (subordinate) statement 1
9.2 etc ...