FKSDFJ
FKSDFJ
FKSDFJ
2
Cont
• Algorithm is general for all sorts of computers and
programming languages
• Same program written by different people give different
performance rate, all because of the algorithms behind the
logic of these programs
• algorithm is used to describe a problem solving method
3
Cont
• Algorithm is written independent of computer programs
• Algorithm is written in a language understandable to the
programmer and does not have strict syntax, statements, etc.
• The codes used for writing algorithm is called pseudo code or
algorithmic notations
4
Informal definition of an algorithm
used in a computer
Finding the largest integer
among five integers
Defining actions in FindLargest algorithm
FindLargest refined
Generalization of FindLargest
Time Space Tradeoff
• Time and space are the two major and primary variables measuring the
efficiency of the program
• Time is the amount of time used to complete a process
• Space is the amount in memory consumed by the process during its
processing
• The complexity of algorithm is directly measured by the amount of time
taken and memory used
• Normally an algorithm larger in size takes much time to process as
compared to the algorithm which is short in size
10
• So with the increase of the size of algorithm, processing time
also increases, and vice versa.
11
Algorithmic Notations – Step,exit
• An algorithm is executed statement by statement, one after
the other
• Each statement in algorithm has a step with or without step
number
12
Algorithmic Notations - Comment
• Comments are useful hints for the reader of algorithm
• These play no role in the processing of algorithm
• In a computer program they completely ignored by the
compiler
– 20 [initialize the number to zero] x=0
13
Algorithmic Notations - Variable
• Variable is a data item that receives its value during
processing
• A variable is given an appropriate name
– 100 X=45
– 110 MAX=50
Algorithmic Assignment
A variable or a data item is given a value
with the help of assignment (=)
30 counter=1
14
Algorithmic Notations - Input
• Input is the process that provides data to the algorithm
• The data is termed as input data
• We can use any of the following statements for input
– READ, INPUT, ACCEPT, etc
– INPUT Variable1
15
Algorithmic Notations - Output
• The process by which algorithm gives out data.
• The data is termed as output data
• We can use any of the following statement for output
– OUTPUT WRITE DISPLAY PRINT etc
– PRINT Name
16
Algorithmic Notations – Selection
• Selection logic employs a number of conditions which lead to a
selection of one out of several alternative modules
– 10 IF condition, then
– [module a]
– [end if]
– 40 IF condition, then
– [module a]
– Else
– [module b]
– [end if]
17
Algorithmic Notations - Iteration
• Iterative logic makes a statement or group of statements to
repeat until a certain condition or for some specified number
of times
• 10 Repeat while condition
• 30 [module]
• 40 [end loop]
18
Example Algorithm
Algorithm (sum of two numbers). X, Y, Z
are three numbers
1. READ X
2. READ Y
3. SET Z=X+Y
4. OUTPUT Z
5. EXIT
19
Class Activity
20
8.2
THREE CONSTRUCTS
Three constructs
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.
Flowchart Symbols
Basic
Name Symbol Use in Flowchart
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
Flowcharts for three constructs
Pseudocode for three constructs
Example 1
Solution
See Algorithm 8.1 on the next slide.
Algorithm 8.1: Average of two
AverageOfTwo
Input: Two numbers
1. Add the two numbers
2. Divide the result by 2
3. Return the result by step 2
End
Example 2
Solution
See Algorithm 8.2 on the next slide.
Algorithm 8.2: Pass/no pass Grade
Pass/NoPassGrade
Input: One number
1. if (the number is greater than or equal to 70)
then
1.1 Set the grade to “pass”
else
1.2 Set the grade to “nopass”
End if
2. Return the grade
End
Example 3
Solution
See Algorithm 8.3 on the next slide.
Algorithm 8.3: Letter grade
LetterGrade
Input: One number
1. if (the number is between 90 and 100, inclusive)
then
1.1 Set the grade to “A”
End if
2. if (the number is between 80 and 89, inclusive)
then
2.1 Set the grade to “B”
End if
Continues on the next slide
Algorithm 8.3: Letter grade (continued)
3. if (the number is between 70 and 79, inclusive)
then
3.1 Set the grade to “C”
End if
4. if (the number is between 60 and 69, inclusive)
then
4.1 Set the grade to “D”
End if
Solution
See Algorithm 8.5 on the next slide.
Algorithm 8.5: Find largest of 1000 numbers
FindLargest
Input: 1000 positive integers
1. Set Largest to 0
2. Set Counter to 0
3. while (Counter less than 1000)
3.1 if (the integer is greater than Largest)
then
3.1.1 Set Largest to the value of the integer
End if
3.2 Increment Counter
End while
4. Return Largest
End
8.5
SUBALGORITHMS
Figure 8-9
Concept of a subalgorithm
Algorithm 8.6: Find largest
FindLargest
Input: A list of positive integers
1. Set Largest to 0
2. while (more integers)
2.1 FindLarger
End while
3. Return Largest
End
Subalgorithm: Find larger
FindLarger
Input: Largest and current integer
1. if (the integer is greater than Largest)
then
1.1 Set Largest to the value of the integer
End if
End
Thank You…!