Topic
Algorithm
OBJECTIVES
By the end of this topic learners should be able to:
1) Explain selection and repetition constructs
2) apply selection and repetition algorithm structures in problem solving
Algorithm
- An algorithm is a sequence of steps followed in order to perform a task /to solve a problem
Algorithm tools
- Algorithms can be represented using the following tools
1. Pseudocode
2. Flowchart
3. Structure diagrams
4. Bottom up or top down design
Pseudocode
- This is a way of describing an algorithm using English or Shona words.
- Pseudocode in simple terms its half English, half programming language code.
- It will include key programming terms
Pseudocode structure
A pseudocode can be arranged in the following ways:
1. Sequence construct
2. Selection construct
3. Iteration construct
- In form 1 we looked at sequence construct, so now let’s go to :
Selection construct
- It is used when a decision has to be made depending on the solution
- It can also be called decision construct
- So if one faced with a problem that can require a decision to be made then the selection
method is the desired one.
Selection control structures
- To solve a problem using a selection construct, there is need to control the flow of a code
instructions.
- This can be done using what are known as selection control structures.
- The key words used are:
1. If . . . Then. . .Endif
2. If . . . Then. . .Else . . . Endif
3. Case . . . of . . . Otherwise . . . End Case
If. . . Then . . . Endif
- This control structure is used when there is only one action to be taken if the condition is
true.
- A condition normally can be an expression e.g. a>b
Syntax
If <condition> Then
Statement(s)
Endif
Example 2.1
Age=18
If Age>17 Then
Display “You can register to vote”
Endif
If . . . Then . . . Else . . . Endif
- This control structure is used when there are two or more actions to be taken.
- The Else part of the construct is executed if the Boolean condition is false
- A Boolean situation consists of two states, either true or false.
Syntax
If <condition> Then
Statement(s)
Else
Statement(s)
Endif
Example 2.2
Age=18
If Age > 17 Then
Display “You can register to vote”
Else
Display “Too young to register to vote”
Endif
Case . . . of . . . Otherwise . . . End Case
- Case of is used when there are multiple options from which to choose.
Syntax
CASE<expression> OF
<expression>
<statement1>
<expression2
<statement>
OTHERWISE
Statement
END CASE
Example 2.3
Case grade of
0 to 39
Display “Fail”
40 to 44
Display “Grade E”
45 to 49
Display “Grade D”
50 to 59
Display “Grade C”
60 to 69
Display “Grade B”
70 to 100
Display “Grade A”
End Case
REPETITION structure
- The repetition structure is also known as loop or iteration structure.
- This structure is used when there is need to repeat performing the same task or instruction
over several times.
Repetition constructs
- A loop structure is built using the following constructs:
1. REPEAT ….UNTIL
2. WHILE . . . ENDWHILE
3. FOR . . . ENDFOR
REPEAT. . . UNTIL
- The sequence of instructions inside the loop is always performed at least once.
- This is because the test is performed after the sequence of instructions has been carried out.
- Each time the instructions inside the loop are performed, the condition is tested, if the
condition is false the loop repeats. If the condition is true the loop ends.
Syntax for REPEAT . . . UNTIL structure
REPEAT
Statement(s)
UNTIL <condition>=true
Example 2.4
- A pseudocode that adds up a series of numbers until the total exceeds 120
Total = 0
REPEAT
Read Number
Total = total + number
UNTIL Total >=120
PRINT Total
Interpreting the above algorithm
Line Number Description Explanation
1 Total = 0 Total variable is initialized with
the value 0
2 REPEAT Start of the loop
3 Read Number Enter the number and store
the value in variable called
Number
4 Total=total + Number Add the contains in the
variable Total to the contains
in the variable Number and
store that in Total
5 UNTIL Total > = 120 End of the loop. However
there is need to check whether
the Total is equal to or above
120.If it is then terminate the
loop and move to the next
line. If the Total is below 120
then go back to line 2.
6 PRINT Total The Total is output to the
screen
DRY RUNNING THE ABOVE ALGORITHM
- Remember to identify the variables and then make them column headings for the Trace
Table
- Our variables are : Total, Number
- Use the following test data 12,34,67,-1,25
-
Total Number PRINT Number
0
12 12
46 34
104 67
103 -1
128 25
128
WHILE . . . DO. . . ENDWHILE
- The instructions or statements inside the loop will be repeated if the condition is true.
- The loop may or may not execute those instruction since the condition is tested at the start
of the loop.
- This structure is used when one does not know how many times the steps inside a loop need
to be performed.
SYNTAX
WHILE <condition> DO
Statement(s)
ENDWHILE
Example 2.5
- Using the pseudo code in example 2.4 which adds up a series of numbers until the total
exceeds 120.
Total = 0
WHILE Total <=120 DO
Read Number
Total = Total + Number
ENDWHILE
Print Total
FOR . . . END FOR
- It is used when you want to perform the sequence of instructions in the loop a set number
of times.
- This type of a loop is sometimes called the counting loop because it counts how many times
the instructions inside the loop have been carried out.
SYNTAX
FOR <variable> = Start Value to End Value
Statement(s)
ENDFOR
Example 2.6
- Write down a pseudocode to find the sum of a series of 10 numbers
- Dry run the pseudocode using the following test data 5,8,2,10,13,12,100,-13,20,80,50
Pseudocode
Sum = 0
FOR I. = 1 to 10
Read Number
Sum = Sum + Number
ENDFOR
Print Sum
Total Number Print total
0
5 5
13 8
15 2
25 10
35 13
47 12
147 100
134 -13
154 20
234 80
- The above pseudocode contains a sequence of instructions inside the loop(i.e. the steps
between the For … End for instruction)
- The steps inside the loop allow a user to input the number and then add this number to sum
which starts off at zero(0)
- This is alone a sum of 10 times, which means that 10 numbers are added together as the
user enters them.
- When the loop has been repeated 10 times, it moves on to the step outside the loop, which
results in the final sum of all 10 numbers being output.
Example 2.7
- A routine is required where you input 60 numbers and the number of negative numbers are
counted and then the result is output.
Negnum=0
FOR a = 1 to 60
READ number
If number < 0 then
Negnum=negnum + 1
Endif
ENDFOR
Output Negnum
Activity
1) Write a pseudocode to prepare a meal of your choice.
2) What is the purpose of the following algorithm, which is part of a program to be used in
the shop’s computer system?
Input X
Input Y
If X< Y then
Temp=X
X=Y
Y=Temp
Endif
Output X
Output Y