Algorithms and Flowcharts: Mr. Angelito M. Caraan
Algorithms and Flowcharts: Mr. Angelito M. Caraan
FLOWCHARTS
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
Algorithm:
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:
Algorithm START
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 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
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
processing.