0% found this document useful (0 votes)
50 views

Pseudocode Notes

Uploaded by

Mazvita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Pseudocode Notes

Uploaded by

Mazvita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

PSEUDOCODE

What is a Pseudocode? Constants Declarations:


A pseudocode is a standard method of showing an ▪ Constants are normally declared at the beginning
algorithm (an algorithm is a series of steps and of a piece of pseudocode (if required).
instructions to achieve a task). Pseudocode uses English ▪ Constants are declared by stating the identifier
keywords that are very similar to those used in high level and the literal value in the following format:
programming. Pseudocode is not bound by the strict
syntax rules of a programming language. CONSTANT <identifier> = <value>

▪ Example – constant declarations


Variables, Constants and Data Types:
CONSTANT HourlyRate = 6.50
Data Types: CONSTANT DefaultText = "N/A"
The following keywords are used to designate some basic Array Declarations
data types: ▪ Arrays are considered to be fixed-length
▪ INTEGER: a whole number e.g. 5, -3 structures of elements of identical data type,
▪ REAL: a number capable of containing a fractional accessible by consecutive index (subscript)
part e.g. e.g. 4.7, 0.3, –4.0, 0.0 numbers.
▪ CHAR: a single character e.g. ꞌxꞌ, ꞌCꞌ, ꞌ@ꞌ ▪ It is good practice to explicitly state what the
▪ STRING: a sequence of zero or more characters e.g. lower bound of the array (i.e. the index of the first
"This is a string", " " (empty string) element) is because this defaults to either 0 or 1
▪ BOOLEAN: the logical values TRUE and FALSE in different systems. Generally, a lower bound of
▪ DATE: a valid calendar date 1 will be used.
Identifiers: ▪ Square brackets are used to indicate the array
▪ Identifiers (the names given to variables, constants, indices.
procedures and functions) are in mixed case.
A one-dimensional array is declared as follows:
▪ They can only contain letters (A–Z, a–z), digits (0–9)
and the underscore character ( _ ). DECLARE <identifier>: ARRAY
▪ They must start with a letter and not a digit. Accented [<lower>:<upper>] OF <datatype>
letters should not be used.
Example – array declarations
▪ It is good practice to use identifier names that
describe the variable, procedure or function they DECLARE StudentNames : [1:30] OF STRING
refer to.
2. Assignment
▪ Single letters may be used where these are
conventional (such as i and j when dealing with array Assignment operator is ←
indices, or X and Y when dealing with coordinates) as
these are made clear by the convention. Assignments should be made in the following format:
<identifier> ← <value>
Elements of Pseudocode
For example:
▪ Declaration
▪ Assignment Statement ▪ A value is assigned to an item/variable.
▪ Input & Output Variable ← Value
▪ Conditional Statement ▪ A variable is a named data store that contains a value
▪ Iterative Statement that may change during execution of a program.
▪ The value on the right can have one value or multiple
1. Declaration:
values combined with mathematical operators.
Variable Declarations
▪ It is good practice to declare variables explicitly One Value: Multiple Values:
in pseudocode. x←5 x←5*6
▪ Declarations are made as follows:
DECLARE <identifier> : <data type> Name ← “John” Name ← “Hello” + “John”
▪ Example – variable declarations ▪ The values on the right can have one or multiple
DECLARE Counter : INTEGER values combined with mathematical operators.
DECLARE TotalToPay : REAL
DECLARE GameOver : BOOLEAN

