Write pseudocode from a structured English description

Resources | Subject Notes | Computer Science

Cambridge A-Level Computer Science 9618 - 9.2 Algorithms - Pseudo-code from Structured English

Cambridge A-Level Computer Science 9618

Topic: 9.2 Algorithms

Objective: Write pseudo-code from a structured English description

This section provides detailed notes and examples on writing pseudo-code from structured English descriptions, a crucial skill for the Cambridge A-Level Computer Science 9618 exam. Pseudo-code is an informal way of representing an algorithm, making it easier to understand and translate into a specific programming language.

Understanding Structured English

Structured English is a simplified form of English that uses keywords to represent control structures like sequencing, selection (if-then-else), and repetition (loops). It provides a clear and concise way to describe the logic of an algorithm.

Common keywords in structured English include:

  • BEGIN and END: Mark the start and end of a block of code.
  • IF: Starts a conditional statement.
  • THEN: Follows the condition in an IF statement.
  • ELSE: Provides an alternative block of code if the condition is false.
  • WHILE: Starts a loop that continues as long as a condition is true.
  • REPEAT: Starts a loop that repeats a block of code a specified number of times.
  • UNTIL: Starts a loop that continues until a condition becomes true.
  • Sequential statements: Statements executed in order.

Converting Structured English to Pseudo-code

The process of converting structured English to pseudo-code involves translating the keywords and logical structures into a more formal, algorithm-like format. Here's a general approach:

  1. Identify the control structures (sequencing, selection, repetition).
  2. Translate the keywords into their pseudo-code equivalents.
  3. Use indentation to clearly show the structure of the algorithm.
  4. Use comments to explain the purpose of different sections of the code.

Example 1: Finding the Maximum of Two Numbers

Structured English:

    BEGIN
        Input the first number.
        Input the second number.
        IF the first number is greater than the second number THEN
            Output the first number as the maximum.
        ELSE
            Output the second number as the maximum.
        ENDIF
    END
    

Pseudo-code:

Step Description
1 Begin
2 Input number1
3 Input number2
4 If number1 > number2 Then
5 Output number1
6 Else
7 Output number2
8 End If
9 End

Example 2: Calculating the Sum of Numbers in a List

Structured English:

    BEGIN
        Input the number of elements in the list.
        Input each element of the list.
        Set the sum to zero.
        FOR each element in the list DO
            Add the element to the sum.
        END FOR
        Output the sum.
    END
    

Pseudo-code:

Step Description
1 Begin
2 Input list_size
3 Set sum = 0
4 For i = 1 to list_size do
5 Input element[i]
6 sum = sum + element[i]
7 End For
8 Output sum
9 End

Example 3: Checking if a Number is Positive

Structured English:

    BEGIN
        Input a number.
        IF the number is greater than zero THEN
            Output "The number is positive."
        ELSE
            Output "The number is not positive."
        ENDIF
    END
    

Pseudo-code:

Step Description
1 Begin
2 Input number
3 If number > 0 Then
4 Output "The number is positive."
5 Else
6 Output "The number is not positive."
7 End If
8 End

Key Considerations

When writing pseudo-code, it's important to:

  • Use clear and concise language.
  • Indicate the data types of variables.
  • Show the flow of control clearly.
  • Use comments to explain complex logic.