Skip to content

Algorithm design and problem-solving

IGCSE Computer Science · Topic 7

Train
Syllabus
Candidates should be able to: Notes and guidance
1 Understand the program development life cycle, limited to: analysis, design, coding and testing • Including identifying each stage and performing these tasks for each stage: – analysis: abstraction, decomposition of the problem, identification of the problem and requirements – design: decomposition, structure diagrams, flowcharts, pseudocode – coding: writing program code and iterative testing – testing: testing program code with the use of test data
2 (a) Understand that every computer system is made up of sub-systems, which are made up of further sub-systems (b) Understand how a problem can be decomposed into its component parts • Including: – inputs – processes – outputs – storage
(c) Use different methods to design and construct a solution to a problem • Including: – structure diagrams – flowcharts – pseudocode
3 Explain the purpose of a given algorithm • Including: – stating the purpose of an algorithm – describing the processes involved in an algorithm
4 Understand standard methods of solution • Limited to: – linear search – bubble sort – totalling – counting – finding maximum, minimum and average values
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
(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
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
7 Complete a trace table to document a dry-run of an algorithm • Including, at each step in an algorithm: – variables – outputs – user prompts
8 Identify errors in given algorithms and suggest ways of correcting these errors
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 • See section 4 for flowchart symbols • See section 4 for pseudocode

Source: Cambridge International syllabus

7.1

The program development life cycle

The program development life cycle 程序开发生命周期 is the set of stages used to make a program. There are four stages.

A programmer typing code at a computer Software is written by programmers, who follow the development life cycle

Stage What you do
analysis 分析 study the problem and work out what is needed
design 设计 plan how the program will work
coding 编码 write the program code and test it as you go
testing 测试 run the finished program with test data to find errors

Four stages in a row — analysis, design, coding, testing — with an arrow feeding back from testing to design The four stages of program development; testing feeds back to fix and refine the design

A program flowchart with process boxes and decision diamonds A program flowchart sets out the steps and decisions of a program during the design stage

Analysis

In analysis you understand the problem. Two key skills help:

  • abstraction 抽象 — keep only the important details and ignore the rest;
  • decomposition 分解 — break a big problem into smaller, easier parts.

Design

In design you plan the solution, often using decomposition. You can show the parts as sub-systems 子系统 in a structure diagram 结构图 (a chart that splits a system into smaller boxes).

Coding and testing

In coding you write the program code. You use iterative testing 迭代测试 — test small parts again and again as you build them. In testing you run the whole program with test data 测试数据 to check it works.

Vocabulary Train
English Chinese Pinyin
program development life cycle 程序开发生命周期 chéng xù kāi fā shēng mìng zhōu qī
analysis 分析 fēn xī
design 设计 shè jì
coding 编码 biān mǎ
testing 测试 cè shì
abstraction 抽象 chōu xiàng
decomposition 分解 fēn jiě
sub-systems 子系统 zi xì tǒng
structure diagram 结构图 jié gòu tú
iterative testing 迭代测试 dié dài cè shì
test data 测试数据 cè shì shù jù
7.2

Design tools

You can plan a solution in three main ways.

  • a structure diagram — shows the parts of a system and how they fit together;
  • a flowchart 流程图 — a diagram using boxes and arrows to show the steps in order;
  • pseudocode 伪代码 — steps written in simple, code-like English (not a real language).

A flowchart for adding the numbers 1 to n, with start/end, input/output, process and decision symbols, plus a key naming each shape A flowchart for the sum algorithm, using the standard symbols (start/end, input/output, process, decision)

Vocabulary Train
English Chinese Pinyin
flowchart 流程图 liú chéng tú
pseudocode 伪代码 wěi dài mǎ
7.3

Algorithms

Bubble sort, pass by pass

An algorithm 算法 is a set of steps, in the right order, that solves a problem. Every algorithm can be split into three parts:

  • input 输入 — the data that goes in;
  • processing 处理 — the work done on the data;
  • output 输出 — the result that comes out.

This is called decomposition into inputs, processes and outputs. For example, for "find the average of three marks": the inputs are the three marks; the processing is adding them and dividing by 3; the output is the average.

Three boxes — INPUT (the 3 marks), PROCESS (add them, divide by 3), OUTPUT (the average) — joined by arrows Every algorithm decomposes into input, processing and output — here, finding the average of three marks

Vocabulary Train
English Chinese Pinyin
algorithm 算法 suàn fǎ
input 输入 shū rù
processing 处理 chǔ lǐ
output 输出 shū chū
7.4

Validation and verification

When data is entered, you check it to reduce mistakes.

Validation 验证 checks that the data is sensible and follows the rules. It cannot check that the data is true, only that it is allowed.

Validation check What it checks
range check 范围检查 the value is between a lowest and highest allowed value
length check 长度检查 the number of characters is allowed (e.g. a password ≥ 8)
type check 类型检查 the data is the right type (e.g. a number, not letters)
presence check 存在性检查 something has actually been entered (not left blank)
format check 格式检查 the data is in the right pattern (e.g. a date as dd/mm/yyyy)
check digit 校验码 an extra digit confirms a number was entered correctly

Verification 核实 checks that data was copied or entered correctly (no mistakes while typing it in). Two methods:

  • visual check 目视检查 — a person compares the typed data with the original;
  • double entry 双重输入 — the data is entered twice and the two copies are compared.
Vocabulary Train
English Chinese Pinyin
validation 验证 yàn zhèng
range check 范围检查 fàn wéi jiǎn chá
length check 长度检查 cháng dù jiǎn chá
type check 类型检查 lèi xíng jiǎn chá
presence check 存在性检查 cún zài xìng jiǎn chá
format check 格式检查 gé shì jiǎn chá
check digit 校验码 jiào yàn mǎ
verification 核实 hé shí
visual check 目视检查 mù shì jiǎn chá
double entry 双重输入 shuāng chóng shū rù
7.5

Trace tables

A trace table 追踪表 records the value of each variable as an algorithm runs, step by step. It helps you:

A trace table with columns count, total, output A trace table records each variable's value as the program runs

  • check that an algorithm works correctly;
  • work out what an algorithm does by following it with given data.

Example: trace this algorithm with the input 5.

INPUT n
total ← 0
FOR i ← 1 TO n
    total ← total + i
NEXT i
OUTPUT total
i total OUTPUT
1 1
2 3
3 6
4 10
5 15 15

The trace shows the algorithm adds up 1 to n. With input 5 the output is 15.

Worked example. Trace this algorithm and give the output.

x ← 20
count ← 0
WHILE x > 1
    x ← x DIV 2
    count ← count + 1
ENDWHILE
OUTPUT count

DIV gives only the whole-number part of a division. Take one row per pass: x becomes 10 (count 1), then 5 (count 2), then 2 (count 3), then 1 (count 4). Now x > 1 is false, so the loop stops and the output is 4. Two habits protect these marks: test the condition before each pass rather than after, and write a new row for every pass - trying to hold the values in your head is what makes traces go wrong.

Explore

A trace table

Step through the loop and fill in the trace table, one row per pass.

Vocabulary Train
English Chinese Pinyin
trace table 追踪表 zhuī zōng biǎo
7.6

Test data

Test data is data you use to test a program. There are four types you must know.

Type Meaning Example (age 0–120 allowed)
normal 正常数据 sensible data that should be accepted 25
abnormal 异常数据 wrong data that should be rejected -4 or "cat"
extreme 极端数据 the largest and smallest values still allowed 0 and 120
boundary 边界数据 the values on each side of a limit (one allowed, one not) 120 and 121
Vocabulary Train
English Chinese Pinyin
normal 正常数据 zhèng cháng shù jù
abnormal 异常数据 yì cháng shù jù
extreme 极端数据 jí duān shù jù
boundary 边界数据 biān jiè shù jù
7.7

Standard methods of solution

You must know these common algorithms.

Linear search

A linear search 线性查找 checks each item in a list, one by one, until it finds the value it wants or reaches the end.

found ← FALSE
FOR i ← 0 TO 9
    IF list[i] = searchValue THEN
        found ← TRUE
    ENDIF
NEXT i
OUTPUT found

A list of eight numbers being scanned left to right, searching for 5; the first four do not match and the fifth is found Linear search checks each item in turn from the start until it finds the value

Bubble sort

A bubble sort 冒泡排序 puts a list in order. It compares each pair of side-by-side items and swaps them if they are in the wrong order. It repeats this until no more swaps are needed.

FOR i ← 0 TO 8
    IF list[i] > list[i + 1] THEN
        temp ← list[i]
        list[i] ← list[i + 1]
        list[i + 1] ← temp
    ENDIF
NEXT i

A list where the first pair 5 and 2 is out of order, shown swapping to 2 and 5, with a note to repeat for each pair Bubble sort compares each side-by-side pair and swaps them if they are out of order, repeating until sorted

Totalling and counting

  • totalling 求和 — keep adding values to a running total (total ← total + value).
  • counting 计数 — add 1 to a counter each time something happens (count ← count + 1).

Maximum, minimum and average

  • to find the maximum 最大值: keep the largest value seen so far.
  • to find the minimum 最小值: keep the smallest value seen so far.
  • to find the average 平均值: divide the total by how many values there are.
total ← 0
FOR i ← 0 TO 9
    total ← total + list[i]
NEXT i
average ← total / 10
OUTPUT average
Vocabulary Train
English Chinese Pinyin
linear search 线性查找 xiàn xìng chá zhǎo
bubble sort 冒泡排序 mào pào pái xù
totalling 求和 qiú hé
counting 计数 jì shù
maximum 最大值 zuì dà zhí
minimum 最小值 zuì xiǎo zhí
average 平均值 píng jūn zhí
7.8

Exam tips

  • Learn the four life-cycle stages: analysis → design → coding → testing. Abstraction keeps only the important details; decomposition breaks a problem into smaller parts.
  • Validation checks data is sensible (range, length, type, presence, format checks); verification checks it was copied correctly (a visual check or double entry).
  • Learn the four test-data types: normal (accepted), abnormal (rejected), extreme (the largest/smallest still allowed), boundary (the values either side of a limit).
  • To work out what an algorithm does, fill in a trace table — write down every variable's value at each step.
  • Know the standard algorithms: linear search (check each item in turn) and bubble sort (swap side-by-side pairs until no swaps are needed).

Log in or create account

IGCSE & A-Level