PSEUDO CODE CONTINUED
PSEUDO-CODE: DECISION MAKING
If-then
General form:
if (condition is met) then
statement(s)
Example:
if temperature < 0 then
wear a jacket
If-then-else
General form:
if (condition is met) then
statement(s)
else 2
statements(s)
PSEUDO-CODE: DECISION MAKING (2)
Example:
if (at work) then
Dress formally
else
Dress casually
3
PSEUDOCODE & ALGORITHM
Example 1: Write an algorithm to determine a
student’s final grade and indicate whether it is
passing or failing. The final grade is calculated as
the average of four marks.
4
PSEUDOCODE & ALGORITHM
Pseudocode:
Input a set of 4 marks
Calculate their average by summing and
dividing by 4
if average is below 50
output “FAIL”
else
output “PASS”
5
PSEUDOCODE & ALGORITHM
Detailed Algorithm
Step 1: Input M1,M2,M3,M4
Step 2: GRADE = (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
output “FAIL”
else
output “PASS”
endif
6
FLOW CHARTS
7 Part Three
THE FLOWCHART
(Dictionary) A schematic representation of a
sequence of operations, as in a manufacturing
process or computer program.
(Technical) A graphical representation of the
sequence of operations in an information system
or program. Information system flowcharts show
how data flows from source documents through
the computer to final distribution to users.
Program flowcharts show the sequence of
instructions in a single program or subroutine.
Different symbols are used to draw each type of
flowchart. 8
THE FLOWCHART
A Flowchart
shows logic of an algorithm
emphasizes individual steps and their
interconnections
e.g. control flow from one action to the next
9
UNDERSTANDING THE ALGORITHM
Possibly the simplest and easiest method to understand the steps in an
algorithm, is by using the flowchart method. This algorithm is composed
of block symbols to represent each step in the solution process as well as
the directed paths of each step.
The most common block symbols are:
Symbol Representation
Start/Stop
Process
Input
Output 10
Symbol Representation
Decision
Connector
Flow Direction
11
EXAMPLE
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE = (M1+M2+M3+M4)/4
Input
M1,M2,M3,M
Step 3: if (GRADE <50) then
4 output “FAIL”
else
GRADE(M1+M2+M3+M4)/
4
output “PASS”
endif
N IS Y
GRADE<
50
output output
“PASS” “FAIL”
12
STOP
EXAMPLE 2
Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Pseudocode:
Input the length in feet (Lft)
Calculate the length in cm (Lcm) by multiplying
Lft with 30
output length in cm (Lcm)
13
EXAMPLE 2
Algorithm Flowchart
Step 1: Input Lft
START
Step 2: Lcm = Lft x 30
Step 3: output Lcm Input
Lft
Lcm Lft x 30
output
Lcm
STOP
14
EXAMPLE 3
Write an algorithm and draw a
flowchart that will read the two sides
of a rectangle and calculate its area.
Pseudocode
Input the width (W) and Length (L) of a
rectangle
Calculate the area (A) by multiplying L
with W
output A
15
EXAMPLE 3
Algorithm START
Step 1: Input W,L
Step 2: A=L x W Input
W, L
Step 3: output A
A=LxW
output
A
STOP 16
IF–THEN–ELSE STRUCTURE
The structure is as follows
If condition then
true alternative
else
false alternative
endif
17
DECISION STRUCTURES
Y N
is
A>B
output output
A B
18
IF–THEN–ELSE STRUCTURE
The algorithm for the flowchart is as follows:
If A>B then
output A
else
output B Y N
is
endif A>B
output output
A B
19
RELATIONAL OPERATORS
Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
Greater than or equal to
Less than or equal to
Not equal to
20
EXAMPLE 4
Write an algorithm that reads two values,
determines the largest value and outputs the
largest value with an identifying message.
ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX VALUE1
else
MAX VALUE2
endif
Step 3: output “The largest value is”, MAX
21
EXAMPLE 4
START
Input
VALUE1,VALUE
2
Y is N
VALUE1>VALUE2
MAX = VALUE1 MAX = VALUE2
output
“The largest value is”,
MAX
22
STOP
23
حاصل ضرب االعداد من
.10-1
حاصل ضرب االعداد
الزوجية من .10-1
حاصل ضرب االعداد 24