Chapter 2:
Algorithmic
Problem Solving
Dr. Mohd Suffian Sulaiman
Faculty of Computer & Mathematical Sciences
Universiti Teknologi MARA
2
Learning Outcome:
Algorithm
Algorithmin programming
Pseudocode and flowchart
Program Development Life Cycle (PDLC)
approach for problem solving.
3
Algorithm
Algorithm is a well-defined computational
procedure that takes some value, or set of values,
as input and produces some value, or set of
values, as output.
An algorithm is thus a sequence of computational
steps that transform the input into the output.
Algorithms are always unambiguous and are used
as specifications for performing calculations, data
processing, automated reasoning and other tasks.
The algorithm can be described in two ways as
follows:
Pseudocode
Flowchart
4 October 18, 2020
Algorithm in programming
Programming is the process of taking an algorithm
and encoding it into a notation, a programming
language, so that it can be executed by a
computer.
Although many programming languages and many
different types of computers exist, the important first
step is the need to have the solution. Without an
algorithm there can be no program.
Computer science is not the study of programming.
Programming, however, is an important part of what
a computer scientist does. Programming is often the
way that we create a representation for our
solutions.
5
Pseudocode
Text-based approach words with our own
language
Organized like an outline, with differing levels of
indentation to indicate the flow of actions within the
program.
Help to ‘think out’ before actual coding
Only consists of executable instruction
6
Flow Chart
Graphical representation
Use specific shapes/symbols indicate program
behaviors and decision types
Start / Stop program Process Preparation/looping
Decision Connector
Input/output Direction of flow
7
Designing algorithm on sequence
structure
8
Designing algorithm on sequence
structure - example
9
Designing algorithm on selection
structure
One way Two way
true
false
10
Designing algorithm on selection
structure
Multi way
11
Designing algorithm on selection
structure - example
12
Designing algorithm on repetition
structure
Pre-test Post-test
13
Designing algorithm on repetition
structure - example
14
Program Development Life Cycle
(PDLC)
Step 1:
Describing the problem Problem Analysis
Step 2:
Making a plan Design the algorithm
(Algorithm Development) (pseudocode and flowchart)
Step 3:
Coding Development /
(Speaking the language Implementation
of the computer)
Step 4:
Debugging Testing
(Getting Rid of Errors)
Step 5:
Finishing the project
Maintenance
(Documentation)
15
PDLC
Step 1 : Describing the problem
A process of identifying the output, processes
and input of a program.
Defining the problem
Identify :
1. Input (given data)
2. Process
- relation between input and output
- using formula
3. Output (the result)
Input Process Output
16
Describing The Problem
• Identify the following:
▫ Input – Prerequisite, data that exists at the
beginning of the problem
▫ Process – Set of steps that need to be done
in getting to the output.
▫ Output – The expected
data/information/result from the program
17
Example
Step 1: Problem Analysis
Write a program that can input 3 integer number from user.
Find the average for the number. Display all the numbers and
the average
Problem analysis Input : enter 3 numbers
Process : 1. total up 3 numbers
2. Divide the number by 3 (to find average)
Output : display 3 numbers and the average
18
PDLC
Step 2: Making a plan (design the algorithm)
Planning the solution to a problem
Plan on how to solve the problem by using:
Algorithm - Set of specific, sequential steps that
describe exactly what the program must do to
complete the tasks
Algorithm can be represented by using:
1. Flow Chart
2. Pseudo Code
19
Example
Step 2: Making a plan (design the
algorithm)
Algorithm is a precise step-by-step action
to perform overall task of the program.
1. Set num1, num2, num3, sum=0, avg=0 as integer
2. Read 3 integer number
3. Total up 3 integer number
sum = num1+num2+num3
4. Find the average
avg = sum / 3
5. Display 3 integer number and average
20
Example
Flowchart Pseudocode
start
Set num1, num2, num3,
sum=0, avg = 0 as int
START
Read num1, num2, num3 SET num1, num2, num3, sum=0,
avg=0 as int
INPUT num1,num2,num3
Calculate sum = num1 + num2 + num3 calculate sum = num1+num2+num3
calculate avg = sum / 3
Calculate avg = sum / 3
PRINT num1,num2,num3
PRINT avg
Display num1, num2, END
num3, avg
End
21
PDLC
Step 3: Coding / Development / Implementation
Express solution in a programming language
Translate
the logic from the flowchart or
pseudocode.
There are many programming languages :
BASIC, COBOL, Pascal, Fortran, C, C++, Java
etc
Each language has it’s own syntax (rules of
language)
22
Example
Step 3: Coding / implementation / development
Output of the program:
23
Step 4 - Debugging
Eliminating errors
2 types of errors:
Syntax Error – violation of rules of a
language in which computer is written
Forexample: leaving out semicolon (;) would
stop the entire program from working
Logic Error – error that occurs when a
programmer has used incorrect calculation
or left out a programming procedure.
For example : number divide by 0 (x÷0)
24
Step 5 – Finishing the Project
Integrate your code/program with others (if
necessary)
Test the whole complete system
Development Team
User
Documentation
Testing Result
User manual/User Guide
25
Exercise 1
Create a flowchart based on the pseudocode as
follows:
26
Exercise 2
Create a flowchart based on the pseudocode as
follows:
27
Exercise 3
Create a flowchart based on the pseudocode as
follows: