Algorithms Grade 12
Algorithms Grade 12
SCHOOL
MARIO@2024
2
Algorithm planning
and design
MARIO@2024
3
ALGORITHM
MARIO@2024
4
1. Problem recognition
2. Definition
3. Design
4. Program coding
5. Program testing
6. Implementation
MARIO@2024
6
EXERCISE
1. Define an algorithm
2. Explain the first two guidelines on how to develop a
system.
3. list the stages on how to develop a system
MARIO@2024
7
PROGRAM
DEVELOPMENT LIFE
CYCLE
MARIO@2024
Problem Problem 8
recognition recognition
Problem
definition
Program
design
KEY Program
▪ The solid arrows coding
show the next step.
▪ The broken arrows Program
show the previous testing
step.
Program
MARIO@2024
implementation
9
PROBLEM RECOGNITION
MARIO@2024
10
PROBLEM DEFINITION
MARIO@2024
11
PROGRAM DESIGN
MARIO@2024
pseudocode Flowchart 12
START
BEGIN
SET PI = 3 .142
Read radius
WRITE “ Enter radius of the
sphere”: PI = 3. 142
READ radius Volume = 4/3 *PI* radius3
Volume = 4/3 * PI * radius 3
Print Volume
WRITE Volume
END
MARIO@2024
Stop
13
PROGRAM CODING
MARIO@2024
14
IMPLEMENTATION
MARIO@2024
17
EXERCISE
1.Explain the following system developments stages.
(a)Problem recognition
(b)Program design
(c)Implementation
2. At what stage of system development errors can
be detected?
3. State two error and explain each error.
MARIO@2024
18
HIERARCHICAL DIAGRAM
Library Management
System
Borrow Return
MARIO@2024
22
▪Correctness
▪Verifiability
▪Completeness
▪Traceability
▪Efficiency
▪Simplicity
▪Design documentation
MARIO@2024
23
EXERCISE
MARIO@2024
24
ALGORITHM DESIGN
TOOLS
MARIO@2024
25
▪Pseudocode
▪Flowchart
▪Structure charts
▪Decision tables
MARIO@2024
26
1. PSEUDOCODE
MARIO@2024
27
EXAMPLE OF A PSEUDOCODE
1. Write the pseudocode to calculate the area of the
circle.
BEGIN
SET PI = 3.142
WRITE “Enter radius of a circle”:
READ Radius
Area = PI*radius𝟐
WRITE Area
END.
MARIO@2024
28
ADVANTAGES OF PSEUDOCODE
▪It is ease to create
▪It requires simple syntax to write
▪It reduces time spent in coding, testing and
modifying a system
▪Statements are easily translated to high level
language.
▪Pseudocode implements structured concepts
in a better way
MARIO@2024
29
DISADVANTAGES OF PSEUDOCODE
▪Pseudocode is not visual hence difficult to
understand complex logic.
▪Pseudocode varies widely because there is no
accepted standard.
▪It can be quite lengthy for complex problems.
MARIO@2024
30
EXERCISE
1. What is pseudocode?
2. Write the pseudocode to calculate the area
of the rectangle.
MARIO@2024
31
2. FLOWCHART
▪A flowchart is a graphical representation of an
algorithm.
▪It uses standard set of symbols to show the
operations and decisions to be followed by a
computer in solving a problem.
▪Simple instructions are written within boxes that are
connected using arrows to indicate the flow of
input, processing and output operations.
MARIO@2024
SYMBOLS
Shows the start and the end of the
32
Start/stop
algorithm
Read x,y Represents input and output
Predefine
d process
MARIO@2024
Start 34
PI = 3.142
Area = PI* radius𝟐
Write Area
stop
MARIO@2024
35
ADVANTAGES OF FLOWCHART
MARIO@2024
36
DISADVANTAGES OF FLOWCHART
A B C D
E
MARIO@2024
EXERCISE 38
1. The following algorithm in Pseudocode is used to calculate
the area of a triangle given base (b) and height (h).Rewrite
using flowchart
START
enter b
enter h
IF (b < 0) or (h < 0) THEN
WRITE input error
ELSE
calculate Area = 𝟏Τ𝟐 *b*h
WRITE Area
ENDIF
MARIO@2024 STOP
39
3. HIERARCHICAL CHART
MARIO@2024
40
Banking System
Withdraw Deposit
MARIO@2024
4. DECISION TABLES41
Y = Yes
N = No
I = immaterial
IF option VALID
Option = add
Add Module
Option = edit
Add Module
Option = edit
delete Module
Option = del
ELSE
Error Routine
ENDIF
MARIO@2024
Exit to main menu
44
EXERCISE
MARIO@2024
45
DESIGN AND
INTERPRETATION OF
ALGORITHM
MARIO@2024
46
MARIO@2024
47
CONTROL STRUCTURES
1. Sequence control structure
2. Selection control structure
3. Looping control structure
MARIO@2024
49
SEQUENCE CONTROL
MARIO@2024
50
Program: Payroll
Begin
Enter days worked; Direction of execution
Enter rate per day;
Grosspay = rate * days;
print Grosspay;
End
MARIO@2024
51
EXAMPLE
Start Pseudocode
BEGIN
Read L, W WRITE “Enter length and
width”
Area = L * W READ L, W
P = 2 *(L + W) Area = L * W
Perimeter = 2*(L+W)
Write Area, Perimeter WRITE Area
WRIRE Perimeter
Stop END
MARIO@2024
53
EXERCISE
IF SELECTION
▪If…… is used when only one option is
available.
Syntax
IF <condition> THEN
statements
ENDIF
MARIO@2024
EXAMPLE 56
MARIO@2024
Flowchart SOLUTION 57
Start Pseudocode
BEGIN
Read Age WRITE “Enter your Age”
READ Age
Is Age IF Age > =18
NO
>= 18? WRITE “Vote”
END
YES
Vote
MARIO@2024
58
IF …….ELSE SELECTION
▪If …….Else selection is suitable when there are two
available options.
▪Syntax
IF <condition> THEN
statements;
ELSE
Statements;
ENDIF
MARIO@2024
59
EXAMPLE
MARIO@2024
Start SOLUTION 60
Pseudocode
Read Age BEGIN
WRITE “Enter your Age”
READ Age
IF Age > =18
Is Age NO
WRITE “Vote”
>= 18?
Not eligible ELSE
to vote WRITE “ Not eligible to
YES vote”
Vote ENDIF
END
Stop
MARIO@2024
61
EXERCISE
MARIO@2024
NESTED IF SELECTION 62
EXAMPLE
1. Draw a flowchart for the program that allows the
user to key in the current date and date of birth for
several people. Depending on the calculated age,
the program classifies each person into categories.
Age (years) Category
Below 5 baby
6 - 12 adolescent
13 - 19 Teenager
20 - 34 Youth
MARIO@2024 Above 35 Adult
Start SOLUTION 64
DOB, ToDate
Is NO Is NO Is NO Is NO Is
Age Age Age Age
NO
Age
<5? <13? <20? <35? >34?
YES YES YES YES YES
WRITE Category
MARIO@2024
Stop
65
EXERCISE
MARIO@2024
SWITCH SELECTION 66
PSEUDOCODE
WHILE <condition> DO
statements
END WHILE
MARIO@2024
69
FLOWCHART
Yes
Condition? statements
No
MARIO@2024
70
EXAMPLE
Interpretation
Once a client enters the amount to withdraw,
the algorithm first uses the modulus (%) to divide
the amount by 10. if the remainder is not 0, the
algorithm displays a message “try again” and
returns to the input screen
MARIO@2024
Start FLOWCHART 72
Read Amount
bal
Yes
Yes
bal =bal - amount
Write Receipt
MARIO@2024
Stop
73
REPEAT…….UNTIL
▪ It is a post-test loop that allows the statements within
he loop to be executed at least once. This is
because the condition is tested at the end of the
loop.
REPEAT
Statements
UNTIL <condition>
MARIO@2024
74
statements
true
Condition?
MARIO@2024
false
75
EXAMPLE
The figure below shows an algorithm for computing
the sum of the first 50 natural numbers. Explain step by
step using a trace table how the flowchart
implements repeat until loop.
MARIO@2024
Start 76
Set sum = 0
Set n = 0
n = n +1
sum = sum + n
yes
n<50
no
Write sum
MARIO@2024
Stop
77
interpretation
▪ The sum and integer n are initially assigned to zero.
The algorithm then increments n by 1 before adding
it to sum.
▪ Using a Boolean condition, the algorithm test if n is
less than or equal to 50, if not, the number is
incremented and looping continues until the
condition is false.
▪ If n < 50, the condition returns false hence the
algorithm exist the loop and prints the cumulative
sum.
MARIO@2024
78
EXERCISE
1. Study the algorithm below and use it to answer the question
that follow.
start
Sum = 0
Get a value
Sum = Sum + value
Output the sum
Go step 3 to get next value
Stop
Draw a flow chart from the statement in the algorithm
MARIO@2024
79
FOR LOOP
▪ The For loop is a pre-test loop that sets the lower and
upper limit that delimits the number of times the
statements within the body are to be executed as
follows:
For loop variable = lowerlimit TO upperlimit DO
Statement
ENDFOR
MARIO@2024
EXAMPLE 80
▪ Write a pseudocode for a program that would be used to display the 50 positive
numbers and their sum.
Interpretation: requires the use of the For…..loop that sets the lower limit to 1. in every
loop, the number is added to sum and incremented by 1 until it is equal to or greater
than the upper limit as shown below:
BEGIN
SET Sum = 0
SET upperlimit t0 50
For number = 1 TO Upperlimit
Sum = Sum + number
END FOR
WRITE Sum
END
MARIO@2024
81
LAB
MARIO@2024
82
EXERCISE
MARIO@2024