Lecture 02
Problem Solving with
Algorithms
Prepared by Ban Kar Weng (William)
PDLC
Program Development Life Cycle
What is PDLC ?
• A set of phases that are
1 Analysis used to develop a
program.
2 Design
• Phase 1 - 4 are repeated
3 Development until any problems
encountered during the
development are
4 Testing solved.
5 Documentation
What is PDLC ?
All about the What.
1 Analysis
• What the user wants from
the program?
2 Design
• What is the input and
output of the program?
3 Development
Tasks Involved
4 Testing • Gather user requirements
5 Documentation
What is PDLC ?
All about the How.
1 Analysis
• How to fulfill what the user
wants?
2 Design
• How the program is going
to achieve it?
3 Development
Tasks Involved
4 Testing • Algorithm Design (using
flowchart, pseudocode, or
5 Documentation other design tools.)
What is PDLC ?
All about Code.
1 Analysis
• Implement the algorithm
using programming
2 Design language(s)
3 Development Tasks Involved
• Translate the algorithm
4 Testing design into a programming
language
5 Documentation
What is PDLC ?
All about Correctness.
1 Analysis
• Test if the program does
what the user wants
2 Design correctly.
3 Development Tasks Involved
• Check program’s output
4 Testing and debug when needed.
• Repeat Phase 1 if
5 Documentation enhancements are
required.
What is PDLC ?
All about Recall.
1 Analysis
• Ensure that programmers
able to recall what the
2 Design codes does in the future.
3 Development Tasks Involved
• Generate written
4 Testing documentations.
• Improve source code
5 Documentation comments
Algorithm Representations
Algorithm Representations
How to represent or express algorithms?
Algorithm
Representation
Pseudocode Flowchart Program
Design Design Development
Pseudocode
Pseudocode
• Informal, high-level description of an algorithm.
• Not executable by computers.
• Allows precise description of algorithm without
worrying about the a programming language’s
syntax
Definition adopted from https://blog.usejournal.com/how-to-write-pseudocode-a-beginners-guide-29956242698 and C++ How to Program (8th Ed.) by Deitel and Deitel
Pseudocode: Examples
Example 1: Write a program that accepts an integer and outputs the
integer
Version 1:
INPUT an integer
OUTPUT the integer
Version 2:
INPUT X
OUTPUT X
Pseudocode: Examples
Example 2: Write a program calculating the sum of two integers
Version 1:
INPUT the first integer
INPUT the second integer
ADD the first integer and second integer, store result.
OUTPUT result
Version 2:
INPUT First
INPUT Second
Sum = First + Second
OUTPUT Sum
Flowchart
Flowchart
What do you understand
from the flowchart on
the right?
In what situation would I
“Watch TV” only once?
What happens if there is
truly no homework?
Flowchart
• A visual representation that shows the flow of the steps in
an algorithm.
• Consists of a flow of interconnected symbols.
• Not directly executable by computers.
• Easy to understand.
• Applied in computing and non-computing domain.
Flowchart
• How flowchart is designed depends on the intended
purpose.
§ For human, the instructions in the symbols can be general.
§ For machine, the instructions in each symbols must be convertible
to a programming language.
In the subsequent discussions, we assume that the flowchart
is intended for representing algorithms that will be
implemented in a machine.
Flowchart: Common Symbols (Part 1)
Diagram Name Description
Flow Connects symbols to show the algorithm flow.
Terminal Indicates the program’s start and end.
(i.e. start end )
Input/Output Used to input data into the system or output data.
Process A process / action to be performed.
(e.g. a calculation or the assignment of a value)
Flowchart: The Input / Output Symbol
• Input : Obtain data from an input device.
Variable
Get N
• Output : Display data to an output device
Value Variable
Show “Hello” Show N
NOTE: “Get” or “Show” may be replaced with any word as long as it is unambiguous.
Flowchart: The Process Symbol
Indicates a particular operation.
In computing, this represents a CPU operation. The common ones
include:
Variable
• Assignment operation y <- 1 y <- x
• Arithmetic operation y <- x + 1 y <- y * x
Flowchart: Examples
Example 3: Start
Draw a flowchart that
accepts an integer and
outputs the integer To be demonstrated
live
Flowchart: Examples
Example 3: Start
Draw a flowchart that
accepts an integer and
outputs the integer
Flowchart: Examples Start
Example 4:
Draw a flowchart that
calculates the sum of two
To be demonstrated
integers live
Flowchart: Examples Start
Example 4:
Draw a flowchart that
calculates the sum of two
integers.
Flowchart: Common Symbols (Part 2)
Diagram Name Description
Connector Connects separate flows in the same page.
Decision A yes/no question to be answered.
The program flow will then branch into one of
two paths, depending on the answer.
Flowchart: The Decision Symbol
Indicates a branching point. Lines coming out from the symbol
indicates different possible situations.
In computing, this symbol is often used evaluates Boolean expression,
so there are only two possible outcome: true or false (yes or no). Lines
coming out are connected by the Connector symbol.
Examples:
y=1
Output Output
“One” “Not One”
Start
Flowchart: Examples
Example 5:
Draw a flowchart that
accepts two integers and
To be demonstrated live
outputs the smallest.
Start
Flowchart: Examples
Example 5:
Draw a flowchart that
accepts two integers and
outputs the smallest.
Flowchart: Examples
Example 6:
Draw a flowchart that
accepts an integer.
Show “Zero” if the To be demonstrated live
integer is 0. Otherwise,
show “Positive” or
“Negative”, depending
on the sign of the
integer.
Flowchart: Examples
Example 6:
Draw a flowchart that
accepts an integer.
Show “Zero” if the
integer is 0. Otherwise,
show “Positive” or
“Negative”, depending
on the sign of the
integer.
Flowchart: Examples
Example 7:
Draw a flowchart that
shows “Hello”
infinitely. To be demonstrated live
Flowchart: Examples
Example 7:
Draw a flowchart that
shows “Hello”
infinitely.
Flowchart: Examples
Example 8:
Draw a flowchart that
shows “Hello” 100
times. To be demonstrated live
Flowchart: Examples
Example 8:
Draw a flowchart that
shows “Hello” 100
times.
Q&A
Acknowledgement
• This presentation has been designed using resources from
PoweredTemplate.com