Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

Algorithms & Flow Chart

Lecture 5
Introduction to Computing
bilal mir
Algorithm
 Step by step procedure to solve a problem.
 Step by step set of instructions for solving an
input (problem) to generate an output.
 A precise statement to solve problems using
computer.
 Two ways to design an algorithm:
 Pseudo Code
 Combination of natural languages and programming
languages.
 More reliable than flow chart.
 Flow chart
02/27/23
Introduction to Computing bilal mir
Properties of Algorithm
 Simple
 Efficient
 Retain less memory
 Not too much time consuming
 Accurate
 Complete
 Finite

02/27/23 Introduction to Computingbilal mir


Algorithm Efficiency
 Speed: method that take the least time.
 Space: method that uses the least memory.
 Code: method that is shortest to describe.
 Speed is now the most important factor.
 Time Complexity: The amount of time required to
execute an algorithm.
 Space Complexity: The amount of memory
required to execute an algorithm.

02/27/23 Introduction to Computingbilal mir


Pseudo code & 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
Subjects.
Pseudo code:
 Input a set of 4 subjects
 Calculate their average by summing & dividing by 4
 if average is below 50
Print “FAIL”
else
Print “PASS”

02/27/23 Introduction to Computingbilal mir


Pseudo code & 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”
 Step 4: end

02/27/23 Introduction to Computingbilal mir


Flow Chart
 A graphical or pictorial representation of an
algorithm or sequence of operations in a program.
 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.
 Different symbols are used to draw each type of
flowchart.

02/27/23 Introduction to Computingbilal mir


Flow Chart
A Flow Chart
 Shows logic of an algorithm
 Emphasizes individual steps and their
interconnections
 e.g. control flow from one action to the next

02/27/23 Introduction to Computingbilal mir


Flow Chart
Basic Symbols
Name Symbol Use in Flowchart

Oval Denotes the beginning or end of the program

Parallelogram Denotes an input operation

Rectangle Denotes a process to be carried out


e.g. addition, subtraction, division etc.

Diamond Denotes a decision (or branch) to be made.


The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)

Hybrid Denotes an output operation

Introduction to Computingbilal
Denotes mir
the direction of logic flow in the program
Flow line
Example
START
Step 1: Input M1,M2,M3,M4
Input
Step 2: GRADE  (M1+M2+M3+M4)/4
M1,M2,M3,M4 Step 3: if (GRADE <50) then
Print “FAIL”
GRADE(M1+M2+M3+M4)/4 else
Print “PASS”
Step 4: end
N Y
IS
GRADE<50

PRINT PRINT
“PASS” “FAIL”

STOP

02/27/23 Introduction to Computingbilal mir


Example 2
 Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Pseudo Code:
 Input the length in feet (Lft)
 Calculate the length in cm (Lcm) by
multiplying LFT with 30
 Print length in cm (LCM)

02/27/23 Introduction to Computingbilal mir


Example 2
Algorithm
Flowchart
 Step 1: Input Lft
START
 Step 2: Lcm  Lft x 30
 Step 3: Print Lcm Input
Lft

Lcm  Lft x 30

Print
Lcm

STOP

02/27/23 Introduction to Computingbilal mir


Example 3
 Write an algorithm and draw a flowchart that
will read the two sides of a rectangle and
calculate its area.
Pseudo Code:
 Input the width (W) and Length (L) of a
rectangle
 Calculate the area (A) by multiplying L with
W
 Print A

02/27/23 Introduction to Computingbilal mir


Example 3

Algorithm START

 Step 1: Input W,L


Input
 Step 2: AL x W W, L

 Step 3: Print A
ALxW

Print
A

STOP

02/27/23 Introduction to Computingbilal mir


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

02/27/23 Introduction to Computingbilal mir


Decision Structures

Y is
N
A>B

Print A Print B

02/27/23 Introduction to Computingbilal mir


If–Then–Else Structure
 The structure is as follows
If condition then
true alternative
else
false alternative
end

02/27/23 Introduction to Computingbilal mir


If–Then–Else Structure
 The algorithm for the flowchart is as follows:

If A>B then
print A Y is
N
A>B
else
print B
Print A Print B
end

02/27/23 Introduction to Computingbilal mir


Relational Operators
Relational Operators

Operator Description
> Greater than

< Less than

= Equal to

 Greater than or equal to

 Less than or equal to

 Not equal to

02/27/23 Introduction to Computingbilal mir

You might also like