AxoPrep

Computer Science

B1.1 Approaches to computational thinking

Breaking problems down with specification, abstraction, decomposition and pattern recognition, then designing, drawing and tracing algorithms. Study guide, 4 practice questions with markschemes and 4 flashcards. Free.

writing a problem specification · abstraction · decomposition · pattern recognition · algorithm design · flowcharts and tracing algorithms

Practise all 4 questions Study 4 flashcards

Study guide

From problem to specification

Before writing code, pin down what must be built. A problem specification states:

Example: a canteen pre-order app. Inputs: student ID, chosen meals, pick-up time. Output: confirmation and a daily list for the kitchen. Constraint: orders close at 10:00. Success criterion: an order placed after 10:00 is rejected with a message.

The four thinking tools

A good algorithm is unambiguous, finite (it always stops), has defined inputs and outputs, and is correct for every valid input including edge cases.

Representing algorithms

Flowchart symbols: oval = start/stop, parallelogram = input/output, rectangle = process, diamond = decision (two labelled exits), arrows = flow.

flowchart TD
  S([Start]) --> I[/Input mark/]
  I --> D{mark >= 0 and mark <= 100?}
  D -- No --> E[/Output 'invalid'/] --> I
  D -- Yes --> P{mark >= 50?}
  P -- Yes --> O1[/Output 'pass'/]
  P -- No --> O2[/Output 'fail'/]
  O1 --> X([Stop])
  O2 --> X

The same logic in Python:

mark = int(input("Mark: "))
while mark < 0 or mark > 100:
    print("invalid")
    mark = int(input("Mark: "))
print("pass" if mark >= 50 else "fail")

Tracing algorithms

A trace table has one column per variable (plus conditions and output) and one row each time a value changes. It is how you prove what code does without running it.

a = 48
b = 18
while b != 0:
    r = a % b
    a = b
    b = r
print(a)
abrb != 0Output
4818-True
181212True
1266True
600False
6

Purpose: it finds the greatest common divisor (Euclid's method). Being able to state the purpose after tracing is a frequent exam part.

Testing an algorithm

Choose test data in three kinds. For a function accepting ages 13 to 19 inclusive:

KindExample valuesExpected
Normal16accepted
Boundary13, 19 (and just outside: 12, 20)accept, accept, reject, reject
Invalid / erroneous-4, "abc"rejected

Boundary values matter because off-by-one mistakes (< instead of <=) only show up at the edges.

Common mistakes

Exam tips

Cheat sheet

Key definitions

Problem specification

A precise statement of a problem's inputs, required processing, outputs, constraints and success criteria.

Abstraction

Removing details that are irrelevant to the purpose so that only the essential features of a problem are modelled.

Decomposition

Breaking a complex problem into smaller sub-problems that can be solved independently.

Pattern recognition

Identifying similarities between problems or parts of a problem so a common solution can be reused.

Algorithm

A finite, unambiguous sequence of steps that solves a problem.

Flowchart

A diagram that represents an algorithm using standard symbols connected by arrows showing the flow of control.

Trace table

A table recording the values of variables at each step of an algorithm, used to check its behaviour by hand.

Boundary data

Test values at the edges of the valid range, and just outside it, used to catch off-by-one errors.

Practice questions

SLPaper 2Standard

Question 1

(a) [4]

Trace the following Python code and state the output.

n = 6
steps = 0
while n != 1:
    if n % 2 == 0:
        n = n // 2
    else:
        n = 3 * n + 1
    steps = steps + 1
print(steps)
Markscheme
  • 1 Values of n: 6, 3, 10, 5, 16
  • 1 Values of n continue: 8, 4, 2, 1
  • 1 steps increases by one per iteration
  • 1 Output: 8
(b) [1]

Identify the condition that makes the loop stop.

Markscheme
  • 1 When n becomes equal to 1 (the condition n != 1 is false)
SLPaper 2Standard

Question 2

(a) [4]

Copy and complete a trace table for the code below, then state the final value of rev.

num = 4072
rev = 0
while num > 0:
    d = num % 10
    rev = rev * 10 + d
    num = num // 10
Markscheme
  • 1 d takes the values 2, 7, 0, 4
  • 1 rev takes the values 2, 27, 270, 2704
  • 1 num takes the values 407, 40, 4, 0
  • 1 Final value of rev: 2704
(b) [1]

Outline the purpose of the algorithm.

Markscheme
  • 1 It reverses the digits of a positive integer
SLPaper 1Foundation

Question 3

(a) [2]

A school wants an app that lets students reserve study rooms. Outline how decomposition could be applied to this problem.

Markscheme
  • 1 Break the problem into smaller sub-problems / modules
  • 1 Valid examples, e.g. user login, showing free rooms, making a booking, cancelling a booking, sending reminders
(b) [2]

Identify two details that could be removed through abstraction when modelling a room for the booking system.

Markscheme
  • 1 Any one irrelevant detail, e.g. wall colour
  • 1 A second irrelevant detail, e.g. type of furniture / window size

Key facts

Decomposition

Breaking a complex problem into smaller, more manageable sub-problems that can be solved separately.

Abstraction

Removing details that are not needed for the purpose, keeping only the essential features of a problem or object.

Normal, boundary and invalid test data

Normal: typical valid values. Boundary: values at the edges of the valid range. Invalid: values that must be rejected (out of range or wrong type).

What is a trace table for?

Recording the value of each variable step by step as an algorithm runs, to find its output or locate logic errors.