Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Algorithm Design and Problem Solving Notes 2025

The document outlines the fundamentals of algorithm design and problem-solving, emphasizing the program development life cycle, which includes analysis, design, coding, and testing. It covers techniques such as decomposition, structure diagrams, flowcharts, pseudocode, and standard algorithms like bubble sort and linear search. Additionally, it discusses the importance of validation, verification, and testing with various types of test data.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Algorithm Design and Problem Solving Notes 2025

The document outlines the fundamentals of algorithm design and problem-solving, emphasizing the program development life cycle, which includes analysis, design, coding, and testing. It covers techniques such as decomposition, structure diagrams, flowcharts, pseudocode, and standard algorithms like bubble sort and linear search. Additionally, it discusses the importance of validation, verification, and testing with various types of test data.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1

ALGORITHM DESIGN AND PROBLEM SOLVING


Learners should be able to:

• learn about the use of, and stages in, the program development life cycle
• use decomposition to split a system into sub-systems.
• create structure diagrams, flowcharts and pseudocode algorithms.
• explain how a bubble sort and linear search works.
• describe and produce algorithms that include finding the maximum, minimum and average values.
• understand the need for validation and verification and write programs that use both.
• identify appropriate test data for an algorithm.
• complete a trace table for an algorithm.
• learn how to check a program for errors and amend the program.
• learn how to explain the purpose of an algorithm

Program development life cycle

• a series of structure step / activities that are followed to produce a system.

OBJECTIVES:

1. Understand the program development life cycle, limited to: analysis, design, coding and testing.
There are several varieties of life cycle, including cyclic, spiral, waterfall and rapid development. These all include the
stages analysis, design, coding and testing.

Including identifying each stage and performing these tasks for each stage:
– Analysis: the first stage of the program development life cycle that involves investigating the problem.
• Abstraction:
Is used as a tool.
Is a process of selecting the necessary facts and discard unnecessary.
keeps the key elements required for the solution to the problem and discards any unnecessary details and
information that is not required.
• decomposition of the problem.
The process of breaking down a complex problem into smaller parts, which can then be subdivided into even
smaller parts, that can be solved easily.
• identification of the problem and requirements:
process of identifying the problem and problem requirements.
– Design:
The program specification from the analysis stage is used to show to how the program should be developed. This can be
formally documented using structure charts, flowcharts and pseudocode.

• structure diagrams:
a hierarchical diagram that shows the decomposition of a system.
• Flowcharts:
a diagrammatic representation of an algorithm.
shows each step of a process, in the order that each step is performed.
shows diagrammatically the steps required to complete a task and the order that they are to be performed.
• Pseudocode:
any readable code-like statements that all programmers will understand. This means that it uses keywords, and
constructs such as IF statements, WHILE loops, etc.
is a simple method of showing an algorithm.
It describes what the algorithm does by using English key words that are very similar to those used in a high-
level programming language.

RULES TO WRITE PSEUDOCODE

» a non-proportional font is used throughout.

@srupfutse 0773244696 1
2

» All keywords (words used to describe a specific action e.g. INPUT) are written in capital letters.

» All names given to data items and subroutines start with a capital letter.

» Where conditional and loop statements are used, repeated or selected statements are indented by two spaces.

– Coding:
• writing program code and iterative testing.
• the writing of a program using one or more programming languages.
– Testing:

• testing program code with the use of test data.


• repeated use of a system to try all different possibilities to make sure the system is fully
working and cannot be broken.

There are four types of test data:


Normal - data that the program should accept.
Abnormal - data that the program should not accept.
Extreme - data that is at the edge of what is allowed.
Boundary - data that is on the edge of being accepted and being rejected.

OBJECTIVES:

2. (a) Understand that every computer system is made up of sub-systems, which are made up of further sub-systems.
• A computer system is made up of software, data, hardware, communications and people; each computer system
can be divided up into a set of sub-systems.
• Each sub-system can be further divided into sub-systems and so on until each sub-system just performs a single
action.

The computer system and its sub-systems


