Seneca

Correcting Algorithms

Computer Science: OCR GCSE, topic 2.1.7

Correcting Algorithms

Knowing what an algorithm does is important. Being able to fix an algorithm if it all goes wrong is also important.

Incorrect identifiers

Incorrect identifiers

  • A common mistake in programs is incorrect identifiers.
  • Be very careful with uppercase and lowercase letters.
  • Be careful with similar looking characters:
  • 0 and O.
  • 1 and l (lower case L).
Incorrect operators

Incorrect operators

  • Look out for the difference between similar operators.
  • One example is testing if a number is less than another, rather than less than or equal to the other.
Missing processes

Missing processes

  • Sometimes a line of code has been forgotten.
  • A forgotten line of code can lead to issues such as infinite loops.
  • In an infinite loop, the code will never end.

Examples of how to Correct Algorithms

Incorrect identifiers

Incorrect identifiers

  • In this example, there is a problem on line 1.
  • Weather has initially been misspelt.
  • If the program were to run, the programmer would see an error indicating that there is no variable that is named weather, because instead, it was defined before as whaether. So, the program can not see if the value of sun is stored within the weather variable.
Incorrect operators

Incorrect operators

  • This code determines whether a number is odd or even.
  • A number is even if it is EXACTLY divisible by 2 with NO REMAINDER.
  • The error is on line 3.
  • Whilst >= does include 0, it also includes the only other answer of 1.
  • If this program were to be run, all numbers would be shown as even.
  • To fix the code, we need to replace > with =

Test yourself on Correcting Algorithms

  • Which pair of characters can easily be confused when typing an identifier?

  • Which operator mistake can cause errors in an algorithm?

  • Choose the true statement in each pair.

  • Choose the true statement in each pair about common algorithm errors.

  • A program uses an if, elif, else statement to grade scores. The first condition checks if score >= 0 and prints 'Good attempt'. The next condition checks if score >= 90 and prints 'High Score'. What will the program print for a score of 85?

Practise this topic for free