Page 1 | 13
Example Assignment Question: DECLARE, Principal, Time : INTEGER
Given that: Cost ← 5 DECLARE Interest: REAL
Create the following variables: CONSTANT Rate = 0.08
OUTPUT “Enter Principal Amount: ”
1. Price – twice the value of cost
INPUT Principal
2. Tax – 12% of the Total Price
OUTPUT “What is the Period? ”
3. Selling Price – Price plus Tax INPUT Period
4. Gender variables containing the value “M” Interest ← Principal * Rate * Time
5. Chosen variable containing the value FALSE OUTPUT “Interest Earned = ”, Interest
Solution: 4. Selection/Conditional Statements:
1. Cost ← 5 ▪ An element of pseudocode which allows the
2. Price ← Cost * 2 algorithm to do different things depending on the
3. Tax ← 0.12 * Price output scenario
4. SellingPrice ← Price + Tax
▪ Selection is a very useful technique, allowing
5. Gender ← “M”
different routes through the steps of a program.
6. Chosen ← FALSE
For example, data items can be picked out
3. INPUT & OUTPUT according to given criteria, such as: selecting the
INPUT: Used for data entry largest value or smallest value, selecting items
It is usually followed by a variable where the data over a certain price, selecting everyone who is
input is stored male.
INPUT variable There are two selection/conditional statements:
e.g. INPUT Cost 1. IF…THEN…ELSE…ENDIF
OUTPUT: Used to display information on a screen 2. CASE OF…OTHERWISE…ENDCASE
OUTPUT variable Examples 1 (Single Condition):
e.g. OUTPUT SellingPrice IF…THEN…ELSE…ENDIF
Program Instruction: Construct an IF statement that
Example INPUT/OUTPUT
awards a 15% discount if the total price of an item is
//DECLARATION (This is a Comment)
DECLARE Cost: INTEGER greater than $500; also display the discounted price of
DECLARE Price: REAL the item.
DECLARE Tax: REAL Solution:
DECLARE SellingPrice: REAL OUTPUT “Please enter Total Price: ”
//ASSIGNMENT (This is a Comment) INPUT TotalPrice
OUTPUT “Enter the Cost” IF TotalPrice > $500
INPUT Cost THEN
TotalPrice←TotalPrice * 0.15
Price ← Cost * 2
OUTPUT TotalPrice
Tax ← 0.12 * Price
ENDIF
SellingPrice ← Price + Tax
(please note the indentation of the selection statement)
OUTPUT SellingPrice
Examples 2 (more than two conditions – nested IFS):
Program Instructions: Construct an IF statement that will
Write a pseudocode to calculate and output the area
generate grades for students based on marks. If marks is
of a rectangle (whole numbers used):
greater or equal to 80, display “A”, if marks is greater or
DECLARE Length: INTEGER equal to 50, display “B”, otherwise display Fail.
DECLARE Width: INTEGER
DECLARE Area: INTEGER DECLARE Marks: INTEGER
INPUT Marks
OUTPUT “Enter the Length” IF Marks >= 80
INPUT Length THEN
OUTPUT “Enter the Width” OUTPUT “A”
ELSE
INPUT Width
IF Marks >= 50
Area ← Length * Width THEN
OUTPUT “The Area is: ”, Area OUTPUT “B”
ELSE
Write a pseudocode to calculate simple interest OUTPUT “Fail”
earned on a deposit $6000 for a fixed period of 6 ENDIF
years at 8% p.a. ENDIF

Page 2 | 13
Using Comparison Operators: Iterative/Repeat Statements:
Operator Comparison ▪ FOR…TO…NEXT
> Greater Than ▪ WHILE…DO…ENDWHILE
▪ REPEAT…UNTIL
< Less Than
= Equal to
FOR…TO…NEXT Statement:
>= Greater Than or Equal To ▪ Used when the number of repetitions are known
<= Less Than or Equal To ▪ A variable is set up with a start and end value.
<> Not Equal ▪ This variable is incremented in steps of one until the
AND Both end value is reached and the iteration finishes.
▪ The loop is useful for reading values into lists/arrays
OR Either
with known length.
NOT Not
Application of FOR…TO…NEXT Loops:
Example 3 (while using comparison operators):
Construct IF statement(s) that will display the messages: Example 1:
“Invalid Mark”, “Pass” or “Fail” given the following Consider this pseudocode: The user is required to enter
condition: numbers 4 times. Using “INPUT Number” line many
- IF the Percentage Mark is less than 0 OR Percentage times is tedious and makes your pseudocode untidy.
Mark is greater than 100, then display “Invalid Mark” Suppose there is a way to write only once and the system
- IF the Percentage Mark is greater than 50, then display keeps repeating the “INPUT Number” line for the given
“Pass” the “4 times. Cool!
- Else display “Fail”
OUTPUT “Enter Number: ”
Solution: INPUT Number
INPUT Marks OUTPUT “Enter Number: ”
IF (Marks < 0) OR (Marks > 100) INPUT Number
THEN OUTPUT “Enter Number: ”
OUTPUT “Invalid Mark” INPUT Number
ELSE OUTPUT “Enter Number: ”
IF Marks>50 INPUT Number
THEN
OUTPUT “Pass” Using the FOR…TO…NEXT
ELSE
OUTPUT “Fail”
ENDIF FOR Times = 1 TO 4
ENDIF OUTPUT “Enter Number”
INPUT Number
CASE OF…OTHERWISE…ENDCASE NEXT Times
For a CASE statement the value of the variable decides
the path to be taken. Several values are usually specified. In this pseudocode, the message “Enter Number” will
keep prompting the user to enter a number at every
Example 1: loop/repeat until the last number (4th) has been entered.
OUTPUT “Please Enter Grade”
INPUT Grade FOR…TO…NEXT WITH 1-DIMENSIONAL ARRAY:
CASE OF Grade What is an Array?
“A”: OUTPUT “Excellent” ▪ An array is a data structure containing several
“B”: OUTPUT “Good” elements of the same data type.
“C”: OUTPUT “Encouraging” ▪ Elements in an array can be accessed using the same
OTHERWISE: OUTPUT “Invalid Entry” identifier name.
ENDCASE ▪ The position of each element in an array is identified
using the array index.
5. Iterative Statements:
An element of pseudocode which allows the algorithm to
repeat certain actions.