• This division can be shown using top-down design to produce structure diagrams that demonstrate the modular
construction of the system.
• Each sub-system can be developed by a programmer as a sub-routine.
• How each sub-routine works can be shown by using flowcharts or pseudocode.

Top-down design
• is the decomposition of a computer system into a set of subsystems, then breaking each sub-system down into a
set of smaller sub-systems, until each sub-system just performs a single action.
• This is an effective way of designing a computer system to provide a solution to a problem, since each part of
the problem is broken down into smaller more manageable problems.
• The process of breaking down into smaller sub-systems is called stepwise refinement.

OBJECTIVES:

(b) Understand how a problem can be decomposed into its component parts
Any problem that uses a computer system for its solution needs to be decomposed into its component parts. The
component parts of any computer system are:

• » Inputs – the data used by the system that needs to be entered while the system is active
• » Processes – the tasks that need to be performed using the input data and any other previously stored data.
• » Outputs – information that needs to be displayed or printed for the users of the system.
• » Storage – data that needs to be stored in files on an appropriate medium for use in the future.

OBJECTIVES:

c) Use different methods to design and construct a solution to a problem.

• Including:
– structure diagrams
– flowcharts

@srupfutse 0773244696 2
3

– pseudocode

Structure diagrams
• Are used to show top-down design in a diagrammatic form.
• are hierarchical, showing how a computer system solution can be divided into sub-systems with each level giving a
more detailed breakdown. If necessary, each sub-system can be further divided.
Flowcharts
• shows diagrammatically the steps required to complete a task and the order that they are to be performed.
• These steps, together with the order, are called an algorithm.
• Flowcharts are an effective way to communicate how the algorithm that makes up a system or sub-system works.
Pseudocode
• is a simple method of showing an algorithm.
• It describes what the algorithm does by using English key words that are very similar to those used in a high-level
programming language.
• Data items to be processed by the algorithm are given meaningful names in the same way that variables and
constants are in a high-level programming language.
• However, pseudocode is not bound by the strict syntax rules of a programming language.

OBJECTIVES

3. Explain the purpose of a given algorithm


• Including:
– stating the purpose of an algorithm.
– describing the processes involved in an algorithm
• An algorithm sets out the steps to complete a given task.
• This is usually shown as a flowchart or pseudocode, so that the purpose of the task and the processes needed to
complete it are clear to those who study it.

OBJECTIVES

4. Understand standard methods of solution


• Limited to:
– linear search
– bubble sort
– totalling
– counting
– finding maximum, minimum and average values

a) TOTALLING.
Statements in a program that add together a set of data to produce the total.
keeping a total that values are added to.
Example

Total <- 0 //Initialise the total variable to 0


FOR X <- 0 TO 9
INPUT ("Enter a value") // requesting user input a value
INPUT Value
Total <- Total + Value//Add the input to the total
NEXT X
OUTPUT Total// output what is stored in total.

b) COUNTING
• statements in a program that record how many of something there are.
• is working out how many of something there are.
• For example, counting how many fingers you have or counting how many items you bought when you went
shopping.

• To do this in a program you need to:


@srupfutse 0773244696 3
4

• Initialise a variable to start the count, e.g. Countitems assigned to 0.


• Add 1 to the count variable each time, e.g. Count = Count + 1.

Example 1
• Count the number of items the user enters:

Count= 0 //initialise the count variable


Number= 0
WHILE Number <> -1 DO// loop until the user enters -2
Number= INPUT ("Enter a number. Enter -1 to stop")
Count= Count + 1 //add 1 to count each time a number is input
ENDWHILE

c) FINDING THE MINIMUM


• The minimum value is the smallest value within a set.
For example, the marks for a class are entered and the lowest mark is the minimum.
• To do this in a program you need to:
• initialise a minimum variable to be a large value, beyond those that will be entered.
e.g. Minimum = 9999.
• Compare each value to the minimum variable e.g. IF Number < Minimum
If it is, it replaces the minimum value, e.g. minimum = number.
• This means the new value is the minimum, so the next time a value is compared, it is checking it with the new
minimum value.

Example 1

Find the smallest number input by a user:

Minimum = 9999 //initialise minimum to large value

Number = 1

WHILE Number >= 1 DO //loop until the user enters 0

