Comp Prog Lecture 2
Comp Prog Lecture 2
Al orith01
A step-by-step method for
solving a problem or doing a task
·Output List
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.
Example No. 2:
Find the greatest common divisor of
A=40, B=15; using Euclidean
algorithm;
A = 2B + A = 15 ; B =
10 A = 1B 10 A = 10 ; B
+ 5 A = 2B =5A=5;B=
+0 0
gcd is 5
There are three 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
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.
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
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.
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?...)
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.
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.
Algorithms
can be expressed in
many kinds of notation, including:
Natural language
Pseudo Code
Flowcharts
Programming Language
“Pseudo” means imitation or false
and “code” refers to the instructions
written in a programming language.
Pseudocode is another programming
analysis tool that is used for
planning a program.
Pseudocode is also called
Program Design Language (PDL).
By wikipedia definition:
Pseudocode is a compact and
informal high-level description of a
computer programming
algorithm that uses the
structural conventions of some
programming language, but is
intended for human reading
rather than machine reading.
Pseudocode is made up of the
following logic structures that have
been proved to be sufficient for
writing any computer program:
Sequence Logic
Selection Logic
Iteration Logic
Itis used to perform
instructions in a sequence,
that is one after another.
Thus, for sequence logic,
pseudocode instructions are
written in an order in which they
are to be performed.
The logic flow of pseudocode is
from top to bottom.
It is used for making decisions and
for selecting the proper path out of
two or more alternative paths in
program logic.
It is also known as decision logic.
Selection logic is depicted as
either an IF...THEN or an
IF...THEN...ELSE structure.
It is used to produce loops when
one or more instructions may be
executed several times depending
on some of the conditions.
It uses structures called the
DO_WHILE, FOR and the
REPEAT_UNTIL.
1. Write only one statement per line.
Each statement in your
pseudocode should express just
one action for the computer.
If the task list is properly drawn,
then in most cases each task will
correspond to one line of
pseudocode.
Exampl
es
2. Capitalized initial keyword.
In the example above, READ and
WRITE
are in caps.
There are just a few keywords we
will use:
▪ READ, WRITE, IF, ELSE,
ENDIF, WHILE, ENDWHILE,
REPEAT, UNTIL
3.Indent to show
Wehierarchy.
will use a particular indentation
pattern in each of the design
structures:
▪ SEQUENCE: keep statements that are
“stacked” in sequence all starting in the
same column.
▪ SELECTION: indent the statements that fall
inside the selection structure, but not the
keywords that form the selection
▪ LOOPING: indent the statements that fall
inside the
Exampl
es:
• ▫ 4.All
End themulti-line
initial keyword must always in line
with the last or end of the structure.
structures.
•5. Keep statement language independent.
• ▫ Resist the urge to write in whatever
language you are most comfortable with.
There may be special features available in the
language you plan to eventually write the
program in; if you are SURE it will be written
in that language, then you can use the
features. If not, then avoid using the special
features.
In summary:
Write only one statement per
line.
Capitalized initial keyword.
Indent to show hierarchy.
End multi-line structures.
Keep statement language
independent.
• These are follows:
▫ Number each instruction.
This is to enforce the notion, “well-ordered
collection of
... operations.”
▫ Each instruction should be
unambiguous.
It means the computing agent, in this case
the reader, should be capable of carrying
out the instructions. And also, each
instruction should be effectively computable
(do-able).
▫ Completeness.
Following
are some of the
advantages of using
pseudocode:
Converting a pseudocode to a
programming language is much more
easier than converting a flowchart.
As compared to flowchart, it is easier to
modify a pseudocode of a program
logic when program modifications are
necessary.
Italso suffers from some of the
limitations. These limitations are
as follows:
In the cases of pseudocode, a graphic
representation of program logic is
not available.
There are no standard rules to follow
for using a pseudocode. Different
programmers use their own style of
writing pseudocode and hence,
communication problem occurs due to
To
symbolize the arithmetic
operators we use these symbols:
Note: There is a precedence or
hierarchy implied in this symbols.
When we have to make a choice
between actions, we almost
always base that choice on a test.
There is a universally accepted set
of symbols used to represent these
phrases:
It is more difficult to
follow the logic of or
write pseudocode as
compared to
flowcharting.
Prepare ½ crosswise yellow paper
for seatwork after the discussion.
Introduction To Flowcharting
Flowchart:
Represents an
algorithm in
graphical symbols
Flowchart Symbols
Yes
No
Stop
Connectors on the same page
Start
1 2
2
Connectors on a different page
Page 1 Page 2
Start
2
1
Stop
Yes 1
No
2
The detail of how the function works
is put in another flowchart.
Read
n1, n2 , n3
Body of a function is
AVRG (result, n1, n2,n3) the same with
normal flowchart
Stop
End terminal
must be a “Return”
Start
N=6
AVRG ( result,n1, n2,n3)
Read
N Sum = 10 + 5 +
result = sum/3
Print
average Output:
Average: 7
Return
Stop
Sequence
In a computer program or an algorithm,
sequence involves simple steps which are
to be executed one after the other.
The steps are executed in the same order in which they are written.
In a flowchart,
sequence is expressed as:
In pseudocode,
sequence is expressed as:
process 1
process 2
…
…
process n
Sequence
An Example Using Sequence
Problem: Write a set of instructions that describe how to make a pot of tea.
Pseudocode Flowchart
BEGIN
fill a kettle with water
boil the water in the kettle
put the tea leaves in the pot
pour boiling water in the pot
END
Example:
Start
Read
Length, Input:
Width Length <- 5
Width <- 3
Area: 15
Print
Area, Perimeter: 16
Perimeter
Stop
Selection is used in a computer program or algorithm
to determine which particular step or set of steps is to
be executed
1. IF condition THEN
process 1
ENDIF
2. IF condition THEN
process 1
ELSE
process 2
ENDIF
Selection
Binary (structure)
Binary Selection Binary Selection
1. IF condition THEN
process 1
ENDIF
2. IF condition THEN
process 1
ELSE
process 2
ENDIF
Selection
Binary (flowchart structure)
Note: In a flowchart it is most important to indicate
1. which path is to be followed when the condition is true, and
2. which path to follow when the condition is false.
Without these indications the flowchart is open to more than one interpretation.
Note: There are two acceptable ways to represent a decision in all of the structures.
Either method is acceptable. For consistency, the method 1 is used throughout this document.
Binary Selection
Flowchart
Binary Selection
Pseudocode
CASEWHERE signal is
red : stop the vehicle
amber : stop the vehicle
green : proceed through
the intersection
OTHERWISE : proceed with
caution
ENDCASE
Pseudo Code
• Algorithms are commonly written using
Pseudo code.
• The term “Code” usually refers to as
programming language.
• Pseudo code is an English like language that is
similar to programming language that is used
to write Algorithms.
Looping
1. Ask the user for the Centigrade temperature
2. Store the value in box C
3. Calculate the corresponding Fahrenheit
temperature
4. Store it in box F
5. Print the values in boxes C and F,
Appropriately labelled
6. Go to step 1.
• When the Algorithm reaches step 6, it
does not stop but rather transfers control
to step 1 to repeat the Algorithm again.
• A Loop is a set of instructions which can
be repeated. In this case. Step 1 to step 5
form a loop.
• The problem is there’s no condition
under which the loop can be stopped.
• How long will the loop go for ?
• How do we stop the Algorithm ?
The While loop
1. Ask the user for the WHILE C is not 0 DO
Centigrade temperature
2. Store the value in box C
3. If C = 0, then Stop. Set F to 32+(9C/5)
4. Calculate the Print C and F,
corresponding Fahrenheit
temperature Prompt the user for
5. Store it in box F the
6. Print the values in boxes C Centigrade
and F, Appropriately
temperature C
labelled
7. Go to step 1.
END WHILE
The While loop
When a WHILE loop has the
following steps
• The condition is tested.
• If it is true the statements in
the loop are tested and the
process is repeated from step
1.
• If the condition is false then
the WHILE loop
terminates/Ends with the END
WHILE statement. And the
program ends with the STOP
statement.
Repetition
Repetition allows for a portion of an algorithm or computer program
to be done any number of times
dependent on some condition being met.
An occurrence of repetition is usually known as a loop.
Repetition
Repetition In flowcharting
pre-test repetition
In pseudocode, pre-test is expressed as:
repetition is expressed as:
Repetition
Repetition In a flowchart
post-test repetition
In pseudocode, post-test is expressed as:
repetition is expressed as:
REPEAT
process
UNTIL condition is true
Repetition Pre-test (example)
An Example Using Pre-Test Repetition
Problem: Determine a safety procedure for travelling in a carriage on a moving train.
Pre-test Repetition
Flowchart
Pre-test Repetition
Pseudocode
Post-test Repetition
Flowchart
Post-test Repetition
Pseudocode
REPEAT
beat the egg whites
UNTIL fluffy
For Loop
• The For construct is a simple For C = 0 to 100
statement which executes a piece
of code a set number of times.
• It consists of Set F to
1. The word FOR
32+9C/5
2. The loop variable, (problem in
the example) Print (C,F)
3. The word TO
4. The final value
5. To word DO END FOR
6. One or more lines of STOP
instructions to be executed
( The body of the for loop)
7. The END FOR statement
For Loop
RESULT
C F
0 32.0
1 33.8
2 35.6
3 37.4
4 39.2
5 41.0
6 42.8
7 44.6
8 46.4
9 48.2
10 50.0
IF Then ELSE statement
• Abstraction
• Inheritance
• Encapsulation
• polymorphism
Inheritence
• Each subclass share a common set of
characteristics from its parent class. In
addition to common characteristics they have
their own characteristics as well
• OOP class can be divided into subclasses.
Original class is called parent class and those
that share common characteristics are called
subclass.
Class
• Serve as template of a new data type . It
specifies what data and functions will be
included in objects of that class. Defining class
don’t create ay object . Similar to integer
don’t create any integer variable.
Compilers
• Borland C++
• Turbo C++
Integrated development environment
• Put all the tools that you need to develop
program into a single unit
Borland C++
• It is used to create program in windows
environment
• It is a special kind of windows program that
acts like a DOS program ; it runs under
windows but acts like a DOS program