topic badge
AustraliaVIC
VCE 12 Methods 2023

11.01 Algorithms in code

Lesson

 

Previously we have learnt how to represent an algorithm (a sequence of steps) using a flow chart diagram. Each step of an algorithm can also be represented by a set of worded instructions called a line of code. There are many different representations of code, called programming language. These include Python, Scratch, Java, and C. We use programming language to communicate with computers to give them the instructions that we want them to perform.  

A programming language usually consists of

    Statements - which give an instruction for the action - written as capital letters

    Expressions  - which represent a value
 

Pseudocode

Before translating an algorithm into a specific programming language, the steps of the algorithm can be written as pseudocode. Pseudocode is easy to read coding language that can’t be used by a computer but can be easy to follow for humans to check and make sure the program will work before writing into a specific computer language.

 

Common pseudocode notation

There is no strict set of standard notations for pseudocode, but some of the most widely recognised are:

INPUT – indicates a user will be inputting something

OUTPUT or PRINT – indicates that an output will appear on the screen

SET - assign an initial value to an expression

IFTHENELSE – a decision (selection) in which a choice is made

Any instructions that occur inside a decision are usually indented.

Example:

The following Algorithm adds 2 numbers:

This algorithm can be represented in pseudocode as:

START
SET a = 2
SET b = 3
PRINT a + b
END

Question: What is the OUTPUT of this code?

The steps of this code are:

assign a the value 2

assign b the value 3

print the answer of a + b

So the OUTPUT of this code is 5

Making a decision

When a decision is to be made between 2 alternatives in code, we can use the IF – THEN – ELSE statement.  

Example:

The following algorithm determines which of 2 numbers is greater:

START
SET a = 8
SET b = 5
IF a >= b
    THEN PRINT a
ELSE PRINT b
END IF
END

Question: What is the OUTPUT of this code?

The steps of this code are:

assign a the value 8

assign b the value 5

If a (8) is larger than or equal to b (5), then print a (8)

Otherwise print b (5)

Since a is larger than or equal to b the OUTPUT of this code is 8.

Example 1:

 

Look at the pseudocode below

START
SET a = 73
SET b = 44
SET c = a + b
PRINT c
END

What value is printed at the end of the code?

  1. OUTPUT: $\editable{}$

Example 2:

Look at the pseudocode below

START
SET a = 8
IF a / 2 IS integer
    THEN PRINT "a is even"
ELSE PRINT "a is odd"
END IF
END

What statement is printed at the end of the code?

Note: the symbol / represents division.

  1. a is even

    A

    a is odd

    B

Example 3:

Consider the rectangle shown in the image.

The dimensions of the rectangle are $W=3$W=3 metres and $L=6$L=6 metres.

  1. What does the following pseudocode calculate?

    START
    SET W = 3
    SET L = 6
    SET A = W * L
    PRINT A "m2"
    END

    Note: the symbol * represents multiplication.

    Volume

    A

    Units

    B

    Area

    C

    Perimeter

    D
  2. What value is printed at the end of the code?

    OUTPUT: $\editable{}$ m2

 

Outcomes

U34Out2.4

key elements of algorithm design, including sequencing, decision-making and repetition, and representations of the ordered steps for an algorithm including through the use of pseudocode.

What is Mathspace

About Mathspace