Algorithm Handout 24
Algorithm Handout 24
Algorithm Structures
Grade 11
An algorithm is a set of instructions that if followed in sequence will lead to a solution for the
problem.
An algorithm is a sequence of instructions that set out a step-by-step procedure for solving a
problem. It is a list of well-defined actions for completing a task. As you follow an algorithm
you proceed through a well-defined series of step, eventually reaching your goal.
All algorithms:
1. have a set number of steps
2. are precise
3. are unambiguous
4. have instructions that pass the flow of control from process to another
5. eventually end.
An algorithm is precise when it is strictly defined and accurately stated. It is ambiguous when it
is open to multiple interpretations or is vague and unclear.
Algorithms can be represented as:
Written using pseudocode
Drawn as a flowchart.
Pseudocode
This is a type of algorithm, which is an imitation program written using mathematical notations
and English-like statements to describe the logics to solve a problem or carry out a procedure.
These statements comprise of key terms, arithmetic operators and variables or constants.
Each variable or constant must be assigned a data type. The data type specifies the value that
can be stored in a variable or a constant. Data types include:
Assignment Statements
The assignment statements are used to give initial values to variables and to change the value
assigned to a variable. The assignment has two parts the Lvalue and the Rvalue. The Lvalue
refers to the variable as a storage location while the Rvalue refers to what is to be stored. The
format of an Assignment Statement is:
Var_name Rvalue
This means that the value Rvalue will be stored in the variable Var_name.
Example area pi * radius * radius, area = pi* radius * radius
Output Statements
The output statements are used to display the values stored within a variable and communicate
with the user.
The format of an Output Statement used to display the value stored within a variable:
Print var_name
This means that whatever is stored in the variable var_name will be displayed on the monitor.
Pseudocode Statements
What do you want to do Verb/Construct to use How to use
Input data Input Input Name
Read Read Name
Accept
Get
Initialise a variable = Age = 0
Output data and information Output Output ‘Please enter a number’
Print Print ‘End of processing’
Write Output Total
Display
Make a decision If then If x>5 then
Print x
Endif
Symbol Use
+ Addition
- Subtraction
* Multiplication
/ Division
> Greater than
< Less than
>= Greater than or equal to
=< Equal to or less than
= Equal to
<> Not equal to
Types of Algorithm Structures
Sequential
The sequence of actions means the order that they go in. Sometimes the order of the action does
not matter. But sometimes actions depend on the results of previous actions.
Example Write a program that will accept two integers, calculate and print their total.
This algorithm reads two numbers, calculate and print their total.
Start
Print “Please enter two integers”
Read num1, num2
14 15
The total is 29
Activity
1. Write an algorithm to read three numbers. Calculate and print their product.
2. Write an algorithm to read the length and width of a rectangle. Calculate and print the
area and perimeter of the rectangle.
If-then
This statement suggests that one or more statements will only be considered based on a condition
or the answer to a question.
If-then-else
This statement directs the algorithm to one or more statements if the outcome of the condition is
true. The algorithm is directed to another set of statements if the outcome of the condition if the
condition is false.
Condition
Conditions are statements that are created by the programmer which evaluates actions in the
program and evaluates if it's true or false.
Endif
A directive that marks the end of an if statement.
Example
Write an algorithm to read two numbers. Print the smaller of the two numbers.
Algorithm Numbers
This algorithm reads two numbers and prints the smaller of the two numbers.
Variables
Num1, Num2: Integer
Start
Print “Please enter two numbers.”
Read Num1, Num2
If Num1< Num2 then
Print Num1
Else
Print Num2
Endif
Stop
Loops
A loop is a sequence of instruction s that is continually repeated until a certain condition is
reached. Loop structures are used to repeat a set of instructions or set of steps.
For Loop
A "For" Loop is used to repeat a specific block of code a known number of times. For example,
if we want to check the grade of every student in the class, we loop from 1 to that number. A for
loop is also known as a definite loop. Definite loops are executed a set number of times.
Example
A shop gives a 10% discount on sales totaling more the $2000.00. Write a pseudocode algorithm
that prompts the user to input four amounts of sale for ten persons. Calculate and print the total
sale, the discount amount and the amount after the discount is deducted for each person.
Algorithm Price
This algorithm prompts the user to input four amounts of sales for 10 persons. It reads the
sales, calculates and prints the total sales, the discount amount and the amount after the
discount is deducted for each person.
Constant
DisRate = 0.1
SalesMore = 2000.00
Variables
Sales1, Sales2, Sales3, Sales4,
TotalSales, DiscAmount, NetSales: Real
Counter: Integer
Start
TotalSales = Sales1+Sales2+Sales3+Sales4
DiscAmount = TotalSales*DisRate
NetSales = TotalSales-DiscAmount
Endif
While Loop
A "While" Loop is used to repeat a specific block of code an unknown number of times, until a
condition is met. For example, if we want to ask a user for a number between 1 and 10, we don't
know how many times the user may enter a larger number, so we keep asking "while the number
is not between 1 and 10".
Write an algorithm to read a sequence of number terminated by the number 0. Find and print the
sum of negative and positive numbers and the product of the two sums.
Algorithm Values
This algorithm reads a sequence of numbers terminated by 0. It finds and prints the sum the
negative and positive numbers and the product of the two sums.
Variables
Num, NSum, PSum, Product:Integer
PSum = 0
NSum =0
Start
Print “Please enter a sequence of numbers.”
Read Num
Example
Given the number is equal to one, add one to the value of number and print the value of number
until number = 5
Number = 1
Repeat
Number = Number + 1
Output “The number is”, Number
Until (Number = 5)
Output “Out of loop”
End