Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

1

ICT1402
Principles
p of Computer
p Program
g
Design and Programming
P.S. Palliyaguruge
(M.Sc., B.Sc. (Hons))

Syllabus
2

• Technique of problem solving: Algorithms, Flowcharts,


Pseudo codes, Paper Simulation
• Fundamentals of C Programming: Structure of a C
program, Input/Output, Variable Declaration,
Arithmetic Operations, Relational Operations, Logical
Operations
• Controll Structures: if/else,
f/ l while
h l repetition, ffor
repetition, switch multiple selection, do/while, break
and continue
continue, functions
functions, scope of variables and
parameters, recursion, arrays, records, comparison of
structure and class, pointers, dynamic memory
allocation , pointers to functions
What is a computer?
3

• A computer is a machine, which has


input and output circuitry.
central processing unit for carrying out
arithmetical and logical operations.

memory f th
for the storage
t off programs and
d data.
d t
Capability
p y of execution a sequences
q of
instructions.

What is a computer program?


4

• "A computer can execute sequences of


instructions
instructions"
• It is a sequence of instructions which the CPU
interprets as arithmetical and logical operations.
operations
A computer program is stored in memory and
also consists of data and instructions. The input
input‐
output circuits (I/O) enable the machine to
transfer data between the machine and the
external world.
• Programming languages are use to write
programs
What is a computer program?
5

• A model of a computer program

keyboard, computer screen,


disk file, printer, disk file
scanner,
tape drive,
etc..

Computer languages
6

• A programming language is an artificial


l
language d
designed
i d tto express computations
t ti
that can be pperformed byy a machine,,
particularly a computer.
• Can be
b used d to create programs
control the behavior of a machine,
express algorithms
mode of human communication.
Computer languages
7

• Generation.

Problem Solving
8

• Programming is a process of problem solving


• Problem solving techniques
– Analyze the problem
– Outline the problem requirements
– Design steps (algorithm) to solve the problem
• Algorithm:
– Step‐by‐step problem‐solving process
– Solution achieved in finite amount of time
Problem Solving Process
9

• Step 1 ‐ Analyze the problem


– Outline the problem and its requirements
– Design
g steps
p (algorithm)
( g ) to solve the problem
p
• Step 2 ‐ Implement the algorithm
– Implement
I l t th
the algorithm
l ith in i code
d
– Verify that the algorithm works
• Step 3 ‐ Maintenance
– Use and modify the program if the problem
domain changes

Analyze the Problem


10

• Thoroughly understand the problem


• Understand problem requirements
– Does
D program require
i user iinteraction?
t ti ?
– Does program manipulate data?
– What is the output?
• If the problem is complex, divide it into
subproblems
– Analyze each subproblem as above
What is an algorithm?
11

• The idea behind the computer program


• Stays the same independent of
– Which kind of hardware it is running on
– Which programming language it is written in
• Solves a well‐specified problem in a general way
• Is specified by
– Describing the set of instances (input) it must work on
– Describing the desired properties of the output

• "An
An effective procedure for solving a problem in a
finite number of steps."

What is an algorithm? (Cont’d)


12

• 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.

• The while loop iterate a process or sequence


of processes if some condition are true:
• While proposition
Process1
Process2
ProcessN

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

• A single algorithm can be represented in many


ways:
– Formulas: F = (9/5)C + 32
– Words: Multiply the Celsius by 9/5 and add 32
32.
– Flow Charts.
– Pseudo‐code.
P d d
• In each case,, the algorithm
g stays
y the same;;
the implementation differs!

Representation of Algorithms (Cont’d)


20

– A program is a representation of an algorithm


designed for computer applications
applications.

– Process: Activity of executing a program, or


execute the algorithm represented by the
program

–  Process: Activityy of executingg an algorithm.


g
Expressing Algorithms
21

• English description
More
More easily
expressed • Pseudo‐code
Pseudo code precise

• High‐level programming
language

Different ways of stating algorithms


22

• 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

Programs = Algorithms + Data


• Analyze
– Identify the processes
– Identify the data (input & output) ‐ variables
– Identify the major decisions if any
– Identify the loops if any
• Devise a "high
g level" algorithm
g
• Refine and test

Step‐form algorithms
24

• Consists of a sequence of numbered steps or points.


points
• More like a "to‐do" list
example:
Design a program which counts all the vowels on a given
page of text
text.

the main process is Count all the vowels


1. Read a letter from the text
2. if the letter is a vowel then increment the vowel counter
3. Determine if the end of the page has been reached
Refine
1
1. Read a letter from the text
2. If the letter is a vowel then increment the vowel counter
3. If the letter is not the last letter then got to step 1
Variables & Data?
Step‐form algorithms
25

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

• Pseudocode is like a programming language but


it rules
its l are less
l stringent.
ti t
• Written as a combination of English
g and
programming constructs
– Based on selection (if,
(if switch) and iteration (while,
(while
repeat) constructs in high‐level programming
a guages
languages
• Design using these high level primitives
– Independent
I d d t off actual
t l programming
i language
l
Pseudocode (Cont’d)
27

Example: Factorial calculation algorithm in pseudo code


• Factorial(NATURAL NUMBER n)
1 fact1
2 for i1 to n do
3 factfact*i
4 return fact

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

• While loop …..


• Do … while loop
• Repeat
R t … until
til loop
l
• A sentinel (guard value) value :A value used to terminate
a loop.
l Whenh a lloop iis used
d to accept values
l ffrom the
h
input device a specific value indicates the end of data
i
input. t
• Array:
• Arrays are declared by appending a pair of square
brackets to the variable name
• Eg: x[ ] ; 1‐D array
y[ ][ ]; 2‐D array
• X[2] 2

Pseudo language
30

• Operators
+ add
‐ subtract A ith ti operators
Arithmetic t
* multiply
/ divide
 assign assignment operator: xx+1, count0
= 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

A graphical method of designing programs :

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

The language of flow charts


Start • Indicates the starting point
Stop • Indicates the terminating point
• P
Processes h
have only
l one entry point
i and
d one exit
i
Process/Sequence
point.

Sub process • Predefined process (subroutine)

• Decisions have onlyy one entryy p


point, one TRUE
D ii
Decision
exit point and one FALSE exit point.

• Connector: Connect several outputs together

Flow chart
34

Loops
• Repeat loop

• The repeat loop has the


process preceding the
Processes/Sequence decision.
dec so
• A repeat loop will always
execute the pprocess p
part
at least once.
Decision
False • The repeat iterates until
True the condition becomes
true
Flow chart
35

Loops
• While loop

• The decision comes first,


followed by the process
False • Iterates
te ates whilee tthee
Decision
condition is true
True

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

• The Two Steps of Program Development:


– 1.
1 Discover the algorithm.
algorithm
– 2. Represent the algorithm as a program.
• Step 2 is the easy step!
• Step
St 1 can b be very diffi
difficult!
lt!
– To discover an algorithm is to solve the problem!

You might also like