Page 3 | 13
Array Elements: WHILE…DO…ENDWHILE Statement:
Declaring and assigning values (elements) to arrays ▪ Used when the number of repetitions is NOT known
▪ Actions only repeated WHILE a given condition is
The table below is a hypothetical representation of how true
an array would look like: ▪ If the WHILE condition is untrue, the actions in the
loop are never performed.
Array Marks: ▪ This is a PRE-CONDITION LOOP the test for exiting
Marks 70 50 100 75 the loop is at the beginning of the loop.
Index 0 1 2 3 4 5
WHILE…DO…ENDWHILE:
Each of the elements (Marks), which are of the type Example 1: Using Pseudocode & Python:
(Integer) can be accessed through use of the Array Name This pseudocode allows the user to INPUT to input
(Marks) and the Index. random numbers that are greater that 0 and sum them
e.g. to access value 70 thus: up, for an endless number of times. The loop only
OUTPUT Marks[0] stops/exit when value 0 is entered.

Input of value 50 into position, index 1, the empty space, DECLARE, Total, Num: REAL
can be done thus:
OUTPUT “Enter 0 to Exit”
INPUT Marks[1] ← 50
OUTPUT “Enter First Number”
INPUT Num
Array Declaration and Assignment: Total ← 0
The above array can be declared as follows:
DECLARE Marks: ARRAY[0:5] OF INTEGER WHILE NUM > 0 DO:
Total ← Total + Num
Assignment: OUTPUT Total
INPUT Marks[3] ← 58 OUTPUT “Enter 0 to Exit”
(Inputs value 50 into position with index 3) OUTPUT “Enter Next Number”
. INPUT Num
. ENDWHILE
. Python Equivalence of the above pseudocode:
OUTPUT Marks[3]
print("Enter 0 to Exit")
Output the value in position with index 2, that is 50.
Num = int(input("Enter First Number: "))
Total = 0
Example: while Num > 0:
Consider this pseudocode: Using FOR…TO…NEXT to Total = Total + Num
populate values into an array (MyNo) of integers. print("The Total is: ", Total)
print("Enter 0 to Exit")
1D Array with length of 4 values. Num = int(input("Enter Next Number: "))

Array MyNo Example 2:


MyNo This pseudocode allows the user to INPUT Student’s
Index 1 2 3 4 5 6 Marks between values 0 and 100 for unknown number of
times and add them together. Entry of a value less than
DECLARE MyNo: ARRAY[1:6] OF INTEGERS 0 or greater than 100 exits the loop.
DECLARE Total: INTEGER
FOR Index = 1 TO 6 DECLARE Marks: INTEGER
OUTPUT “Enter the Number: ”
INPUT MyNo Total ← 0
NEXT Index OUTPUT “Enter 0 or 101 to finish/exit”
INPUT Marks
The FOR … TO … NEXT allow for repeat of number input WHILE Marks >= 0 OR Marks <= 100 DO
Total ← Total + Marks
into the array (MyNo) until all the six numbers have been
OUTPUT “Enter 0 or 101 to
entered and then the loop will stop after 6
finish/exit”
iterations/repeats. INPUT Marks
ENDWHILE

