ALGORITHM
Algorithm
step-by-step procedure that allows a computer to
solve a problem.
If algorithm written in English like sentence then
it is called “Pseudocode”
Program design process to 2 phases
Problem solving phase
produce an ordered sequence of steps that describe
solution of problem
Implementation phase
implement the program in some programming language
Example: Algorithm
Find the average of 3 numbers
Step 1: Start
Step 2: input 3 numbers a, b, c
Step 3: Compute sum = a+b+c
Step 4: Compute avg = sum/3
Step 5: Print avg value
Step 6: Stop
Advantage Disadvantage
It provides the core In large algorithm, the flow
solution to given problem of program controls
becomes difficult to track
It eases identification and
removal of logical error in Lack visual representation
program of programming constructs
like flowchart
It facilitates algorithm
analysis to find out the
more efficient solution to
a given problem
PSEUDOCODE
Pseudocode is an English-like representation of
the same thing.
Pseudo is a prefix that means “false,” and to
code a program means to put it in a programming
language; therefore, pseudocode simply means
“false code,” or sentences that appear to have
been written in a computer programming
language but don’t necessarily follow all the
syntax rules of any specific language
Example: Pseudocode
Find the average of 3 numbers
Start
Input 3 numbers: number1(a),
number2(b), number3(c)
Calculate their average by adding the
three numbers and divide by 3
Print the average number
Stop
Example: Algorithm and Pseudocode
Write an algorithm and
pseudocode to convert the
length in feet to centimeter.
1ft = 30.48cm
Pseudocode:
Input the length in feet (Lft)
Calculate the length in cm (Lcm) by
multiplying length in feet (Lft) with 30.48
Print length in cm (Lcm)
Algorithm:
Step 1: Input Lft
Step 2: Lcm <- Lft x 30.48
Step 3: Print Lcm
FLOWCHART
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.
Is type of diagram that represents an algorithm or process
Basic Symbols
• Terminal symbols
- Start / Stop symbols
- Shaped like a racetrack
- Also called lozenge or capsule
• Input and Output symbols
- Indicates input and output operation
- Parallelogram
• Processing symbol
- Processing statement such as arithmetic
- Rectangle
Flowchart Symbols Basic
• Display symbols
- Represents output statements
- Hybrid
• Decision symbols
- Denotes a decision (or branch) to be made
- Program should continue along one of two
routes (e.g. If/Then/Else)
- Diamond
• Flow lines symbol
- Lines and Arrows that connect steps
• On-page connector symbol
- Used to connect flowlines
START
Step 1: Start input
Step 2: Input 3 numbers a, b, c a, b, c
Step 3: Compute sum = a+b+c
Step 4: Compute avg = sum/3 sum = a+b+c
Step 5: Print avg value
Step 6: Stop avg = sum/3
output
avg
STOP
Write an algorithm,
Pseudocode and flowchart
that will read the two sides
of a rectangle and calculate
its area.
DECISION STRUCTURES
The expression A>B is a logical expression
it describes a condition we want to test
if A>B is true (if A is greater than B) we take the
action on left
print the value of A
if A>B is false (if A is not greater than B) we take
the action on right
print the value of B
IF–THEN–ELSE STRUCTURE
The structure is as follows
If condition then
true alternative
else
false alternative
endif
DECISION STRUCTURES
IF–THEN–ELSE STRUCTURE
Relational Operators
Pseudocode & Algorithm
Example:
Write an algorithm and pseudocode
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 GRADE by adding all four marks
and divide them by 4
if GRADE is below 50
Print “FAIL”
else
Print “PASS”
endif
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
Example 1
Example:
Write an algorithm that reads
two values, determines the
largest value and prints the
largest value with an
identifying message.
Example
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
Example 4
NESTED IFS
One of the alternatives within an IF–THEN–ELSE
statement
may involve further IF–THEN–ELSE statement
Example:
Write an algorithm that
reads three numbers and
prints the value of the
largest number.
Example: