0% found this document useful (0 votes)
322 views

Identify Ways of Representing Algorithms

The document discusses representing algorithms using pseudocode and flowcharts. It provides examples of pseudocode syntax for input/output and flowchart symbols for input, processing, decision, and flow of logic. The document also includes examples of algorithms represented in pseudocode and flowcharts, including an average calculation algorithm and a wages calculation algorithm.

Uploaded by

Radia Jesmin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
322 views

Identify Ways of Representing Algorithms

The document discusses representing algorithms using pseudocode and flowcharts. It provides examples of pseudocode syntax for input/output and flowchart symbols for input, processing, decision, and flow of logic. The document also includes examples of algorithms represented in pseudocode and flowcharts, including an average calculation algorithm and a wages calculation algorithm.

Uploaded by

Radia Jesmin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

IDENTIFY WAYS OF REPRESENTING ALGORITHMS.

OBJECTIVES:
Identify ways of representing algorithms:
Content
Representation of algorithms as Pseudocode
or Flowcharts; use of flow chart symbols:
input/output process,decision,directional
arrows. Use of: Read, Input, Store, Write,
Output, Display, If Then; If Then Else; For
loop; While loop (Questions which require
nested conditionals or nested loops will not
be asked)
THE ALGORITHMIC LANGUAGE
During the development of an algorithm,
the language gradually progresses from
English towards a notation that resembles
that of a programming language. An
intermediate notation called pseudocode
is commonly used to express algorithms.
Algorithms can also be expressed as
flowcharts.
A flowchart is a pictorial representation of
an algorithm.
Pseudocode
Acceptable words for Input/Output in
pseudocode:
INPUT OUTPUT
Get Display
Accept Print
Input Output
Read
Flowcharts
Flowcharts use special geometrical objects to
designate steps of a program, which are : input,
processing and output. A parallelogram is used
to represent the input operation as well as the
output operation, and a rectangle is used to
represent a processing/assignment statement.
A diamond is used to represent a decision
(If Then Else) structure . An elliptical shape is
used to represent the terminal indicators,
START OR STOP. Directional arrows is used to
indicate the flow of the logic in the algorithm.
Flowcharts Basic Shapes
The four basic shapes are:

Input/Output Processing/Assignment

Decision Start/Stop
Sequence
Below are examples of how the various control
structures are depicted in a flowchart:
Sequence

A
Do A

B
Do B
Selection (The If Then Else Construct)

If C is true then
No Yes
C do E
Else
do D

D E
Loop
Loop (Repetition)

Yes
F G

No
While F is true
do G
Pseudocode Version of the Average Algorithm

Read Num , Num , Num


Average = (Num Num Num
Print Average
Stop.
Flowchart version of the Average Algorithm
Start

Read Num ,
Num ,
Num

Aver = (Num +Num +Num

Print Aver

Stop
Problem 10

Design an algorithm that accepts two


values, the hourly rate and the number of
hours worked by an employee. If the
number of hours exceeds 40 then the
excess hours should be paid at an overtime
rate of twice the hourly rate. Calculate the
wages (including overtime, if any) due to
the employee.
Solution

The first step is to define the solution as follows:

INPUT PROCESSING OUTPUT


Rate, 1) Calculate overtime pay Wages
HoursWorked if any
2) Calculate wages
Solution Step 2
Possible Pseudocode version of the algorithm:
This algorithm calculates the wages due to an employee.
Read Rate, HoursWorked
BasicPay = Rate *
OvertimeHours = HoursWorked –
If (OvertimeHours > ) then
Wages = BasicPay + (OvertimeHours * * Rate)
Else
Wages = Rate * HoursWorked
EndIf
Print Wages
Stop.
Start
Flowchart
Version of
Read Rate,
the Wages Hours worked
Problem
BasicPay = HoursWorked *40

OvertimeHours= HoursWorked - 40

Yes Overtime No
Wages = BasicPay + Wages = HoursWorked * Rate
(OvertimeHours* 2 * Rate) Hours> 0

Print Wages

Stop
Comparison/Selection/Decision Statements
It sometimes becomes necessary when processing data to
compare the value of a variable to the value of another variable
or to compare the value of a variable to that of a constant. The
following Relational Operators are used to make such
comparisons:
Relational Operator Meaning
= Equal to
<> Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Comparison/Selection/Decision Statements

A Condition is an expression that when evaluated


gives either a True or a False. This expression is
called a Boolean expression. These conditions use the
relational operators between two variables or between
a variable and a constant.

Examples:
Boys in Class > = 35
NumberOfBoys <> NumberOfGirls
Boolean Operators
When selection or decisions is based upon one or
more expressions being True or False, it is
possible to combine the expressions together
using Boolean Operators AND or OR.

If the AND operator is used both conditions must


be met in order for the total expression to be true.

If the OR expression is used either condition


must be met in order for the total expression to be
true.
Example of the AND Operator
A club plays cricket only on Sundays and only if it is not
raining. Write an algorithm to accept the day and weather
and print “Game on” if it is a suitable day for playing.
What happens with each of the two operators.
Solution:
Input Day, Weather
If (Day=“Sunday”) And (Weather=“No Rain”) Then
Print “Game ON”
EndIF
Stop
If either condition is not met “Game On” would not be
printed
Example of the OR Operator
If we had used the or operator instead then the algorithm
would read as follows:

Input Day, Weather


If (Day=“Sunday”) OR (Weather=“No Rain”) Then
Print “Game ON”
EndIF
Stop

If either condition is met “Game On” would be printed.


That would mean that “Game On” would be printed If it
is Sunday or on any other day of the week once there is no
rain.
Question 1

Write an algorithm to enter the base and


height of a triangle and find and print the
area. Write a pseudocode version and a
flowchart version of the solution.
Pseudocode version

Algorithm AreaTriangle
This algorithm accepts values for the Base and
Height of a triangle and Prints the area.

Read Base, Height


Area = (Base * Height)/2
Print Area
Stop.
Flowchart Version
Start

Read Base, Height

Area = (Base * Height)/2

Print Area

Stop
Question 2

Write an algorithm to enter a number,


double the number and output the result.
Write a pseudocode version and a
flowchart version of the solution.
Pseudocode version

Algorithm Double
This algorithm accepts a number and doubles it and
outputs the result.

Read Num
Double = Num * 2
Print Double
Stop.
Flowchart Version
Start

Read Num

Double = Num * 2

Print Double

Stop
Question 3

Persons under 18 are not allowed in at


‘Shutters Night Club’.Write an algorithm to
read a person’s age and if it is under 18 the
algorithm should output ‘ Underage
person’. If the person is 18 years or over the
algorithm should output “Person allowed”.
Write a pseudocode version and a
flowchart version of the solution.
Pseudocode version
Algorithm Shutters
This algorithm checks a person’s age and check to
see if they are allowed in the night club.

If (Age < 18) Then


Print “Underage Person”
Else
Print “Person Allowed”
EndIf
Stop.
Flowchart Version
Start

Read Age

No Yes
If (Age < 18) Then

Print “ Person Allowed Print “Underage Person”

Stop
Question 4

Write a program to input a number N. If


the number is greater than 50 subtract 10
from the number, otherwise multiply the
number by 2 and add 5. Print the number
and the result.
Pseudocode version
Algorithm Number N
This algorithm accepts a number N manipulates it
and prints the result.
Read N
If (N > 50) Then
N = N-10
Else
N=N*2
N=N+5
EndIf
Print N
Stop.
Flowchart Version
Start

Read N

No Yes
If (N > 50) Then

N=N*2
N = N - 10
N=N+5

Print N

Stop

You might also like