Professional Documents
Culture Documents
Ict1402 1
Ict1402 1
ICT1402
Principles
p of Computer
p Program
g
Design and Programming
P.S. Palliyaguruge
(M.Sc., B.Sc. (Hons))
Syllabus
2
Computer languages
6
• Generation.
Problem Solving
8
• "An
An effective procedure for solving a problem in a
finite number of steps."
• Before
e o e a co
computer
pute can
ca perform
pe o a task,
tas , itt must
ust
have an algorithm that tells it what to do.
• Informally:
I f ll “A
“An algorithm
l ith iis a sett off steps
t that
th t
define how a task is performed.”
• Formally: “An algorithm is an ordered set of
unambiguous executable steps
steps, defining a
terminating process.”
– Ordered set of steps: structure!
– Executable steps:
p doable (achievable)!
( )
– Unambiguous steps: follow the directions!
– Terminating: must have an end!
What is an algorithm? (Cont’d)
13
• Example:
a p e Making
a g a pot o
of tea
tea:
1. If the kettle does not contain water then fill the kettle
2. Plug the kettle into the power point and switch it on.
3. If the teapot is not empty then empty the teapot.
4. Place tea leaves in the teapot.
5. If the water in the kettle is not boiling then go to step 5
6. Switch the kettle off.
7
7. Pour water from the kettle into the teapot
teapot.
• Features:
Sequence (Process)
Decision (Selection)
Repetition (Iteration or Looping)
Features of an algorithm
14
Sequence (Process)
• Each
E h step or process in
i the
h algorithm
l i h isi
p
executed in the specified order.
• Process must be in the correct place
otherwise the algorithm will most probably
fail.
Features of an algorithm
15
Decision (Selection)
( )
– Constructs ‐ If ... then, If ... then ... else ...
• The outcome of a decision is either true or false
false,
– example:
• if today is a rainy day then bring your ubrella
• The decision takes the form:
• if proposition then process
– Approposition:
p a statement which can onlyy be true or
false
• The decision can also be stated as:
– if proposition then process1 else process2 (if ... then
... else ... )
Features of an algorithm
16
Repetition (Iteration or Looping)
• constructs ‐ Repeat and While
• The repeat loop iterate or repeat a process or
sequence off processes untilil some condition
di i becomes
b
true:
• Repeat
Process1
Process2
ProcessN
Until proposition
• Example:
l
– Repeat
Put water in kettle
Until kettle is full
• The process is Put water in kettle, the proposition is
kettle is full.
Features of an algorithm
17
• What If the kettle was alreadyy full? :overflow
• The repeat loop does some processing before
testing the state of the proposition
• The While loop is more appropriate:
• While kettle is not full
the possibility of
put water in kettle
overflow is eliminated.
Important
p Properties
p of Algorithms
g
18
• Correct
– always returns the desired output for all legal
instances of the problem.
• Unambiguous
• Precise
• Efficient
– Can be measured in terms of
• Time
• Space
– Time tends to be more important
Representation of Algorithms
19
• English description
More
More easily
expressed • Pseudo‐code
Pseudo code precise
• High‐level programming
language
• Step‐Form
Step Form
Written in normal language.
M be
May b imprecise
i i
• Pseudocode
Pseudocode is close to normal language
More precise, use a limited vocabulary
• Flowchart use symbols and language to
represent sequence, decision
• Nassi‐Schneiderman and repetition
A strategy
gy for designing
g g algorithms
g
23
Step‐form algorithms
24
Trace tables
• A very useful tool to see the state of the algorithm with as
much detail as you wish
wish.
• Consists of a table in which each row shows the state of step
in the algorithm and each column shows the value of a
variable at that step.
• The trace table allows you to check the algorithm for errors.
errors
1. Set curr to 1
2. Set last to number of letters on the page
3. Set count to 0
A sample text 4. Read letter at curr
5
5. If letter is vowel then increment count
6. Increment curr
7. If curr <= last go to step 4
Step curr last count letter letter is vowel curr <= last
Pseudocode
26
Pseudo language
28
Consists
C i t off kkeywords
d and d EEnglish
li h lik
like phrases
h tto convey th
the
idea about the algorithm
• flow
fl controls l are indicated
i di dbby iindenting
d i theseh phrases
h
• Keywords are written in bold lowercase letters
• Names of data type are in UPPERCASE letters
• Number each line
• The Assignment operator is
• Eg: x 5 ; assign value 5 to variable x
• Loop constructions
• for loop
• for <assignment expression> to <terminal value> [<do in
steps of> <increment>]
Pseudo language
29
Pseudo language
30
• Operators
+ add
‐ subtract A ith ti operators
Arithmetic t
* multiply
/ divide
assign assignment operator: xx+1, count0
= equal to
<= less than or equal to
Relational operators
>= greater than or equal to
<> not equal to
AND
OR Logical operators
NOT
Pseudo language
31
• Pseudo Procedures
• Procedure: A set of instructions to accomplish a given
t k
task
• Specified by giving a name followed by a parameter list
• Find the factorial;
factorial (NATURAL NUMBER n) <‐‐‐‐‐‐‐ Procedure
header
name parameters
• A sequence of steps in the procedure
• Encapsulate either a part of an algorithm or an entire
algorithm
• Eg: Write an algorithm to calculate the factorial of a
given number
Flow chart
32
Symbols :
• The sequence symbol,
• The decision symbol
symbol,
• The decision construct if ... then
• The decision construct if ... then ... else
• The repetition construct ‐ repeat,
• The repetition
p construct ‐ while,,
Flow chart
33
Flow chart
34
Loops
• Repeat loop
Loops
• While loop
Processes/Sequence
Flow chart
36
Decision
• If then… else…
F l
False • Construct has a process at
True Decision
each branch of the
decision
dec s o symbol.
sy bo Thee only
o y
difference here is that
Processes/Sequen Processes/Sequen
ce each value of the
ce
decision (TRUE/FALSE)
has a process associated
with it.
Algorithm Discovery
37