Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Algorithm:

Pseudocode
with Sir Beets
Raise of Hands
Icebreaker
Pineapples should go with pizza.

Pineapples can go with pizza.

Pineapples never go with pizza.


– An algorithm is a step-by-
Representations
step procedure for solving a
Pseudocode
problem in a limited number
of steps – English-like statements
Flowchart
– Applications in mathematics
What is an – Diagram that shows the
logical sequence of the
– The instructions for each step
Algorithm? are precise
steps
Program
– Many algorithms involve – Sequence of instructions
repeating the same steps written based on the rules
several times and can be of a programming language
(e.g. Pascal, C and Java)
carried out by a computer
Pseudocode
Pseudocode
Pseudocode
Flowchart
Code
Let’s start simple
1. English-like statements
2. Using Variables
3. Using Variables and Operators
Compute and display the average of two numbers.
Steps:
1. Obtain the first number
Problem 1 2. Obtain the second number
3. Add the two numbers to get the sum
Sequential 4. Divide the sum by two to get the quotient
5. Display the quotient
Initial algorithm Pseudocode using variables
1. Obtain the first number 1. Input the value of X
2. Obtain the second number 2. Input the value of Y
3. Add the two numbers to get 3. S gets the sum of X and Y
Pseudocode of the sum
4. Ave gets the result of S
Problem 1 4. Divide the sum by two to
get the quotient
divided by 2
5. Display Ave
5. Display the quotient

X Y S Ave
Variables are holders of values
5 7 12 6
3.14 88
6 8 14 7 “Juan Carlos”
9 4 13 6 ’C’
Integer Division Float Division
operands are integers and the operands are real numbers and
quotient is an integer the quotient is a real number

Integer and Examples: Examples:


Float Division – 7 / 2 =3 – 7.0 / 2.0 = 3.5
– 8 / 2 =4 – 8.0 / 2.0 = 4.0
– 1 / 2 =0 – 1.0 / 2. 0 = 0.5
Determine if an inputted number is EVEN or ODD and
display “Even” or “Odd”
Initial Algorithm Pseudocode using variables
1. Obtain the number 1. Input the value of X
2. If (the number is 2. If (X mod 2 is equal to 0)
divisible by 2) 2.1 Then display “Even”
2.1 Then display “Even” 2.2 Else display “ODD”
Problem 2 2.2 Else display “Odd”
Conditional
Textual outputs or displays should have quotation marks.
Without quotation marks would mean the use of a variable.
Is the average of two numbers odd or even? Display the answer.
1. Input the value of X

Problem 2. Input the value of Y

1 and 2 3. S gets the sum of X and Y


4. Ave gets the result of S divided by 2
5. If (Ave mod 2 is equal to 0)
6.1 Then display “The average is Even”
6.2 Else display “The average is Odd”
Basic Operations and Symbols
Basic Mathematical Operations Operator Symbol Example
Addition (Sum) Plus ( + ) A+B
Subtraction (Difference) Dash or Hyphen ( - ) A-B
Multiplication (Product) Asterisk ( * ) A*B
Division (Quotient) Slash ( / ) A/ B
Modulo (Remainder) Percent ( % ) A%B

Mathematical operations are used for clarity and precision


Assignment and Relational Operations
Assignment Operation Operator Symbol Example
Assignment Equals ( = ) A=3

Relational Operations Operator Symbol Example for A = 3; B = 7;


Less than < A<B TRUE
Greater than > A>B FALSE
Equality == A == B FALSE
Not equal != A != B TRUE
Less than or equal to <= A <= B TRUE
Greater than or equal to >= A >= B FALSE
Using
Using Variables Variables and Operators
1. Input the value of X 1. Input X
2. Input the value of Y 2. Input Y
3. S gets the sum X and Y 3. S = X +Y
Problem 4. Ave gets the result of S divided by 4. Ave = S / 2
1 and 2 2
5. If ( Ave % 2 == 0)
5. If (Ave mod 2 is equal to 0) 6.1 Then display “The average is Even”
6.1 Then display “The average is Even” 6.2 Else display “The average is Odd”
6.2 Else display “The average is Odd”
1. Composition (Sequential)
Sequence of statements are executed in order of appearance
Problem 1: Finding Average

2. Alternation (Conditional)
Three Types of Two sequences of statements may form alternatives so that only 1 of
the alternative is executed
Statements Problem 2: Even or Odd

3. Iteration
A sequence of statements may be executed repeatedly, zero or more
times (zero meaning execution may be omitted altogether)
Repeating Statements
Version 1
1. Sing “Amen” Sing Amen 3 times
2. Sing “Amen”
3. Sing “Amen”
1. Y=1
Version 2
Repeating 2. While ( Y <= 3 )
1. Sing “Amen”
Statements 2. Repeat Step 1 two more
2.1 Sing “Amen”
2.2 Y = Y + 1
Iteration times
Version 3
1. Repeat 3 times
1.1 Sing “Amen”
Display the all positive even integers from N to 0.
1. Input N
2. Y = N
Practice 3. While ( Y >= 0 )
3.1 If ( Y % 2 == 0 )
3.1.1 Then display Y
3.2 Y = Y - 1

12 10 8 6 4 2 0
Look for things that involves mathematical
computation
1. Receipt
2. Price tags with discount
On Your Own 3. GCF and LCM

How do I know if what I am doing is right?


ü Write the results for each line.
读万卷书不如行万里路
Chinese “Travelling thousands of miles is better than reading
Proverb thousands of books.”

You might also like