Algorithm Design and Problem Solving Notes 2025
Algorithm Design and Problem Solving Notes 2025
• 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
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.
@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:
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.
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:
• 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
OBJECTIVES
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
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.
Example 1
• Count the number of items the user enters:
Example 1
Number = 1
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
Example 1
@srupfutse 0773244696 4
5
Number = 1
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
Example 1
Number= 1
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
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
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
ELSE
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.
• 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
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.
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.
@srupfutse 0773244696 8
9
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
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
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)
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
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")
@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.
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.
Example 1
• A program allows the user to enter their age as a whole number between 10 and 100 years.
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
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.
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
• 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
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)
@srupfutse 0773244696 14