You are on page 1of 41

CS112 - PROGRAMMING FUNDAMENTAL

Lecture # 03 - Algorithm and Pseudocode


Syed Shahrooz Shamim
Junior Lecturer,
CS Department, UIT
Algorithms
What is Algorithm…?
• By wikipedia definition:
• an algorithm is a sequence of finite
instructions, often used for calculation
and data processing.
• It is formally a type of effective method in
which a list of well-defined instructions
for completing a task will, when given an
initial state, proceed through a well-
defined series of successive states,
eventually terminating in an end-state.
What is Algorithm…?
Euclidean Algorithm
 First documented algorithm by Euclid (300
B.C.) to compute greatest common divisor
(gcd).
 Example: gcd(3,21)=3
 Condition:

1. Let A and B be integers with A > B  0.


2. If B = 0, then the gcd is A and the algorithm ends.
3. Otherwise, find q and r such that
A = qB + r where 0  r < B
Note that we have 0  r < B < A and gcd(A,B) = gcd(B,r).
Replace A by B, B by r. Go to step 2.
Euclidean Algorithm
• Example
– Find the greatest common divisor of A=40,
B=15; using Euclidean algorithm;

A = 2B + 10  A = 15 ; B = 10
A = 1B + 5  A = 10 ; B = 5
A = 2B + 0  A = 5 ; B = 0
gcd is 5
Properties of Algorithm
• There are some properties of algorithm that must
have to consider in solving a certain problem in
programming:
• Finiteness
• Absence of Ambiguity
• Sequence Definition
• Input and Output Definition
• Effectiveness
• Scope of Definition
Properties of Algorithm
• Finiteness
– The execution of a programmed
algorithm must be complete after a finite
number of operations have been
performed. Otherwise, we cannot claim
that the execution produces a solution.
Properties of Algorithm
• Absence of Ambiguity
– The representation of every step of an
algorithm should have a unique
interpretation which also understand by
the human.
– It is convenient to deal with algorithms
presented in notational with sparse detail:
• Example:
– Pseudo code
– Flowcharts
Properties of Algorithm
• Sequence of Definition
– The sequence in which the steps of the
algorithm are to carried out should be
clearly specified.
– In algorithmic specifications, the
instructions are performed from top to
button, unless the instruction themselves
otherwise specified.
Properties of Algorithm
• Input and Output Definition
– Inputs – are the data items that is
presented in the algorithm.
– Outputs – are the data items presented to
the outside world as the result of the
execution of a program based on the
algorithm.
– An algorithm ought to produce at least
one output (otherwise, what use is it?...)
Properties of Algorithm
• Effectiveness
– it consists of basic instructions that are
realizable. This means that the
instructions can be performed by using
the given inputs in a finite amount of
time.
– The instructions of an algorithm may
order the computer only to perform tasks
that is capable of carrying out.
Properties of Algorithm
• Scope Definition
– An algorithm applies to the following:
• Specific problem or class of problem
• The range of inputs has to be predefined
• The range determines the generality of the
algorithm.
How to express an ALGORITHM?
• Algorithms can be expressed in many
kinds of notation, including:
– Natural language
– Pseudo Code
– Flowcharts
– Programming Language
PseudoCode
• Pseudocode (pronounced SOO-doh-kohd) is a
detailed yet readable description of what a
computer program or algorithm must do,
expressed in a formally-styled natural language
rather than in a programming language.
Pseudocode is sometimes used as a detailed step
in the process of developing a program. It
allows designers or lead programmers to
express the design in great detail and provides
programmers a detailed template for the next
step of writing code in a specific programming
language.
PseudoCode
• The first thing we do when designing a
program is to decide on a name for the
program.
PseudoCode
• The first thing we do when designing a
program is to decide on a name for the
program.
• Let’s say we want to write a program to
calculate interest, a good name for the
program would be CalculateInterest.
• Note the use of CamelCase.
PseudoCode
• So we start the program as:

