CP2 Module 2 - Algorithm and Pseudocode
CP2 Module 2 - Algorithm and Pseudocode
ALGORITHMS
COMPUTER PRORAMMING 2 2
LEARNING OUTCOMES:
COMPUTER PRORAMMING 2 3
Introduction to Algorithms
• Set of steps to accomplish a task or how
to arrive at a solution to a problem
• Created by Muhammad al-Khwarizmi
(Persian Mathematician)
• Often used for calculation, data
processing and programming
• Algorithms can be expressed in any
language
COMPUTER PRORAMMING 2 4
Introduction to Algorithms
• Algorithms for making things will often be divided into
sections
COMPUTER PRORAMMING 2 5
MAKING A PANCAKE
COMPUTER PRORAMMING 2 6
Introduction to Algorithm
COMPUTER PRORAMMING 2 7
TYPES OF ALGORITHMS
COMPUTER PRORAMMING 2 8
FORMS OF ALGORITHM
PSEUDOCODE FLOWCHART
COMPUTER PRORAMMING 2 9
What is a Pseudocode?
• A type of structured English that is used to specify an
algorithm.
• Fake code that specifies the steps required to accomplish
the task
• Cannot be compiled nor executed, and there are no real
formatting or syntax rules.
COMPUTER PRORAMMING 2 10
Purpose of Pseudocodes
To describe how an To design code in a
algorithm works group setting
(Reduced Complexity) (Increase flexibility)
To explain a computing
process to less-technical users
(Ease of Understanding)
COMPUTER PRORAMMING 2 11
How to Write Pseudocode Statements?
1. A computer can receive information
• Read (information from a file)
• Get (information from the keyboard)
2. A computer can put out information
• Write (information to a file)
• Display (information to the screen)
COMPUTER PRORAMMING 2 12
How to Write Pseudocode Statements?
4. A computer can assign a value to a piece of data
CASE 1 CASE 2
Give data an initial value Assign a value as a result of a process
Initialize or Set sum = 5 + 4;
CASE 3
Keep a piece of information for later use
Save or Store
COMPUTER PRORAMMING 2 13
How to Write Pseudocode Statements?
5. A computer can compare two piece of information and
select one of two alternative actions (conditionals)
IF condition THEN EXAMPLES:
some action
ELSE
alternative action
END IF
COMPUTER PRORAMMING 2 14
How to Write Pseudocode Statements?
6. A computer can repeat a group of actions (loop)
WHILE condition (is true) EXAMPLES:
some action
ENDWHILE
COMPUTER PRORAMMING 2 16
ALGORITHM TO A PSEUDOCODE
PROBLEM 1: Create a program that will calculate the pay of an
employee.
Algorithm
1. Determine working hours
2. Determine employee rate
3. Calculate the pay by multiplying
working hours and employee’s rate
4. Display employee’s pay
COMPUTER PRORAMMING 2 17
ALGORITHM TO A PSEUDOCODE
PROBLEM 2: Write a program that accepts two numbers from the
user and prints out the difference of those number.
Algorithm Pseudocode
1. User encodes the first number. BEGIN
2. User encodes the second number. Let num1 and num2 = 0
3. Find the difference between the Prompt user to input num1
first and second numbers. Prompt user to input num2
difference = num1 – num2
4. Show the difference.
Output difference
END
COMPUTER PRORAMMING 2 18
COMPUTER PRORAMMING 2 19
Resources:
• Padre, Nilo M. et al. (2016). Programming Concepts Logic Formulation, 1st ed.
Manila: Unlimited Books Library Services & Publishing, Inc.
• Why Algorithms are called Algorithms. BBC Ideas (2019). Retrieved from:
https://www.youtube.com/watch?v=oRkNaF0QvnI&feature=emb_logo
• What is an algorithm and why should you care? Khan Academy (2015).
Retrieved from: https://www.youtube.com/watch?v=CvSOaYi89B4&t=180s
• What is Pseudocode And How Do You Use It? CodeCademy (2019). Retrieved
from: https://www.youtube.com/watch?v=PwGA4Lm8zuE
COMPUTER PRORAMMING 2 20
COMPUTER PRORAMMING 2