Number = INPUT ("Enter a number or O to stop")

IF Number < Minimum THEN // check if the number entered is smaller than the current minimum

Minimum = Number //if true then make minimum because the number

ENDIF

ENDWHILE

d) FINDING THE MAXIMUM


• The maximum value is the largest value within a set.
For example, the marks for a class are entered and the highest mark is the maximum.
• To do this in a program you need to:
Initialise a maximum variable to be a small value, beyond those that will be entered,
e.g. Maximum = -9999.
• Compare each value to the maximum variable,
e.g. IF Number > Maximum.
• If it is, it replaces the maximum value,
e.g. Maximum = Number.
• This means the new value is the maximum, so the next time a value is compared, it is checking it with the new
maximum value.

Example 1

Find the largest number input by a user:

Maximum = -9999 //initialise maximum to small value

@srupfutse 0773244696 4
5

Number = 1

WHILE Number >= 1 DO //loop until the user enters 0

Number = INPUT ( "Enter a number or 0 to stop")

IF Number > Maximum THEN // check if the number entered is larger than the current maximum

Maximum = Number //if true then make maximum because the number

ENDIF

ENDWHILE

e) FINDING THE AVERAGE


• The average here is referring to the mean.
• This is the total of all the values added together, then divided by how many numbers there are.
• For example, if the data is 1, 3, 5, 8, 4, 2, 6, 9 ,
• the average = ( 1 + 3 + 5 + 8 + 4 + 4 + 6 + 9) I 8 = 40 / 8 = 5
• To do this in a program you need to use the count and total from earlier in this section.

The average is then calculated with the formulae Total / Count.

Example 1

Find the average of the numbers input by a user:

Count= 0 //initialise the count to 0

Total=0 //initialise the total to 0

Number= 1

WHILE Number >= 1 DO //loop until the user enters 0

Number = INPUT (“Enter a number or O to stop")

Total= Total + Number //add the number entered to the total

Count= Count + 1 //add 1 to the count

ENDWHILE

f) LINEAR SEARCH
• In a program you might need to look for a specific value in a set of data.
• A linear search will check each item one at a time, starting with the first item and continuing until it either finds
the item, or it checks the last value.
• Search this set of data for the number 4.
5 3 9 4 2 1 8

• Compare the first number (5) with the search value (4). They are not the same.
• Compare the second number (3) with 4. They are not the same.
• Compare the third number (9) with 4. They are not the same.
• Compare the fourth number(4) with 4. They are the same. It has been found.

Example A

Searching an array.

The array dataArray stores a set of data under one identifier. Each element in the array has an index.

INDEX 0 1 2 3
DATA 1 5 6 8

Search the array for the number 6


@srupfutse 0773244696 5
6