PROGRAM CalculateInterest:
PseudoCode
• So we start the program as:

PROGRAM CalculateInterest:

• And in general it’s:

PROGRAM <ProgramName>:
PseudoCode
• Our program will finish with the following:

END.

• And in general it’s the same:

END.
PseudoCode
• So the general structure of all
programs is:

PROGRAM <ProgramName>:
<Do stuff>
END.
SEQUENCE
PseudoCode
• When we write programs, we assume that
the computer executes the program starting
at the beginning and working its way to the
end.
• This is a basic assumption of all algorithm
design.
• We call this SEQUENCE.
PseudoCode
• In Pseudo code it looks like this:

Statement1;
Statement2;
Statement3;
Statement4;
Statement5;
Statement6;
Statement7;
Statement8;
PseudoCode
• For example, for making a cup of tea:

Organise everything together;


Plug in kettle;
Put teabag in cup;
Put water into kettle;
Wait for kettle to boil;
Add water to cup;
Remove teabag with spoon/fork;
Add milk and/or sugar;
Serve;
PseudoCode
• Or as a program:

PROGRAM MakeACupOfTea:
Organise everything together;
Plug in kettle;
Put teabag in cup;
Put water into kettle;
Wait for kettle to boil;
Add water to cup;
Remove teabag with spoon/fork;
Add milk and/or sugar;
Serve;
END.
SELECTION
PseudoCode
• What if we want to make a choice, for
example, do we want to add sugar or not to
the tea?
• We call this SELECTION.
PseudoCode
• So, we could state this as:

IF (sugar is required)
THEN add sugar;
ELSE don’t add sugar;
ENDIF;
PseudoCode
• Or, in general:

IF (<CONDITION>)
THEN <Statements>;
ELSE <Statements>;
ENDIF;
PseudoCode
• Or to check which number is biggest:

IF (A > B)
THEN Print A + “is bigger”;
ELSE Print B + “is bigger”;
ENDIF;
PseudoCode
• Adding a selection statement in the program:
PROGRAM MakeACupOfTea:
Organise everything together;
Plug in kettle;
Put teabag in cup;
Put water into kettle;
Wait for kettle to boil;
Add water to cup;
Remove teabag with spoon/fork;
Add milk;
IF (sugar is required)
THEN add sugar;
ELSE do nothing;
ENDIF;
Serve;
END.
ITERATION
PseudoCode
• What if we need to tell the computer to keep
doing something until some condition
occurs?
• Let’s say we wish to indicate that the you
need to keep filling the kettle with water
until it is full.
• We need a loop, or ITERATION.
PseudoCode
• Or, in general:

WHILE (<CONDITION>)
DO <Statements>;
ENDWHILE;
PseudoCode
• Or to print out the numbers 1 to 5:

A = 1;
WHILE(A < 5)
DO Print A;
A = A + 1;
ENDWHILE;
PseudoCode
• What is the benefit of using a loop?
PseudoCode
• Consider the problem of searching for an
entry in a phone book with only condition:
Get first entry
If this is the required entry
Then write down phone number
Else get next entry
If this is the correct entry
then write done entry
else get next entry
if this is the correct entry
…………….
PseudoCode
• This could take forever to specify.

• There must be a better way to do it.


PseudoCode
• We may rewrite this as follows:

Get first entry;


Call this entry N;
WHILE N is NOT the required entry
DO Get next entry;
Call this entry N;
ENDWHILE;

This is why we love loops!


PseudoCode
• Or as a program:
PROGRAM MakeACupOfTea:
Organise everything together;
Plug in kettle;
Put teabag in cup;
WHILE (Kettle is not full)
DO keep filling kettle;
ENDWHILE;
Wait for kettle to boil;
Add water to cup;
Remove teabag with spoon/fork;
Add milk;
IF (sugar is required)
THEN add sugar;
ELSE do nothing;
ENDIF;
Serve;
END.

You might also like