Lect1 Algorithms and Flowchart
Lect1 Algorithms and Flowchart
◼ Introduction
What is an algorithm?
◼ An algorithm is “a finite set of precise
instructions for performing a computation or for
solving a problem”
A program is one type of algorithm
◼ All programs are algorithms
◼ Not all algorithms are programs!
Directions to somebody’s house is an algorithm
A recipe for cooking a cake is an algorithm
The steps to compute the cosine of 90° is an
algorithm
2
Algorithms
◼ A tool for solving a well-specified
computational problem
3
Algorithms Cont.
◼ 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.
5
ALGORITHMS
Algorithm
Implementation
Program
Compilation
Executable
(solution)
8
From Algorithms to Programs
Problem
Algorithm: A sequence
of instructions describing
how to do a task (or
process)
C++ Program
9
Practical Examples
◼ Internet and Networks
The need to access large amount of information with the
shortest time.
Problems of finding the best routs for the data to travel.
Algorithms for searching this large amount of data to quickly
find the pages on which particular information resides.
◼ Electronic Commerce
The ability of keeping the information (credit card numbers,
passwords, bank statements) private, safe, and secure.
Algorithms involves encryption/decryption techniques.
10
Hard problems
◼ We can identify the Efficiency of an algorithm
from its speed (how long does the algorithm
take to produce the result).
◼ Some problems have unknown efficient
solution.
◼ These problems are called NP-complete
problems.
◼ If we can show that the problem is NP-
complete, we can spend our time developing
11an efficient algorithm that gives a good, but
Components of an Algorithm
◼ Variables and values(data structure)
◼ Instructions
◼ Sequences
A series of instructions
◼ Procedures
A named sequence of instructions
we also use the following words to refer to a
“Procedure” :
◼ Sub-routine
◼ Module
◼ Function
12
Components of an Algorithm
Cont.
◼ Selections(if and if else..)
An instruction that decides which of two
possible sequences is executed
The decision is based on true/false condition
◼
14
Performance of this algorithm is a function of n
ALGORITHMS AND
FLOWCHARTS
Problem Analysis
ALGORITHMS AND FLOWCHARTS
◼ A typical programming task can be divided into
two phases:
◼ Problem solving phase
produce an ordered sequence of steps that describe
solution of problem
this sequence of steps is called an algorithm
◼ Implementation phase
implement the program in some programming
language
Steps in Problem Solving
◼ First produce a general algorithm (one can use
pseudocode)
◼ Refine the algorithm successively to get step by
step detailed algorithm that is very close to a
computer language.
◼ Pseudocode is an artificial and informal
language that helps programmers develop
algorithms. Pseudocode is very similar to
everyday English.
Pseudocode & Algorithm
◼ Example 1: Write an algorithm 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 average by summing and dividing
by 4
◼ if average is below 50
Print “FAIL”
else
Print “PASS”
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
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.
Information system flowcharts show how data flows from source
documents through the computer to final distribution to users.
Program flowcharts show the sequence of instructions in a single
program or subroutine. Different symbols are used to draw each
type of flowchart.
The Flowchart
A Flowchart
shows logic of an algorithm
emphasizes individual steps and their
interconnections
e.g. control flow from one action to the next
ALGORITHMS …
Start/ Stop
Start Stop
Input/ Output
Read Write
Algorithms …
Process box
❑ Use not more than two statements in each box.
Sum:= Sum + 1;
Count:= Count + 1;
ALGORITHMS …
Decision/selection box
ALGORITHMS …
Direction line (next step)
Start:
process A;
process B;
Process
Stop:
Repetition/iteration/looping
• There are times when a program needs to repeat certain
steps until told otherwise, or until a condition has been
met.
• This process is known as iteration.
• Iteration is also often referred to as looping, since the
program ‘loops’ back to an earlier line of code. Sections of
codes that are iterated are called loops.
• Iteration enables programmers to greatly simplify
a program.
• Instead of writing out the same lines of code again and
again, a programmer can write a section of code once, and
ask the program to execute it again and again until no
longer needed.
Example
Start:
DO WHILE:
Process;
Do exit action;
ENDWHILE:
Algorithms …
REPETITION
These are steps that must be performed more than once.
Start:
REPEAT:
Process
Until condition adjustment
UNTIL condition holds
Selection
◼ Selection is the second programming construct.
◼ In programming, there are occasions when a decision
needs to be made. Selection is the process of making a
decision.
◼ The result of the decision decides which path
the program will take next.
◼ For example, a program could tell a user whether they
are old enough to learn how to drive a car.
◼ If the user's age meets the required driving age, the
program would follow one path and execute one set
of instructions.
◼ Otherwise, it would follow a different path and execute a
different set of instructions.
Example
◼ age = int(input("How old are
you? ")
◼ if age > 16 then print("You are
old enough to drive a car!")
else
◼ print("Come back when you are
older!") endif
Algorithms …
DECISION/ SELECTION
IF condition holds
THEN perform Process
IF condition THEN
…. ELSE …
Example
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<5
0
PRINT PRINT
“PASS” “FAIL”
STOP
Example 2
◼ Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Pseudocode:
◼ Input the length in feet (Lft)
◼ Calculate the length in cm (Lcm) by
multiplying LFT with 30
◼ Print length in cm (LCM)
Example 2
Flowchart
Algorithm START
Print
Lcm
STOP
Example 3
Write an algorithm and draw a flowchart that
will read the two sides of a rectangle and
calculate its area.
Pseudocode
◼ Input the width (W) and Length (L) of a rectangle
◼ Calculate the area (A) by multiplying L with W
◼ Print A
Example 3
Algorithm START
Print
A
STOP
Flowchart – example 1
Begin
Calculate
Age = current year – birth date
Display
age
End
Read age
End
sum = 0
current_number = 1
NO
current_number <= 10? print sum
YES
End
sum = sum + current_number
current_number = current_number + 1