Is DataArray [ 0 J = 6? No, DataArray [ 0 J = 1

Is DataArray [ 1 J = 6? No, DataArray[l) = 5

Is Da taArray [ 2 J = 6? Yes, DataArray[2) = 6

6 was found in index 2.

This can now be written as an algorithm:

FOR Arrayindex = 0 to LENGTH(Array) -1 //loop through each element

IF DataArray[Arrayindex) = SearchValue THEN //check if the current index is the data searched for

OUTPUT "Found at " & Arrayindex//if it is, output the index where it is found

ELSE

Arrayindex 􀂏 Arrayindex + 1 //if it not found, increment arrayindex to check the next value

ENDIF

NEXT Arrayindex

This algorithm is inefficient because if it finds the search value then it still continues

searching. This is useful if you want to know whether the search value appears more

than once.

You can make the algorithm more efficient by stopping as soon as the value is found,

for example:

Found = FALSE

Arrayindex = 0

//run while the value is not found, and you have not checked all elements

WHILE Found = FALSE AND Arrayindex < LENGTH(Array) DO

IF DataArray[Arrayindex) = SearchValue THEN //check if the current index is the data searched for

OUTPUT "Found at " & Arrayindex //if it is output where it was found

Found=TRUE//set found to be true to stop the while loop running again

ELSE

//if it is not found, increment Arrayindex to the next value

Arrayindex = Arrayindex + 1

ENDIF

ENDWHILE

g) BUBBLE SORT:
• a sorting algorithm that moves through the list repeatedly swapping values in pairs.
A bubble sort takes the first 2 values; value 1 and 2, and compares them.
If they are the wrong way around it swaps them.
It then puts these back and takes values 2 and 3 and compares them.
If they are the wrong way around it swaps them. T
This repeats until it has worked all the way through the list once.

@srupfutse 0773244696 6
7

It then starts again with the first two values. It is called a bubble sort because it acts like a bubble moving across
all the elements to the end, then starting from the beginning.

• There are two ways to tell the algorithm when to stop.

• 1 The algorithm has been through the entire list, the number of elements in the list -1. So if there are 10
elements, the algorithm runs 9 times. If there are 100 elements, the algorithm runs 99 times.
• 2 Either the algorithm has run through the list XElements - 1 times, or it has run through the list, checking all of
the elements, and it has not made any changes.

Example 1
Stopping only when it has run array length -1 times.
//loop array length - 1 number of times
FOR NumberPasses = 0 to LENGTH(DataArray) - 1
//loop through each element in the array
FOR Index = 0 to LENGTH(DataArray) - 1
//check if the data is in the correct order
IF DataArray[Index] > DataArray[Index + 1] THEN
//if not swap the items
Temp = DataArray[Index]
DataArray[Index] = DataArray[Index + 1]
DataArray[Index + 1] = Temp
ENDIF
NEXT Index
NEXT NumberPasses

Example 2
Stopping when there are no changes or when it has run array length - 1 times.
NumberPasses = 0
//continue until one pass has no changes (changes = false) or it has looped array length - 1 times
WHILE Changes = FALSE or NumberPasses <= LENGTH(DataArray) - 1 DO
//reset changes each time a new pass starts
Changes = FALSE
//loop through each element in the array check if the data is in the correct order
FOR Index = O to LENGTH(DataArray) - 1
IF DataArray[Index] > DataArray[Index + 1] THEN
//if not swap the items
ENDIF
Temp = DataArray[Index]
DataArray[Index] = DataArray[Index + 1]
DataArray[Index + 1] = Temp
Changes = TRUE
NEXT Index
ENDWHILE

OBJECTIVES

5. (a) Understand the need for validation checks to be made on input data and the different types of validation check
• Including:
– range check
– length check
– type check
– presence check
– format check
– check digit
– the purpose of each validation check and writing algorithms to implement each validation check

@srupfutse 0773244696 7
8

Validation on input

• is the checking of data that is input to make sure it is reasonable, and/or


within set bounds.

For example,
making sure that a number is entered for an age or
limiting the range of numbers that can be entered.

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.

Validation
• ensures that only data that is reasonable is accepted.
• 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 checks including


– range check
– length check
– type check
– presence check
– format check
– check digit

i. Range Check
• checks that the value of a number is between an upper value and a lower value.
• assesses whether data is within one or two bounds. For example, an age must be between 0 and
100.
• A date must be before today's date.
• Range checks can be programmed using selection to produce an error, or within a loop that keeps
asking you enter a value until it is valid.

For example,
Using selection:
This algorithm will check whether a number is higher than 1 and less than 10.

Example 1
Number <-- INPUT("Enter a number between 1 and 10")
IF Number < 1 OR Number > 10 THEN
OUTPUT ("Invalid")
ELSE
OUTPUT ("Valid")
ENDIF

Example 2
Using a loop:
This algorithm will check if a number is higher than 1 and less than 10. It will continually ask for
a value to be input until the number is valid.

Number <-- INPUT("Enter a number between 1 and 10")


WHILE Number < 1 OR Number > 10 DO
OUTPUT("Invalid please try again")
Number <-- INPUT ( )
ENDWHILE

@srupfutse 0773244696 8
9

//checking that percentage marks are between 0 and 100 inclusive:


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

ii. 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, for instance:
• will check the number of characters that are present. This could in a string, for example, the length
of "hello world" is 11, the space is also a character.
• It could be in a variable, for example, in this example, 8 will be output:

Example 1
TheData <-- "123 ABC!"
OUTPUT(LENGTH(TheData))
The length of a piece of data can be found using a variety of commands depending on the
language you are using.

The following are all valid and there are many more:
• theData.length()
• LENGTH(theData)
• LEN(theData)
• theData.len

A length check can be programmed using selection or iteration.

Example 1
Using selection:
This algorithm will check if the data input has 10 or less characters to be invalid, otherwise (10 or
more) it is valid.

Data = INPUT ()
IF LENGTH(Data) < 10 THEN
OUTPUT("Invalid")
ELSE
OUTPUT ("Valid")
ENDIF

Example 2
Using iteration:
This algorithm will also check the length but will continually ask for this to be input until it is 10
or more characters long.

Data = Input ()
WHILE LENGTH(Data) < 10 DO
OUTPUT("Invalid, please try again")
Data = INPUT ()
ENDWHILE

@srupfutse 0773244696 9
10

Example 3
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
// Password has a data type of string and LENGTH is the pseudocode operation that returns a
whole number showing the number of characters in the string. or that the data entered is a
reasonable number of characters, for example, a family name could be between 2 and 30
characters inclusive so that names with one character or thirty-one or more characters would be
rejected.

Example 4
OUTPUT "Please enter your family name "
REPEAT
INPUT FamilyName
IF LENGTH(FamilyName) > 30 OR LENGTH(FamilyName) < 2
THEN
OUTPUT "Too short or too long, please re-enter "
ENDIF
UNTIL LENGTH(FamilyName) <= 30 AND LENGTH(FamilyName) >= 2

iii. 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 1
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)

• There are two ways of writing a type check:


1 Use a function such as . GetDataType () to return the data type, that you can then compare to the
one you want.
2 Use a function such as . Is Integer () to return True if it is an integer and False otherwise.

Example 2
Using selection:
This algorithm will check if the data entered is an integer value to be valid.

INPUT Data
//check if the data entered is an Integer
IF Data.GetDataType() <> Integer THEN //if it is not an integer
OUTPUT ("Invalid")
ENDIF
Example 3
Using iteration:
This algorithm will continually ask for the data to be input until it is a string value.
INPUT Data
//loop while the data entered is not a string
WHILE Data.IsString = FALSE DO
OUTPUT("Invalid please try again")
INPUT Data
ENDWHILE
@srupfutse 0773244696 10
11

iv. 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.
Example 1
OUTPUT "Please enter your email address "
REPEAT
INPUT EmailAddress
IF EmailAddress = ""
THEN
OUTPUT "*=Required "
ENDIF
UNTIL EmailAddress <> ""

v. Format Check
• checks that the characters entered conform to a pre-defined pattern. for example, cub number
must be in the form CUB9999)
Example 1
Using a loop:
This algorithm checks whether an input is in the format two numbers, /, two numbers, /, four numbers.
INPUT Date
//loop while the data is not in the correct format
WHILE (SUBSTRING(Date, 0, 2) .IsNumeric = FALSE OR //2 numbers
SUBSTRING(Date, 2, 1) <> 11 / 11 OR II 1st slash
SUBSTRING(Date, 3' 2) .IsNumeric = FALSE OR //2 numbers
SUBSTRING(Date, 5, 1) <> 11 / 11 OR / /2nd slash
SUBSTRING(Date, 6, 4) .IsNumeric = FALSE) DO II 4 numbers, year
OUTPUT("Invalid")
INPUT Date
ENDWHILE
OUTPUT ("Valid")

vi. 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:
» An incorrect digit entered, for example, 5327 entered instead of 5307
» Transposition errors where two numbers have changed order for example 5037 instead of 5307
» 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.

@srupfutse 0773244696 11
12

OBJECTIVES
(b) Understand the need for verification checks to be made on input data and the different types of verification
check
• Including:
– visual check
– double entry check
– the purpose of each verification check and writing algorithms to implement each verification check

Verification
• is used to check that the data does not change as it is being entered.
• 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 for input data include:


» Double entry
» Screen/visual check.

A. 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.
two different people enter the same data which are then compared.

B. Screen/Visual Check
is a manual check completed by the user who is entering the data.
comparing the data entered with the original side-by-side.
When the data entry is complete the data is displayed on the screen and the user is asked to confirm that it
is correct before continuing.
The user either checks the data on the screen against a paper document that is being used as an input form
or, confirms whether it is correct from their own knowledge.

6. Suggest and apply suitable test data.


• Limited to:
– Normal
– Abnormal
– Extreme
– Boundary
• Extreme data is the largest/smallest acceptable value
• Boundary data is the largest/smallest acceptable value and the corresponding smallest/largest rejected value

Example 1
• A program allows the user to enter their age as a whole number between 10 and 100 years.

Test data type Example data


Normal Any number between 10 and 100 inclusive, e.g. 10, 20, 50, 100, etc.
Abnormal Any data that is not a number between 10 and 100, e.g. 9, -1, "age", "30", 12.2.
Extreme The largest number accepted - 100. The smallest number accepted - 10.
Boundary Either side of the largest - 100 and 101. Either side of the smallest - 9 and 10.

OBJECTIVES
7. Complete a trace table to document a dry-run of an algorithm.
• Including, at each step in an algorithm:
– variables
– outputs
– user prompts

@srupfutse 0773244696 12
13

Trace Table

• structure to complete when walking through an algorithm manually, where the values that change are written in
each row.
• A trace table is a structure to help you follow an algorithm, for example, to find an error, or to work out what
the algorithm does.
• Each statement in an algorithm is run manually, and the values that are written to variables are written in the
table in the order the changes are made.
• Each column in the table is designated to one variable.
• There can also be a column for any outputs to be written in.
• User prompts are also entered in the trace table, these can be entered in a separate column or as an output
(because they will be output).

For example,

• x = input ("Enter a number"), the text "Enter a number" is a user prompt and will need to be added to the trace
table.

OBJECTIVES

8. Identify errors in given algorithms and suggest ways of correcting these errors

FINDING THE PURPOSE WITH A TRACE TABLE

Describe the processes in this algorithm and state its purpose.

INPUT Valuel
INPUT Value2
INPUT Value3
IF Valuel > Value2 AND Valuel > Value3 THEN
OUTPUT(Valuel)
ELSEIF Value2 > Valuel AND Value2 > Value3 THEN
OUTPUT(Value2)
ELSE
OUTPUT(Value3)
ENDIF
OUTPUT(Valuel + Value2 + Value3)

For Example,

1 First test the algorithm with a set of data that you come up with (if they are not provided for you). test this algorithm
using 1, 2 then 3.
2 Create a trace table by following the algorithm with the data input.
3 Repeat the process with a different set of data, for example, this time using 10, 5, 1.

4 Complete the trace table.

OBJECTIVES

9. Write and amend algorithms for given problems or scenarios, using: pseudocode, program code and flowcharts
• Precision is required when writing algorithms,
e.g. x > y is acceptable but x is greater than y is not acceptable

Finding errors in a program and correcting the errors.

• To find an error in an algorithm you need to know the purpose of the algorithm.
• You can then test the algorithm with different data, completing trace tables if needed, to find out what the
algorithm actually does.
• Once you know the difference between what it does and should do, you can start to identify the errors and the
changes you need to make.
@srupfutse 0773244696 13
14

USING A TRACE TABLE TO FIND ERRORS IN A PROGRAM


• Using a trace table will allow you to follow each step in the algorithm and by comparing this to the intended
outcome you can find where it goes wrong, and then work out how to correct it.

For example,
this program should take 5 numbers as input and output the total of all the numbers.

01 Total <- 1
02 Quantity <- 1
03 WHILE Quantity < 5 DO
04 INPUT Number
05 Total <- Number
06 Quantity <- Quantity + 1
07 ENDWHILE
08 OUTPUT (Total)

• First dry run the algorithm.


• If not provided, you will need to identify some test data to use.
• This algorithm should take 5 numbers as input, so you could use 1, 2, 3, 4, 5.
• Then work out what the result of the algorithm should be.
• It outputs the total, so it should output: 1 +2+ 3 +4+ 5= 15

@srupfutse 0773244696 14

You might also like