G10 Chapter 7 - Algorithm Design and Problem Solving
G10 Chapter 7 - Algorithm Design and Problem Solving
Problem Solving
Chapter 7
Analysis
Before any problem can be solved, it needs to be clearly defined and set out so anyone
working on the solution understands what is needed. This is called the ‘requirements
specification’ for the program. The analysis stage uses abstraction and decomposition
tools to identify exactly what is required from the program.
Abstraction keeps the key elements required for the solution to the
problem and discards any unnecessary details and information that is
not required.
For example, a map only shows what is required for travelling from one
place to another. Different methods of transport will require different
types of map.
Any daily task can be divided into several parts. For example, getting
dressed:
» Select items to wear
» Remove any clothes being worn
» Put selected items on in order.
Abstraction
● Abstraction is the concept of simplifying complex systems by
hiding the intricate details and exposing only the essential
features.
● It allows developers to focus on high-level functionalities without
needing to understand the underlying implementation.
● By using abstraction, code becomes easier to manage, reuse, and
maintain, facilitating clearer communication and better design.
Abstraction
Abstraction
Design
The program specification from the analysis stage is used to show to how the program
should be developed. When the design stage is complete, the programmer should
know what is to be done, i.e. all the tasks that need to be completed, how each task is
to be performed and how the tasks work together.
This can be formally documented using structure charts, flowcharts and pseudocode
Coding and iterative testing
The program or set of programs is developed.
Each module of the program is written using a suitable programming language and
then tested to see if it works.
Iterative testing means that modular tests are conducted, code amended, and tests
repeated until the module performs as required.
"Iterative" refers to a process that is repeated multiple times, often with the aim of
refining or improving the outcome with each cycle.
Testing
The completed program or set of programs is run many times with different sets of
test data.
This ensures that all the tasks completed work together as specified in the program
design.
Computer systems, sub-systems
and decomposition
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.
Computer systems can be very large, very small or any size in between; most people
interact with many different computer systems during their daily life without realising it.
For example, when you wake up in the morning, you might use an app on your
smartphone for your alarm, then you might check the weather forecast on your
computer before driving to work. The alarm program is a very small computer system
but when you check the weather forecast, you obtain the information you need from
one of the largest computer systems in the world.
The computer system and its
sub-systems
Top-down design is the decomposition of a computer system into a set of sub-
systems, then breaking each sub-system down into a set of smaller sub-systems,
until each sub-system just performs a single action.
This structured approach works for the development of both large and small computer
systems. When larger computer systems are being developed this means that several
programmers can work independently to develop and test different sub-systems for the
same system at the same time. This reduces the development and testing time.
Decomposing a problem
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.
Decomposing a problem: an
alarm app
For example, the alarm app can be decomposed into:
» inputs – time to set the alarm, remove a previously set alarm time, switch an alarm off,
press snooze button
» processes – continuously check if the current time matches an alarm time that has
been set, storage and removal of alarm times, management of snooze
» outputs – continuous sound/tune (at alarm time or after snooze time expired)
» structure diagrams
» flowcharts
» pseudocode.
Structure diagram
Structure diagrams can be used to show top-down design in a diagrammatic form.
Structure diagrams are hierarchical, showing how a computer system solution can be
divided into sub-systems with each level giving a more detailed breakdown.
Process flowchart symbols are used to show actions, for example, when values are
assigned to variables.
If a process has been defined elsewhere then the name of that process is shown.
Flowcharts symbol
Input and Output
The same flowchart symbol is used to show the input of data and output of
information.
Flowcharts symbol
Decision
Decision flowchart symbols are used to decide which action is to be taken next; these
can be used for selection and repetition/iteration. There are always two outputs from a
decision flowchart symbol.
Flowcharts symbol
Flow lines
Flowchart flow lines use arrows to show the direction of flow, which is usually, but not
always, top to bottom and left to right.
Flowcharts
symbol
Convert the following steps into
flowchart
Step 1: Start
Step 4: Display A
Step 5: Stop
Step 6: Stop
Step 4: Display S
Step 5: Stop
Step 4: Display A
Step 5: Stop
Step 6: Stop
Step 4: Display S
Step 5: Stop
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.
The variable on the left of the ̈ is assigned the value of the expression on the right. The
expression on the right can be a single value or several values combined with any of the
following mathematical operators.
The pseudocode for an
assignment statement
The pseudocode for conditional
statements
When different actions are performed by an algorithm according to the values of the
variables, conditional statements can be used to decide which action should be taken.
1. A condition that can be true or false such as: IF ... THEN ... ELSE ... ENDIF
The pseudocode for conditional statements
The pseudocode for conditional
statements
When different actions are performed by an algorithm according to the values of the
variables, conditional statements can be used to decide which action should be taken.
2. a choice between several different values, such as: CASE OF ... OTHERWISE ... ENDCASE
The pseudocode for conditional statements
The pseudocode for conditional statements
The pseudocode for iteration
When some actions performed as part of an algorithm need repeating this is called
iteration. Loop structures are used to perform the iteration.
However, in order to test a solution thoroughly it may need to be worked through several
times with different sets of test data.
Test data - normal data
In order to prove that program or algorithm solutions do what they are supposed to do, a
set of test data should be used that the program would normally be expected to work
with, together with the result(s) that are expected from that data.
The type of test data used to do this is called normal data. Normal data should be used
to work through the solution to find the actual result(s) and see if they are the same as
the expected result(s).
For example, consider an algorithm that records the percentage marks, entered in whole
numbers, from ten end-of-term examinations for a pupil, and then finds the average
mark.
Normal test data: 50, 50, 50, 50, 50, 50 50, 50, 50, 50
Expected result: 50
Test data - abnormal/erroneous data
Solutions also need to be tested to prove that they do not do what they are supposed not
to do. In order to do this, test data should be chosen that would be rejected by the
solution as not suitable, if the solution is working properly.
This type of test data is called abnormal test data. (It is also sometimes called
erroneous test data.)
For example, erroneous/abnormal data for our algorithm to find the average percentage
marks from ten end of term examinations could be:
● 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.
● An algorithm defines the steps necessary to produce the desired outcome, and the
computer follows the instructions to complete the task efficiently and accurately.
The purpose of an algorithm
Standard methods of solution
The ability to repeat existing methods is very important in the design of algorithms; when an
algorithm is turned into a program the same methods may be repeated many thousands of
times.
You need to be able to use and understand these standard methods used in algorithms:
➔ Totalling
➔ Counting
All the standard methods of solution are shown as pseudocode algorithms and will be used to
practise program writing in the next chapter.
Totalling
Counting
Counting
Maximum, minimum and average
Maximum, minimum and average
Maximum, minimum and average
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.
This is called a linear search, which inspects each item in a list in turn to see if the
item matches the value searched for.
Linear search
Linear search
Linear search
Linear Search is a simple searching algorithm that checks each element of a list or
array sequentially to find a target value. The process continues until the target
element is found or the entire list is traversed.
Example 1:
Steps:
Steps:
Result:
This method of sorting is called a bubble sort. Each element is compared with the next
element and swapped if the elements are in the wrong order, starting from the first
element and finishing with next-to-last element. Once it reaches the end of the list,
we can be sure that the last element is now in the correct place.
However, other items in the list may still be out of order. Each element in the list is
compared again apart from the last one because we know the final element is in the
correct place. This continues to repeat until there is only one element left to check or no
swaps are made.
Bubble Sort
Bubble Sort
Bubble Sort is a simple comparison-based sorting algorithm. It works by repeatedly
stepping through the list, comparing adjacent elements, and swapping them if they
are in the wrong order. This process is repeated until the list is sorted.
The algorithm gets its name because smaller elements "bubble" to the top (beginning)
of the list, and larger elements "sink" to the bottom (end) during each pass through the
list.
Bubble Sort
Advantages of Bubble Sort:
(O(n²) means the running time grows quadratically with the input size,
and for large datasets, it can become very slow.)
Steps:
● Pass 1:
○ Compare 5 and 2 → swap them → [2, 5, 9, 1, 5, 6]
○ Compare 5 and 9 → no swap → [2, 5, 9, 1, 5, 6]
○ Compare 9 and 1 → swap them → [2, 5, 1, 9, 5, 6]
○ Compare 9 and 5 → swap them → [2, 5, 1, 5, 9, 6]
○ Compare 9 and 6 → swap them → [2, 5, 1, 5, 6, 9]
● After the first pass, the largest number 9 is at the end.
Bubble Sort
● Pass 2:
○ Compare 2 and 5 → no swap → [2, 5, 1, 5, 6, 9]
○ Compare 5 and 1 → swap them → [2, 1, 5, 5, 6, 9]
○ Compare 5 and 5 → no swap → [2, 1, 5, 5, 6, 9]
○ Compare 5 and 6 → no swap → [2, 1, 5, 5, 6, 9]
● After the second pass, 6 is in its correct position.
● Pass 3:
○ Compare 2 and 1 → swap them → [1, 2, 5, 5, 6, 9]
○ Compare 2 and 5 → no swap → [1, 2, 5, 5, 6, 9]
○ Compare 5 and 5 → no swap → [1, 2, 5, 5, 6, 9]
● The list is now fully sorted: [1, 2, 5, 5, 6, 9].
Result:
Variables are used to store data that may need to be updated or changed, such as user
input, results of calculations, or the state of a program.
Meaningful names: Give variables descriptive names to make the code more readable and
maintainable (e.g., use user_age instead of just x).
Example:
age = 25
name = "Alice"
Constant
A constant is similar to a variable, but its value is set once and cannot be changed during the
execution of the program.
Constants are used to store values that should remain the same throughout the program, such as
mathematical constants (e.g., Pi), configuration values (e.g., max user limit), or other fixed data.
Naming conventions: Constants are typically written in all uppercase letters (e.g., MAX_USERS, PI)
to distinguish them from variables.
Limit constant usage: Constants should only be used for values that should never change (e.g., PI),
not for values that are expected to vary throughout the program.
Trace table
A trace table can be used to record the results from each step in an algorithm; it is used
to record the value of an item (variable) each time that it changes.
The manual exercise of working through an algorithm step by step is called a dry run.
A trace table is set up with a column for each variable and a column for any output. For
example:
Test data is then used to dry run the flowchart and record the results on the trace table.
During a dry run:
● every time the value of a variable is changed, the new value is entered in that
column of the trace table
● every time a value is output, the value is shown in the output column.
6. Construct your algorithm, making sure that it can be easily read and
understood by someone else. Precision is required when writing algorithms, just
as it is when writing program code. This involves setting it out clearly and using
meaningful names for any data stores.
8. If any errors are found, correct them and repeat the process until you
think that your algorithm works perfectly.
Writing and amending algorithms
Have a look at this structure diagram and flowchart for the algorithm to select the
largest, Max, and smallest, Min, numbers from a list of ten numbers. This time the
flowchart is more easily readable than the structure chart:
features of a high‐level programming language that a
programmer could use to make sure that their program will be
easier to understand by another programmer.