Year 11 · Computer Science · Spring 1

Paper 2 practice: algorithms, programming, logic, databases · Lesson 2 of 5

Writing pseudocode to a specification

Exam practice7.4 Standard methods of solution7.5 Validation and verification8.1 Programming concepts

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 2 of 5

You will learn to

  • Write an algorithm in Cambridge pseudocode from a written description.
  • Use the exact keywords: DECLARE, INPUT, OUTPUT, IF, CASE, FOR, WHILE and REPEAT.
  • Use totalling, counting and a validation loop in your algorithms.
  • Use meaningful names, comments and indentation.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 2 of 5

Success criteria

You have got it when you can say:

  • I can write four short algorithms that match their descriptions.
  • I can close every structure with the right keyword, like ENDIF or NEXT.
  • I can highlight the parts of my answer that match the mark-scheme points.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 2 of 5

Retrieval starter

Questions from before. Write your answers, then show them.

  1. Last lesson

    A good primary key is…

    A A surnameB A unique ID numberC A date of birth

    B. A unique ID number

  2. Last unit

    Hex B2 in binary is…

    A 1011 0010B 1011 0100C 1100 0010

    A. 1011 0010

  3. Earlier this year

    Pharming is…

    A Guessing every passwordB Being sent to a fake website even when you type the right addressC A fake email asking for a password

    B. Being sent to a fake website even when you type the right address

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 2 of 5

Key words (1 of 8)

Program development life cycle
The four stages of making a program: analysis, design, coding and testing.
Pseudocode
A structured, code-like way of writing an algorithm that is not tied to one language.
Totalling
Keeping a running total by adding each value to a total variable.
Counting
Adding 1 to a counter variable each time something happens.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 2 of 5

Key words (2 of 8)

Linear search
Checking each item in a list in turn until the target is found or the list ends.
Bubble sort
Sorting by comparing neighbouring items and swapping them if they are in the wrong order, pass after pass.
Validation
An automatic check that input data is reasonable and follows set rules.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 2 of 5

Key words (3 of 8)

Verification
A check that data has been copied or typed in exactly as the original.
Check digit
An extra digit calculated from the other digits, used to catch typing errors.
Normal data
Test data that should be accepted.
Boundary data
The largest and smallest accepted values and the nearest rejected values next to them.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 2 of 5

Key words (4 of 8)

Trace table
A table used in a dry run to record the value of each variable as an algorithm runs.
Variable
A named place in memory whose value can change while the program runs.
Constant
A named value that is set once and cannot change while the program runs.
Identifier
The name given to a variable, constant, array or subroutine.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 2 of 5

Key words (5 of 8)

Data type
The kind of value stored: INTEGER, REAL, CHAR, STRING or BOOLEAN.
Assignment
Storing a value in a variable, written with ← in pseudocode.
Sequence
Statements run one after another in the order they are written.
Selection
Choosing which statements to run using a condition (IF or CASE).

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 2 of 5

Key words (6 of 8)

Iteration
Repeating a block of statements in a loop.
Count-controlled loop
A FOR loop that repeats a set number of times.
Pre-condition loop
A WHILE loop that checks its condition before each pass, so it may run zero times.
Post-condition loop
A REPEAT UNTIL loop that checks its condition after each pass, so it runs at least once.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 2 of 5

Key words (7 of 8)

Procedure
A named subroutine that does a task and is run with CALL; it does not return a value.
Function
A named subroutine that returns a single value to where it was called.
Parameter
A variable in a subroutine heading that receives a value when the subroutine is called.
Local variable
A variable declared inside a subroutine that can only be used inside it.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 2 of 5

Key words (8 of 8)

Global variable
A variable declared in the main program that can be used anywhere in the program.
Library routine
A ready-made, tested subroutine provided with the language, such as ROUND or RANDOM.
Array
A data structure holding many values of the same data type under one name, each found by its index.

7.4 Standard methods of solution · The explanation · step 1 of 34 · part 1 of 5

Totalling and counting

Totalling means adding values into a running total: `Total ← Total + Value`.

Counting means adding 1 each time something happens: `Count ← Count + 1`.

Both variables must start at 0 before the loop.

7.4 Standard methods of solution · The explanation · step 1 of 34 · part 2 of 5

Totalling and counting (continued)

e.g.Example
Marks 34, 48, 21, 40, 15, 45. Pass mark 25. Total: 0 → 34 → 82 → 103 → 143 → 158 → 203 PassCount: 34 yes (1), 48 yes (2), 21 no, 40 yes (3), 15 no, 45 yes (4) Output: Total: 203, Number of passes: 4

7.4 Standard methods of solution · The explanation · step 1 of 34 · part 3 of 5

Totalling and counting (continued)

DECLARE Marks : ARRAY[1:6] OF INTEGER
DECLARE Index : INTEGER
DECLARE Total : INTEGER
DECLARE PassCount : INTEGER
// Marks already holds 34, 48, 21, 40, 15, 45
Total ← 0
PassCount ← 0
FOR Index ← 1 TO 6
  Total ← Total + Marks[Index]
  IF Marks[Index] >= 25
    THEN
      PassCount ← PassCount + 1
  ENDIF
NEXT Index
OUTPUT "Total: ", Total
OUTPUT "Number of passes: ", PassCount

7.4 Standard methods of solution · The explanation · step 1 of 34 · part 4 of 5

