Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

ALGORITHM

1
Starter activity

Five sticks
 Nina has five sticks. She puts them on the table and creates this shape:

 Amica comes to the table. She takes one stick and puts it in a different place:

 Then Attaliya comes to the table, she also takes one stick and puts it in a different
place.

Question:
Which shape is Attaliya not able to make?
A B C D
Think, write, pair, share 2
Starter activity

Five sticks
Answer:
The answer is D.

Explanation:

Needs more than


one stick to be
moved.

A B C D

3
Algorithm
Objectives

In this lesson, you will:


1. Define algorithm
2. list at least three characteristics of
algorithms
3. state three ways of representing
algorithms
4. create and write algorithms 4
Algorithm - Definition

 An algorithm is a step-by-step sequence of


instructions that are used to solve a
problem.

 An algorithm is sequence of finite steps to


solve a particular problem.
5
Algorithm - Characteristics

1. The steps must be finite


2. The steps must be precise
3. The steps must be unambiguous
4. The steps must have a flow of control
from one process to another
5. The steps must terminate
6
Algorithm - Characteristics

 The steps must be finite:

 This means that the algorithm must come


to an end after a certain number of steps.
 This is important because it ensures that
the algorithm will not run forever and
consume infinite resources. 7
Algorithm - Characteristics

 The steps must be precise:

 This means that the steps in the algorithm


must be clear and unambiguous.
 Each instruction in an algorithm must be
precise enough to be understood
independently.
 If the steps in an algorithm are not precise, 8

the algorithm may not produce the correct


output.
Algorithm - Characteristics

 The steps must be unambiguous:

 This means that the steps in the algorithm


must have only one possible interpretation.
 There should be no room for different
interpretations.
 If the steps in an algorithm are not
unambiguous, the algorithm may produce 9

different outputs depending on who is


following it.
Algorithm - Characteristics

 The steps must have a flow of control


from one process to another:

 This means that the steps in the algorithm


must be executed in a specific order.
 The algorithm should specify which step
should be executed next based on the
result of the previous step. 10
Algorithm - Characteristics

 The steps must terminate:

 This means that the algorithm must come


to an end after a certain number of steps
and produce an output.
 This is important because it ensures that
the algorithm will not run forever without
producing a result. 11
Algorithm – representing algorithm

Algorithms can be represented using:

1. Narrative
2. Pseudocode
3. Flowchart

12
Algorithm – representing algorithm using
narrative

Narrative

With narrative, each step or instruction in the


algorithm is written in clear, simple language
or natural language.
It involves explaining the step-by-step process
of the algorithm in a clear and understandable 13

manner.
Algorithm – representing algorithm using
narrative

Example: An algorithm to add two numbers


and display their sum:

1. Enter the first number


2. Enter the second number
3. Add the numbers together
4. Display the total 14
Algorithm – representing algorithm using
narrative

Exercise 1: create an
algorithm for making a cup of
coffee

15
Algorithm – representing algorithm using
narrative

Exercise 1: create an algorithm for making a


cup of coffee
1. Fill kettle with water.

2. Turn on kettle.

3. Place coffee in cup.

4. Wait for water to boil.

5. Pour water into cup.


16

6. Add milk and sugar.

7. Stir.
Algorithm – representing algorithm using
pseudocode

Pseudocode

 The name ‘pseudocode’ comes from the


term ‘pseudo’, meaning ‘fake’ and the term
‘code’, meaning program. It is a fake
program.
 It uses words and symbols that closely
resembles a computer program to describe 17

the steps an algorithm.


Algorithm – representing algorithm using
pseudocode

● Even though in pseudocode, the terms used


closely resembles a computer program, they
are used without following the rigid rules of
the language (known as the syntax).
● Pseudocode can contain variables, constants
& different operators (+, *, >, <, =, ).
● Pseudocode is more precise than narrative,
18

but it is still less formal than a programming


language.
Algorithm – representing algorithm using
pseudocode

Example: An algorithm to add two numbers


and display their sum:

1. INPUT number1
2. INPUT number2
3. SET total  number1 + number2
4. OUTPUT total 19
Algorithm – representing algorithm

comparing narrative and


pseudocode

20
Algorithm – representing algorithm:
comparing narrative and pseudocode

Narrative

1. Enter the first number


2. Enter the second number
3. Add the numbers together
4. Display the total
21
Algorithm – representing algorithm:
comparing narrative and pseudocode

Pseudocode

1. INPUT number1
2. INPUT number2
3. SET total  number1 + number2
4. OUTPUT total
22
Algorithm – representing algorithm using
pseudocode

Exercise 2:
create an algorithm that will
accepts 3 numbers and then
calculate their average.

23
Algorithm – representing algorithm using
pseudocode

Solution:

1. INPUT number1, number2, number3


2. SET average  (number1 + number2 +
number3) / 3
3. OUTPUT average
24
Algorithm – representing algorithm using
flowcharts

 Flowchart is a graphical representation of


an algorithm and uses symbols to denote
each step, and arrows showing how to move
between each step.
 A flowchart may use any of the following
symbols:

25
Algorithm – representing algorithm using
flowcharts

26
Algorithm – representing algorithm:
flowchart

Flowchart

1. START
2. INPUT number1
3. INPUT number2
4. SET total  number1 + number2
5. OUTPUT total 27

6. STOP
Algorithm – representing algorithm using
flowcharts

Flowchart

1. START
2. OUTPUT ‘enter 2
numbers’
3. INPUT number1,
number2
4. SET total  number1
+ number2
5. OUTPUT total
6. STOP 28
Algorithm – representing algorithm using
flowcharts

Exercise 3:

1. START
2. INPUT number1, number2, number3
3. SET average  (number1 + number2 +
number3) / 3
4. OUTPUT average
29

5. STOP
Algorithm – Quiz

1. Define algorithm
2. List at 3 characteristics of algorithms
3. What are two ways to represent algorithm?

30
Summary

In this lesson, you


learnt…
Next lesson, you
● The definition of will…
algorithm
● characteristics of Learn about
algorithm pseudocode and
● Ways of representing flowcharts in more
algorithm
detail along with
 Narrative
control structures
 Pseudocode

 Flowcharts

● Represented 31

algorithms using:

You might also like