Page 4 | 13
REPEAT…UNTIL
▪ Used when the number of repetitions/iterations is
NOT known
▪ The actions are repeated UNTIL a given condition
becomes TRUE .
▪ The actions in this loop are always completed AT
LEAST ONCE.
▪ It is a POST CONDITION loop as the test for exiting
the loop is at the end of the loop.

Example 1:
A repetition where the number of repeats is not known,
that is completed at least once.
A condition (-1) determines whether we want to continue
our program.

DECLARE Number: REAL


REPEAT
OUTPUT “Input Any Number: ”
INPUT Number
UNTIL Number = -1

Example 2: What does the following code do?


DECLARE Total: INTEGER
DECLARE Mark: INTEGER
Total ← 0
Mark ← 0
REPEAT
Total ← Total + Mark
OUTPUT “Enter value for Mark -1 to
finish”
INPUT Mark
UNTIL Mark = -1

Answer: Each iteration asks the user for mark. The mark
given will be added to the total. The program stops when
the user enter value -1

Page 5 | 13
Pseudocode: Standard Methods of Solution
The ability to repeat existing methods is very important in design of algorithms; when an algorithm is
turned into a program the same method may be repeated many thousands of times.
Following are most standard methods used in algorithm:

✓ Totalling
✓ Counting
✓ Finding Minimum, Maximum & Average (Mean)
✓ Searching using Linear Search
✓ Sorting Using a Bubble Sort
Totalling:
Totalling is used with the repletion with the total updated every time the loop is repeated. Keeping a
running total is one of the frequently used programming techniques in many computer systems.
For example:
• Total marks on students on a number of subjects;
• Total a receipt at a supermarket checkout
Question:
Using a condition-controlled loop, write a pseudocode to allow the user to input student’s marks and
calculate the total marks of three subjects.
The table is used as an illustration to help form
an idea on how your loops will execute. You do
not need to draw it if you can visualize the idea!
Counter keyword can be used in place of index
to indicate loop counts; index is used to
enhance your understanding of the change in
loops count.

Pseudocode:

Python:

Marks = [0]*3
Total = 0
for i in range(3):
Marks[i] = int(input("Enter Marks: "))
Total = Total + Marks[i]
i = i + 1
print("The Total is", Total) Page 6 | 13
Counting:
Counting is used with repetition with the counter increased by 1 every time the loop is repeated.
Counting items or events is the most frequently used programming technique.
For Example:
• Counting the number of items sold in a single transaction at a supermarket.
• Counting the number of pass marks in a student’s assessment marks
Question:
Using a condition-controlled loop, write a pseudocode to allow the user to input student’s marks of 5
subjects, and count the number of passes IF the pass mark is equal to or greater than 50.
Solution:
Pseudocode: Python:
DECLARE Marks: ARRAY[0:4] OF INTEGERS Score = [0]*5
PassCount = 0
SubjectNumber = 5
for counter in range(SubjectNumber):
Score[counter] = int(input("Enter Marks: "))
if Score[counter] >= 50:
PassCount = PassCount + 1
counter = counter + 1
print(PassCount)

Maximum/Minimum
Finding the largest and smallest values in a list (array) are two standard methods that are frequently
found in algorithm.
Example: Finding the lowest and highest mark awarded to a class of students.
Scenario:
Using pseudocode, write a program to calculate and output the maximum and the minimum marks for
a single subject with five tests taken by a student.
Pseudocode: Python:
DECLARE Marks: ARRAY[0:4] OF INTEGER Marks = [0]*5
Min ← 100 Min = 100
Max ← 0 Max = 0
for counter in range(5):
FOR Counter ← 0 TO 4: Marks[counter] = int(input("Input Marks: "))
OUTPUT “Enter Marks: ” if Marks[counter] < Min:
INPUT Marks Min = Marks[counter]
if Marks[counter] > Max:
IF Marks[Counter] < Min:
Max = Marks[counter]
THEN counter = counter + 1
Minimum ← Marks[Counter] print("The Minimum Marks is: ", Min)
ENDIF print("The Maximum Marks is: ", Max)
IF Marks[Counter] > Max:
Working: With Test Values
THEN
50, 40, 20, 60, 50
Maximum ← Marks[Counter]
Loop 0: Min: 50; Max: 50
ENDIF
Loop 1: Min: 40; Max: 50
NEXT Counter
Loop 2: Min: 20; Max: 50
OUTPUT “The Minimum is: ”, Min
Loop 3: Min: 20; Max: 60
OUTPUT “The Maximum is: “, Max
Loop 4: Min: 20; Max: 60 (Final Output)
Page 7 | 13
Bubble Sort:
▪ We are given with an input array which is supposed to be sorted in ascending order
▪ We start with the first element and i=0 index and check if the element present at i+1 is greater then we swap the
elements at index i and i+1.
▪ If above is not the case, then no swapping will take place.
▪ Now “ i ” gets incremented and the above 2 steps happen again until the array is exhausted.
▪ We will ignore the last index as it is already sorted.
▪ Now the largest element will be at the last index of the array.
▪ Now we will again set i=0 and continue with the same steps that will eventually place second largest at second
last place in the array.
▪ Now the last 2 indexes of the array are sorted.
Pseudocode Example: the table is for demonstration purposed only

