ALGORITHMS AND
FLOWCHARTS
Mr. Angelito M. Caraan
Instructor
School of Computer Studies
ALGORITHMS AND FLOWCHARTS
A typical programming task can be divided into
two phases:
Problem solving phase
produce an ordered sequence of steps that describe
solution of problem
this sequence of steps is called an algorithm
Implementation phase
implement the program in some programming
language
Steps in Problem Solving
First produce a general algorithm (one can use
pseudocode)
Refine the algorithm successively to get step by
step detailed algorithm that is very close to a
computer language.
Pseudocode is an artificial and informal
language that helps programmers develop
algorithms. Pseudocode is very similar to
everyday English.
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.
Pseudocode & Algorithm
Pseudocode:
Input a set of 4 marks
Calculate their average by summing and dividing
by 4
if average is below 50
Print “FAIL”
else
Print “PASS”
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
Print “FAIL”
else
Print “PASS”
endif
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.
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
Flowchart Symbols
Basic
Flowchart Symbols
Basic Control Structures
Sequence – process is executed from one
to another in a straightforward manner.
Example
Q: Design a flowchart that will accept and display a number. Write its equivalent
algorithms.
Algorithm:
Step 1. Read in the value of N.
Step 2. Print the value of N.
Basic Control Structures
Selection (if – then – else) – a choice is
provided between two alternatives.
DECISION STRUCTURES
The expression A>B is a logical expression
it describes a condition we want to test
if A>B is true (if A is greater than B) we take
the action on left
print the value of A
if A>B is false (if A is not greater than B) we
take the action on right
print the value of B
IF–THEN–ELSE STRUCTURE
The structure is as follows
If condition then
true alternative
else
false alternative
endif
Example
Q: Draw a flowchart that will input values for A and B. Compare two values
inputted and print which value is higher including the mark “Higher”. Write its
equivalent algorithm.
Algorithm:
Step 1. Read in the value of A
and B.
Step 2. Test if A is greater than B.
Step 3. If A is greater than B, A is
higher. However, if A is less than
B, B is higher.
Step 4. Print the number and the
remark “Higher”.
NESTED IFS
One of the alternatives within an IF–
THEN–ELSE statement
may involve further IF–THEN–ELSE
statement
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
Basic Control Structures
Repetition (Looping) – this structure
provides for the repetitive execution of an
operation or routine while the condition is
true.
Example
Q: Construct a flowchart that will count from 1 to 10 and print each number
counted using the do-while-repetition structure. Write its equivalent algorithm.
Algorithm:
Step 1. Initialize the value of C to 0.
Step 2. Test if C is less than 10.
Step 3. If C is less than 10, add 1 to the
value of C, print the value then
go back to Step 2. However, if
C is greater than 10, stop
processing.
Problem 1
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
Print length in cm (LCM)
A: Problem 1
Flowchart
Algorithm START
Step 1: Input Lft Input
Lft
Step 2: Lcm Lft x 30
Lcm Lft x 30
Step 3: Print Lcm
Print
Lft
END
Problem 2
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
Print A
A: Problem 2
Algorithm START
Step 1: Input W,L Input
W, L
Step 2: A L x W
ALxW
Step 3: Print A
Print
A
END
Problem 3
Draw a flowchart that reads two values, determines the
largest value and prints 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: Print “The largest value is”, MAX
A: Problem 3
START
Input
VALUE1,VALUE2
Y is
N
VALUE1>VALUE2
MAX VALUE1 MAX VALUE2
Print
“The largest
value is”, MAX
END
Problem 4
Write an algorithm that reads three
numbers and prints the value of the largest
number.
A: Problem 4
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX N1 [N1>N2, N1>N3]
else
MAX N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX N2 [N2>N1, N2>N3]
else
MAX N3 [N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX
Problem 5
Construct a flowchart that will add numbers
from 1 to 10, print each number added using
the do-while-repetition structure. Write its
equivalent algorithm.
A: Problem 5
Algorithm: Start
Step 1. Initialize the value of
C to 0, and Sum to 0. C=0
Step 2. Test if C is less than Sum = 0
10.
Step 3. If C is less than 10, F
C < 10 End
add 1 to the value of
C. Add the value of C T
to the value of Sum.
C=C+1
print the value then go
back to Step 2. Sum = Sum + C
However, if C is
greater than 10, stop Print Sum
processing.