Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Subject:
Topics:
Avg
score:
–
Topics
done:
0/32

Writing and amending algorithms

Practise 39 exam-style questions on Writing and amending algorithms for Cambridge IGCSE Computer Science (Paper 2). Write your answer as you would in the exam and get it marked in seconds against the mark scheme, point by point, with an AI tutor on hand if you get stuck.

  • 39 questions
  • 1–7 marks each
  • Instant marking
  • No sign-up needed
20 questions, marked as you go

Sample questions with model answers

Here are 4 of the questions in this topic, each with a model answer, an explanation of why it earns the marks and the mark scheme an examiner would use.

Question 1

2 marks

A student wrote this line in an algorithm for a speed camera:

IF Speed is more than the speed limit THEN OUTPUT "Fine"

The speed limit is stored in the constant Limit.

Explain why this line would not be accepted as pseudocode, and rewrite it as precise Cambridge pseudocode.

Model answer

The condition is written in English instead of using a relational operator, and it does not use the identifier Limit, so it is not precise.

IF Speed > Limit
  THEN
    OUTPUT "Fine"
ENDIF

Explanation

Paper 2 algorithms must be precise: comparisons are written with operators such as >, < and = and use the actual identifiers. A phrase like 'is more than the speed limit' could be read in different ways and could not be turned directly into program code. Every IF also needs THEN and a closing ENDIF.

Mark scheme
  1. 1 mark

    The condition is written in English words ('is more than the speed limit'), so it is not precise / does not use a relational operator / does not use the identifier Limit

  2. 1 mark

    Correct pseudocode using the > operator and the identifier Limit, with THEN and ENDIF, e.g. IF Speed > Limit THEN OUTPUT "Fine" ENDIF

    accept the IF statement laid out over several lines; do not accept >= (more than means >); do not accept a missing ENDIF

Question 2

3 marks

This pseudocode calculates the average height of the students in a class of 30.

Total ← 0
FOR Student ← 1 TO 30
    INPUT Height
    Total ← Total + Height
NEXT Student
OUTPUT "Average height: ", Total / 30

Classes can be different sizes.

Rewrite the algorithm so that the user first inputs the number of students in the class, and the average is then calculated for that number of students.

Model answer

OUTPUT "How many students are in the class?"
INPUT ClassSize
Total ← 0
FOR Student ← 1 TO ClassSize
    INPUT Height
    Total ← Total + Height
NEXT Student
OUTPUT "Average height: ", Total / ClassSize

Explanation

The fixed value 30 appears in two places - the loop limit and the division - and both must be replaced by the variable holding the class size. Missing one is a common error: the loop could run the right number of times but the average would still be divided by 30. The class size must be input before the loop starts.

Mark scheme
  1. 1 mark

    MP1 inputs the number of students into a variable before the loop, e.g. INPUT ClassSize

  2. 1 mark

    MP2 the loop runs from 1 to that variable, e.g. FOR Student ← 1 TO ClassSize

    do not accept the loop still running to 30

  3. 1 mark

    MP3 the average is calculated by dividing by the same variable, e.g. Total / ClassSize

Question 3

4 marks

This pseudocode inputs 10 scores for a darts player and outputs the total.

Total ← 0
FOR Throw ← 1 TO 10
    INPUT Score
    Total ← Total + Score
NEXT Throw
OUTPUT "Total: ", Total

Each score is a whole number from 0 to 180.

Rewrite the algorithm so that it also finds and outputs the highest score.

Model answer

Total ← 0
Highest ← 0
FOR Throw ← 1 TO 10
    INPUT Score
    Total ← Total + Score
    IF Score > Highest
      THEN
        Highest ← Score
    ENDIF
NEXT Throw
OUTPUT "Total: ", Total
OUTPUT "Highest: ", Highest

Explanation

Amending an algorithm means keeping everything that already works and adding only what is needed: a new variable, a comparison inside the loop and an extra output. Highest can start at 0 here because no score can be lower than 0; if negative values were possible, it should start at the first value input instead.

Mark scheme
  1. 1 mark

    MP1 initialises a variable for the highest score before the loop to a value no higher than the lowest possible score (e.g. Highest ← 0 or Highest ← -1)

    accept setting Highest to the first score input

  2. 1 mark

    MP2 inside the loop, compares each score with the highest, e.g. IF Score > Highest

  3. 1 mark

    MP3 replaces the highest with the score when the score is larger, e.g. Highest ← Score

  4. 1 mark

    MP4 outputs the highest score after the loop, with the original total still calculated and output

Question 4

5 marks

Write an algorithm in pseudocode that inputs a positive whole number and outputs the sum of its digits. For example, an input of 4072 gives an output of 13.

You must use the DIV and MOD functions; do not treat the number as a string.

Model answer

OUTPUT "Enter a positive whole number"
INPUT Number
DigitSum ← 0
WHILE Number > 0 DO
    DigitSum ← DigitSum + MOD(Number, 10)
    Number ← DIV(Number, 10)
ENDWHILE
OUTPUT "Sum of digits: ", DigitSum

Explanation

MOD(Number, 10) gives the last digit (the remainder when dividing by 10) and DIV(Number, 10) removes it. Repeating these until the number reaches 0 visits every digit: 4072 gives 2, then 407 gives 7, then 40 gives 0, then 4 gives 4, so the sum is 13. The number of digits is not known in advance, so a condition-controlled loop is needed.

Mark scheme
  1. 1 mark

    MP1 inputs the number and initialises the sum to 0

    max 5

  2. 1 mark

    MP2 a condition-controlled loop that repeats while the number is greater than 0

    accept REPEAT ... UNTIL Number = 0

  3. 1 mark

    MP3 extracts the last digit using MOD(Number, 10)

  4. 1 mark

    MP4 adds the last digit to the sum inside the loop

  5. 1 mark

    MP5 removes the last digit inside the loop, e.g. Number ← DIV(Number, 10)

  6. 1 mark

    MP6 outputs the sum after the loop

Ready to try the rest? Every answer you write is marked against the mark scheme, so you can see exactly where the marks are.

AI Tutor Hints first, answers after you try

Hi! Stuck? Ask me for a hint and I’ll help you work it out. Once you’ve checked your answer I can explain exactly where the marks are.