Linear Search:
▪ A search is used to check if a value is stored in a list, performed by systematically working through the items in the
list.
▪ It inspects each item in a list in turn to see if the item matches the value searched for.
▪ For example, searching for a name in a class list of student names, where all the names stored are different:

Pseudocode Example:
OUTPUT "Please enter name to find "
INPUT Name
Found ← FALSE
Counter ← 1
REPEAT
IF Name = StudentName[Counter]
THEN
Found ← TRUE
ELSE Counter ← Counter + 1
ENDIF
UNTIL Found OR Counter > ClassSize
IF Found
THEN
OUTPUT Name, " found at position ", Counter, " in the list."
ELSE
OUTPUT Name, " not found."
ENDIF

Page 8 | 13
Pseudocode: Validation & Verification
In order for computer systems to only accept data inputs that are reasonable and accurate, every item of data needs
to be examined before it is accepted by the system. Two different methods, with very similar sounding names, are
used:
▪ Validation: ensures that only data that is reasonable is accepted.
▪ Verification: used to check that the data does not change as it is being entered.

Validation
Validation is the automated checking by a program that data is reasonable before it is accepted into a computer
system.
When data is validated by a computer system, if the data is rejected a message should be output explaining why the
data was rejected and another opportunity given to enter the data.

Types of Validation:
1. Range Check 4. Presence Checks
2. Length Check 5. Format Checks
3. Type Checks 6. Check Digits

Range Check:
A range check checks the value of a number is between an upper value and a lower value. For example, checking the
% of marks are between 0 and 100 inclusive.

Range Check Pseudocode:

OUTPUT "Please enter the student's mark "


REPEAT
INPUT StudentMark
IF StudentMark < 0 OR StudentMark > 100
THEN
OUTPUT "The student's mark should be in the range 0 to
100, please re-enter the mark "
ENDIF
UNTIL StudentMark >= 0 AND StudentMark <= 100

Length Check:

A length check checks either:

▪ that data contains an exact number of characters, for example that a password must be exactly eight characters in
length so that passwords with seven or fewer characters or nine or more characters would be rejected.
▪ or that the data entered is a reasonable number of characters, for example, a family name could be between two
and thirty characters inclusive so that names with one character or thirty-one or more characters would be
rejected.
Length Check Pseudocode:
OUTPUT "Please enter your password of eight characters "
REPEAT
INPUT Password
IF LENGTH(Password) <> 8
THEN
OUTPUT "Your password must be exactly eight
characters, please re-enter "
ENDIF
UNTIL LENGTH(Password) = 8

Page 9 | 13
Type Check:
A type check checks that the data entered is of a given data type, for example, that the number of brothers or sisters
would be an integer (whole number). Example: integer, string/text, data/time etc.

Type Check Pseudocode Example:


OUTPUT "How many brothers do you have? "
REPEAT
INPUT NumberOfBrothers
IF NumberOfBrothers <> DIV(NumberOfBrothers, 1)
THEN
OUTPUT "This must be a whole number, please re-enter"
ENDIF
UNTIL NumberOfBrothers = DIV(NumberOfBrothers, 1)

Presence Check:
▪ A presence check checks to ensure that some data has been entered and the value has not been left blank, for
example, an email address for an online transaction must be completed.
▪ Presence check denotes that data entry cannot be empty or blank; for example, in capturing a student’s data into
the system, Date of Birth is a mandatory entry and cannot be left empty.
▪ OUTPUT "Please enter your email address "