Totalling and counting (continued)

marks = [34, 48, 21, 40, 15, 45]
total = 0
pass_count = 0
for index in range(6):
    total = total + marks[index]
    if marks[index] >= 25:
        pass_count = pass_count + 1
print("Total:", total)
print("Number of passes:", pass_count)

7.4 Standard methods of solution · The explanation · step 1 of 34 · part 5 of 5

Totalling and counting (continued)

!Watch out

Setting Total to 0 inside the loop is a classic error. The total is wiped each time round, so only the last value is kept.

7.4 Standard methods of solution · The explanation · step 2 of 34 · part 1 of 6

Finding the maximum and minimum

To find the maximum (largest), keep a variable `Highest`. Compare each value with it. If the value is bigger, it becomes the new `Highest`.

The minimum (smallest) works the same way with `Lowest` and `<`.

Start `Highest` at the smallest possible value and `Lowest` at the largest possible value. Another safe way is to start both at the first item.

7.4 Standard methods of solution · The explanation · step 2 of 34 · part 2 of 6

Finding the maximum and minimum (continued)

e.g.Example
Marks out of 50: 34, 48, 21, 40, 15, 45. Highest: 0 → 34 → 48 (21, 40, 15, 45 are not bigger than 48) Lowest: 50 → 34 → 21 → 15 (40, 45 are not smaller than 15) Output: Highest: 48, Lowest: 15

7.4 Standard methods of solution · The explanation · step 2 of 34 · part 3 of 6

Finding the maximum and minimum (continued)

DECLARE Marks : ARRAY[1:6] OF INTEGER
DECLARE Index : INTEGER
DECLARE Highest : INTEGER
DECLARE Lowest : INTEGER
// Marks already holds 34, 48, 21, 40, 15, 45 (each out of 50)
Highest ← 0
Lowest ← 50

7.4 Standard methods of solution · The explanation · step 2 of 34 · part 4 of 6

Finding the maximum and minimum (continued)

FOR Index ← 1 TO 6
  IF Marks[Index] > Highest
    THEN
      Highest ← Marks[Index]
  ENDIF
  IF Marks[Index] < Lowest
    THEN
      Lowest ← Marks[Index]
  ENDIF
NEXT Index
OUTPUT "Highest: ", Highest
OUTPUT "Lowest: ", Lowest

7.4 Standard methods of solution · The explanation · step 2 of 34 · part 5 of 6

Finding the maximum and minimum (continued)

marks = [34, 48, 21, 40, 15, 45]
highest = 0
lowest = 50
for index in range(6):
    if marks[index] > highest:
        highest = marks[index]
    if marks[index] < lowest:
        lowest = marks[index]
print("Highest:", highest)
print("Lowest:", lowest)

7.4 Standard methods of solution · The explanation · step 2 of 34 · part 6 of 6

Finding the maximum and minimum (continued)

!Watch out

If Lowest starts at 0, no mark is ever smaller, so the answer is wrong. Start Lowest at the top of the range (50 here) or at the first value.

The explanation · 32 more steps

More on the lesson page

The lesson page has the rest of the explanation, step by step:

  • Finding the average
  • Linear search
  • Bubble sort
  • How many comparisons? (extra)
  • Validation and verification are different
  • Range, length, type and presence checks
  • Format and check digit checks
  • Writing a validation loop
  • Verification: visual check and double entry
  • Variables: named boxes in memory
  • Constants: values that never change
  • The five data types
  • Choosing the right data type
  • Input and output
  • Sequence: one step after another
  • Selection with IF
  • Nested IF: up to three levels
  • Selection with CASE
  • Relational and logical operators
  • Arithmetic operators, DIV and MOD
  • Iteration: the count-controlled FOR loop
  • The pre-condition WHILE loop
  • The post-condition REPEAT UNTIL loop
  • Choosing the right loop
  • Totalling and counting
  • String handling: LENGTH, UCASE, LCASE
  • SUBSTRING and the first position: 1 or 0
  • Procedures and parameters
  • Functions: subroutines that return a value
  • Local and global variables
  • Library routines: MOD, DIV, ROUND, RANDOM
  • Writing maintainable programs

Open the lesson page

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 2 of 5

Quick quiz · 1 of 3

A diamond in a flowchart means…

  1. A Input or output
  2. B A process
  3. C A decision

C. A decision

Rectangle: process. Parallelogram: input or output. Diamond: a question with yes and no.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 2 of 5

Quick quiz · 2 of 3

Bubble sort stops when…

  1. A Half the list is sorted
  2. B A whole pass makes no swaps
  3. C The first swap happens

B. A whole pass makes no swaps

No swaps in a pass means everything is in order.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 2 of 5

Quick quiz · 3 of 3

FOR i ← 1 TO 10 … NEXT i runs the loop…

  1. A 10 times
  2. B 9 times
  3. C 11 times

A. 10 times

1 to 10 inclusive. Python's range(1, 11) does the same.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 2 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. A REPEAT … UNTIL loop…

    A Always runs at least onceB Runs for everC May run zero times

    A. Always runs at least once The test is at the end, so the body runs first. WHILE tests first and may run zero times.

  2. 17 MOD 5 is…

    A 3.4B 2C 3

    B. 2 MOD is the remainder: 17 = 3 × 5 + 2. DIV would give 3.