Beruflich Dokumente
Kultur Dokumente
Introduction
Algorithms are the building blocks of any computer
program
They are a set of rules to be followed in order to
solve problems using computers
What is an Algorithm?
An algorithm is a part of the plan for the computer
program;
An algorithm is an effective procedure for solving a
problem in a finite number of steps.
Characteristics of an
Algorithm
Well-ordered: The steps are in a clear order
Unambiguous: The operations described are
understood by a computing agent without further
simplification
Effectively computable: The computing agent can
actually carry out the operation
What is Algorithm?
How to express an
Algorithm?
Input/Output
Input: Get var1, var2,
Output: Display var1, var2,
Selection
Single-Selection IF
1. IF condition THEN (IF condition is true, then do
subordinate statement 1, etc. If condition is false, then
skip statements)
1.1 statement 1
1.2 etc.
Pseudocode Language
Constructs
Double-Selection IF
2. IF condition THEN (IF condition is true, then do
subordinate statement 1, etc. If condition is false, then
skip statements and execute statements under ELSE)
2.1 statement 1
2.2 etc.
4. SWITCH expression TO
4.1
4.2
4.3
4.4
case 1: action1
case 2: action2
etc.
default: actionx
Pseudocode Language
Constructs
Repetition
5. WHILE condition (while condition is true, then do
subordinate statements)
5.1 statement 1
5.2 etc.
7. WHILE condition
Pseudocode Example
Express an algorithm to get two numbers from the
user (dividend and divisor), testing to make sure that
the divisor number is not zero, and displaying their
quotient using pseudocode
1.
2.
3.
4.
5. ENDIF
6. Display dividend and divisor
7. Calculate quotient as dividend/divisor
Example Pseudocode
Start
Read A, B
Start - Terminal
Calculate C = A*B Read A, B Input
Display C
Calculate C = A*B Action
Stop
Display C - Output
Stop - Terminal
Start
Use variables A,B and C
Display write two numbers
Read A, B
Calculate C = A*B
Display multiplication of numbers ,
C
Stop
Flowcharts
A graphical tool that diagrammatically depicts the
steps and structure of an algorithm or program
General rules for flowcharts
All symbols of the flowchart are connected by flow lines
(note arrows, not lines)
Flowlines enter the top of the symbol and exit out the
bottom, except for the Decision symbol, which can have
flow lines exiting from the bottom or the sides
Flowcharts are drawn so flow generally goes from top to
bottom
The beginning and the end of the flowchart is indicated
using the Terminal symbol
Flowchart Symbols
Example Flowchart
Structured Programming
Any computer program can be written with just three
structures: sequences, decisions, and loops.
Sequences: one command is executed after previous one.
Decisions (selections): statement(s) is (are) executed if
certain condition gives TRUE or FALSE value.
Loops (repetition): statement(s) is (are) executed repeatedly
until certain condition gives TRUE or FALSE value.
Sequence
Decisions (selections)
If
If else
Switch
Decisions (selections)
Nested If
Simply, if structure in if structure
Example - 3: Both final and average grades must be
greater than or equals to 35 for curve calculation in
BEU.
if (final >= 35) then
{
if (average >= 35) then
execute curve calculation commands
endif
}
else
Print FF grade
endif
Loops (repetition)
While
Do while
For
Loops (repetition)
Example
Write an algorithm to determine a students
grade and indicate whether he is successful or
not.
The average grade is calculated as the average of
internal marks and endsem marks.
Student will be successful if his average grade is
greater or equals to 50.
Pseudo Code
Start
Use variables internal, endsem and average
Input internal and endsem
Calculate the average by summing internal and
ensem and dividing by 2
if average is below 50
Print FAIL
else
Print SUCCESS
Stop
Algorithm
Step 1: Input internal and endsem
Step 2: average = (internal + endsem)/2
Step 3: if (average < 50) then
Print FAIL
else
Print SUCCESS
endif
Flowchart
Pseudocode vs Flowcharts
Pseudocode
Artificial and Informal
language
Helps programmers to
plan an algorithm
Similar to everyday
English
Not an actual
programming
language
Flowcharts
A graphical way of
writing pseudo code
Rounded rectangle
terminal
Parallelogram input /
output
Rectangle actions
Diamonds decision /
conditional
Circles connector
END