Presence Check Pseudocode:


REPEAT
INPUT EmailAddress
IF EmailAddress = " "
THEN
OUTPUT "*=Required "
ENDIF
UNTIL EmailAddress <> "“

Format Check:
▪ A format check checks that the characters entered conform to a pre-defined pattern.
▪ For Example, motor vehicle plate number (Botswana) B488 BCJ
[The first character should always be a “B”; Followed by 3 Digits; and 3 Alphabetical Letters].
This is a pattern that all private MVs should follow.

Check Digit:
▪ A check digit is the final digit included in a code; it is calculated from all the other digits in the code.
▪ Check digits are used for barcodes, product codes, International Standard Book Numbers (ISBN) and Vehicle
Identification Numbers (VIN).
▪ Check digits are used to identify errors in data entry caused by mis-typing or mis-scanning a barcode. They can
usually detect the following types of error:
o an incorrect digit entered, for example, 5327 entered instead of 5307
o transposition errors where two numbers have changed order for example 5037 instead of 5307
o omitted or extra digits, for example, 537 instead of 5307 or 53107 instead of 5307 » phonetic errors, for
example, 13, thirteen, instead of 30, thirty.

Verification:
Verification is checking that data has been accurately copied from one source to another – for instance, input into a
computer or transferred from one part of a computer system to another.
Verification Methods:
1. Double entry
2. Screen/visual check.
For double entry the data is entered twice, sometimes by different operators. The computer system compares both
entries and if they are different outputs an error message requesting that the data is entered again.

Page 10 | 13
Test Data
▪ A program needs to be tested thoroughly to ensure that it is working as intended.
▪ Algorithms written in pseudocode or flowcharts can be tested by a person working through them using any data is
required and seeing what the result is.
▪ Computer programs can be tested by running them on a computer using any data that is required and seeing what
the result is output.
▪ In order to test a solution thoroughly it may need to be worked through several times with different sets of data.

Data That Can Be Used for Testing:


1. Normal Data
2. Abnormal Data
3. Extreme Data
4. Boundary Data

Normal Data:

DATA THAT A PROGRAM WOULD EXPECT TO RECEIVE, AND THE RESULT THAT ARE EXPECTED FROM THE DATA
For example, consider an algorithm that records the percentage marks, entered in whole numbers, from six end-of-
term examinations for a pupil, and then finds the average mark. A set of NORMAL TEST DATA for this purpose could
be:

Abnormal Data:

DATA THAT A PROGRAM SHOULD REJECT. OUR PROGRAM NEEDS TO REJECT THE ERRONEOUS DATA
For example, consider an algorithm that records the percentage marks, entered in whole numbers, from six end-of-
term examinations for a pupil, and then finds the average mark. A -5 would be an ABNORMAL DATA and the program
needs to reject the erroneous data because no one can get a negative, mark:

Extreme Data:

EXTREME DATA ARE THE LARGEST AND SMALLEST VALUES THAT NORMAL DATA CAN TAKE
For example, consider an algorithm that records the percentage marks, entered in whole numbers, from six end-of-
term examinations for a pupil, and then finds the average mark. EXTREME DATA would be 100 and 0.

Boundary Data:

USED TO ESTABLISH WHERE THE LARGEST AND SMALLEST VALUES OCCUR. AT EACH BOUNDARY TWO VALUES
ARE REQUIRED: ONE VALUE IS ACCEPTED AND ONE VALUE IS REJECTED
For example, for % Marks in the range 0 to 100, the algorithm should be tested with the following BOUNDARY DATA.

Page 11 | 13
Trace Table to Document Dry Runs of Algorithms

▪ A thorough structured approach is required to find out the purpose of an algorithm.


▪ This involves recording and studying the results from each step in the algorithm and requires the use of Test Data.
▪ A Trace Table is used to record the results from each step in an algorithm.
▪ This manual exercise of working through an algorithm step by step is called Dry Run.

Consider the algorithm represented by the following flow chart. Complete the Trace Table using the following
Test Data: 9, 7, 3, 12, 6, 4, 15, 2, 8, 5

Page 12 | 13
Trace Table for the above flow chart:

A B C X OUTPUT
0 0 100
1 9 9 9
2 7 7
3 3 3
4 12 12
5 6
6 4
7 15 15
8 2 2
9 8
10 5
15 2

Page 13 | 13

You might also like