Algorithms
GOOD DAY, students. This is lesson 24 in our series of lessons. In this week’s lesson, we will continue to look at the representation of algorithms with pseudocode and flow charts.
In the previous lesson, you were introduced to the format of a pseudocode structure and the different types of control structures. We will now re-examine some of those structures in a more detailed manner.
PSEUDOCODE REPRESENTATION
Below is an example of a pseudocode representation of an algorithm, where you will be able to observe the different parts of an algorithm.
EXAMPLE 1
Write a pseudocode algorithm to read the values of three numbers, find the product of the numbers and output the product.
Algorithm product_of_three_numbers {The algorithm header}
This algorithm finds the product of three numbers and outputs the product of the numbers.
Declare num1, num2, num3, product as integer {the declaration}. Start
Print “Please enter 3 numbers”
Read num1, num2, num3 Product num1 * num2 * num3
Print Product
Stop {The Terminator}
REPRESENTING ALGORITHMS USING FLOW CHARTS
A flow chart is a graphical representation of an algorithm. It uses special geometrical objects to designate the basic steps of a program, which are:
1. A parallelogram.
This is used to represent both input and output operations.
2. A rectangle.
This is used to represent a processing/assignment statement. 3. A diamond.
This is used to represent a decision (if-then-else and loops) and control structures.
4. An elliptical shape.
This is used to represent the terminal indicators, START or STOP 5. Directional arrows.
These are used to indicate the flow of the logic in the algorithm. Example 1: Finding the square of a number and outputting the square.
CONTROL STRUCTURES AND FLOW CHARTS
Before we look at the selection statement, below are examples of how the various control structures are depicted in a flow chart. Selection (Decision) SELECTION/ CONDITIONAL BRANCHING STATEMENTS
In our everyday lives, we make simple decisions and important decisions. These decisions that we make are normally based on a particular condition. For example, if school is keeping, you will go to school. If school is not keeping, you will stay home. The decision will be made based on the condition of whether or not ‘school is keeping’.
THE IF STATEMENT
The if-statement is a conditional statement. It causes execution of some statement to depend on the truthvalue of a certain condition, given as a Boolean expression. A Boolean expression is an expression that results in a value that is either true or false. For example, the expression 2<9 result would be true because the number 2 is indeed less than the number 9.
The ‘if statement’ structure begins with an ‘if’ and ends with the word ‘endif’. If the condition is true, then the statements immediately after ‘then’ are executed; if it is false, then the statements after ‘else’ are executed, provided that the ‘else’ clause is present. See examples below.
EXAMPLE 1
if I am feeling sick then I will visit the doctor endif
EXAMPLE 2
Condition
if I am feeling sick then I will visit the doctor else
I will attend school endif
EXAMPLE 3
Nested If structure – Several conditions are tested.
Read grade
If grade >= 85 then
Print “A”
Else
If grade >= 75 then
Print “B”
Else
If grade >= 65 then ElsePrint “C”
Print “D”
Endif
Let us look at a pseudocode algorithm that will show sequencing and selection, as well as the flow chart version of the algorithm.
EXAMPLE 3
Write a pseudocode algorithm to read two numbers and to print the value of num1 if it is greater than num2, else print num2.
Pseudocode version
Algorithm Comparison _of_two_values
Start
Declare A, B as datatype integer Read A, B if A > B then elsePrint A
Print B endif
Stop
FLOW CHART VERSION
We have come to the end of this lesson. See you next week when we will continue to look at psuedocode algorithms. Remember: If you fail to prepare, you prepare to fail.
Natalee A. Johnson Urquhart teaches at Ardenne High School. Send comments to kerry-ann. hepburn@gleanerjm.com