Академический Документы
Профессиональный Документы
Культура Документы
Chapter 2:
Preliminaries
Imran Ali Memon
IT Department
Contents
2.1 Algorithm
2.2 Algorithmic Notations
Steps Control Exit, Comments, Variable Names,
Assignment Statement, Input & Output, Procedures
2.3 Control Structures
2.4 Complexity of Algorithms
2.5 Subalgorithm
2.1 Algorithm
• An algorithm is a well-defined list of steps for
solving a particular problem.
2.1 Algorithm
• The format for the formal presentation of an
algorithm consist of two parts:
• The first part is a paragraph which tells the purpose
of an algorithm, identifies the variables which
occur in the algorithm and list the input data.
• The second part of the algorithm consists of the list
of steps that is to be executed.
2.2 Algorithmic Notations
Steps, Control, Exit:
• The steps of the algorithm are executed one after the other,
beginning with step 1.
• Control may be transferred to Step n of the algorithm by the
statement “Goto step n”.
• Goto statements may be practically eliminated by using certain
control structures.
• If several statements appear in the same step,
Set K := 1, LOC := 1 and MAX := DATA[1]
then they are executed from left to right.
• The algorithm is completed when the statement Exit(Stop in
flowchart) is encountered.
2.2 Algorithmic Notations
Comments:
• Each step may contain a comment in brackets.
• Which indicates main purpose of step.
• Comment usually appear at the beginning or the end of
the step.
2.2 Algorithmic Notations
Variable Names:
• Variables names will use capital letters, as MAX and
DATA.
• Single-letter names of variables used as counters or
subscripts will also be capitalized in the algorithm.
2.2 Algorithmic Notations
Assignment Statement:
• Assignment statements will use the dots-
equal notation :=
• E.g: MAX := DATA[1]
• Assigns the value in DATA[1] to MAX.
• Some books use the backward arrow or
equal = sign for this operation.
2.2 Algorithmic Notations
Input & Output :
• Data may be input and assigned to variables
by means of a Read statement with the
following form:
Read : MAX
• Data in variable output by means of a Write or
Print statement with the following form:
Write : “Hello”/MAX
2.2 Algorithmic Notations
Procedures :
• The term “Procedure” will be used for an
independent algorithmic module which solves a
particular problem (like function in programming).
• Sometimes called Module.
2.3 Control Structures
There are three types of logic or flow of control:
Control Structure
No Multiple Alternatives
Else yes
Module B
if ?
No
Else yes
Module n
if ?
Else Module
2.3 Control Structures
This algorithm input two numbers NUM1 & NUM2 and
compare them.
Step1. Read NUM1, NUM2.
Step2. If NUM1 > NUM2, then :
Write: NUM1, ”greater than ”, NUM2.
Else if NUM1 < NUM2, then :
Write: NUM1, ”less than ”, NUM2.
Else :
Write: NUM1, ”is equal to”, NUM2.
[End of If structure.]
Step3. Exit.
2.3 Control Structures
(3) Iteration Logic (Repetitive Flow):
• This kind of logic refers to either of two types of structure
involving loops.
• Each type begins with Repeat statement and is followed by
a module, called the body of loop.
• We indicate the end of the structure by the statement [End
of Loop.]
• There are two types of iteration logic:
– Repeat for loop
– Repeat while loop
2.3 Control Structures
i. Repeat for loop:
• The loop will usually have the form:
• This loop uses an index variable, such as K,
to control the loop.
• Here R is called the initial value, S the end
value or test value, and T the increment.
• Loop is executed first with K=R, then with
K=R+T, then with K=R+2T, and so on.
• The cycling ends when K>S.
2.3 Control Structures
ii. Repeat while loop:
• The loop will usually have the form:
• This loop uses the condition to control the
loop.
• Cycling continues until the condition is
false.
2.3 Control Structures
Note:
we can omit the word ‘step’.
We use repeat structures instead of Go to statements.
Subalgorithm
• Complete & independently algorithm module
• Called/invoked by main algorithm / subalgorithm
• Receives values called arguments from
originating(calling) algorithm
• Perform computations
• Sends back the result to calling algorithm
Subalgorithm
• Defined independently
– Can be called by different algorithms
– Can be called different times in same algorithm.
• NAME(PAR1, PAR2, PAR3…..)
Example
Example
The End