Beruflich Dokumente
Kultur Dokumente
2
ඇල්ග ොරිතමයක්...
An Algorithm must be…
Be Precise
Be Logical
Be Effective
Terminate Eventually
3
Flow Charts
It is a step by step Diagrammatic representation of
the program
4
ගැලීම් සටහන් සහහ ොද ා ගන්ා සකොත
Standard Flow Charts Symbols
Diagram Shape Representation
Decision Box
Rhombus
5
ගැලීම් සටහන් සහහ ොද ා ගන්ා සකොත
Standard Flow Charts Symbols
Diagram Shape Representation
සංගක්තය හැඩය අදහස
ැලීම් සටහන දීර්ඝ ලන
ලෘත්තය / Circle විට එහි ග ොටස් 2ක්
සම්බන්ධ ලන ගෙන්වීම.
Connection
දත්ත ා යන දිාල
ඊතය / Arrow ගෙන්වීම
Direction of Flow
- උෙ ාර්යය / Sub
Process
6
A Typical Algorithm
Start
Stop
7
Simple Example
Consider the process of getting up in the morning and going to school.
Wake Up
Get Out of Bed
Have A Shower
Dry Yourself
Get Dressed
Have Breakfast
Pack Bag/Lunch
Leave
8
The logic constructs
9
SEQUENCE
10
Simple Example
Consider the process of getting up in the morning and going
to school.
Wake Up
Get Out of Bed
Have A Shower
Dry Yourself
Get Dressed
Have Breakfast
Pack Bag/Lunch
Leave
11
Note
Certain events must occur in a particular order . Example, we
should get out of bed before a shower.
Some events must occur prior to another event(s). Example,
there is no drying yourself prior to a shower
Some other events may occur in any order and do not affect
the overall solution. Example, we can pack bag before or after
having breakfast
12
SEQUENCE
Start
START Wake Up
Dry Yourself
Have Breakfast
Leave
STOP Stop
13
Example -1
14
Start
Solution Sum = 0
Average = 0
Input
Number1
Input
Number2
Sum = Numbe1 +Number2
Average = Sum / 2
Display
Sum, Average
Stop
15
Exercises
Input length and width of a rectangle and
calculate the area and the perimeter.
Input area of the square and calculate the
length.
Input the perimeter of a circle and calculate the
area.
සටහා
Note
Recalling our example algorithm of going to
School, everyone had to follow the same steps.
17
Note
18
Note
19
Note
Note that even though selection is a separate
construct to sequence, the two are combined in the
overall solution, and remember that one doesn’t
replace the other.
20
SELECTION
There may be alternative steps that could be taken subject to a
particular condition
21
SELECTION
IF
True False
Condition?
ENDIF
22
Start
Wake Up
Get Out of
Bed
yes
Example
Am I
Dirty? Have A
Shower
No Dry Yourself
Get Dressed
yes
Am I
Hungr
y? Have
Breakfast
yes
No Is it a
weekda
y? Pack
Bag/Lunch
Read a Book
Leave
Stop
23
Example - 2
24
Start
Get
Length
Get
Width
Is Length Y
equal to
Width ?
N Display
“Figure is Square/
Rhombus”
Stop
25
Example - 3
26
Start
Get
Length
Get
Width
Is Y
Length
equal
WidthN?
Display
“Figure is Square”
Display
“Figure is Rectangle”
Stop
27
Exercises
28
Exercises
29
Note
When a flowchart is too long to fit on a page and is to be
continued on another page a connector symbol is used.
30
A
Add number
Start to value of
total
Increment
Set value of counter by 1
Counter to 0
Set value of
Is N
Total to 0
Count B
B er =
10?
Get Y
Number
Write total
Stop
31
ITERATION
We call it as a Loop
32
ITERATION
There are three different types of loops in structured
programming, all of which are available in all
programming language.
33
ITERATION
Consider the example of getting out of bed
34
ITERATION- While Loop
Section(s) of an algorithm are repeated over and over
(obviously these loops must eventually terminate)
35
ITERATION- While Loop
While
false
Condition
?
true
EndWhile
Statement
sequence
36
ITERATION- for Loop
37
ITERATION- for Loop
For
Control_value = expression
Control_value false
< end_value
?
true
Statement-sequence
Control_value = Control_value +1
Stop
38
Exercise
Draw a flowchart to display the numbers 1, 2, 3, 4, 5,
….., 100
Write an algorithm to find the sum of 10 numbers.
39
Start
no = 1
No
no <= 100
Yes Stop
Display no
no = no + 1
40
Start
counter = 0
total = 0
No
Counter<
10 ?
yes
Display total
Get Number
Stop
total = total+number
counter=counter+1
41
Exercise
42
START
Sum = 0
Read A & B
A decision step is used when the
computer has to make a
comparison
Num = A
No
If Num <= B Print Sum
Yes
43
අභ්ය ස
Exercises
44
අභ්ය ස
Exercises
3. සිසුන් 5 ගේ උස ප්රමාණ ඇතුත් ර සාමානය බා ැනීමට ැලීම් සටහනක් අඳින්න.
උගසහි සාමානයය අඩි 6 ට ලැඩි නම් “The students are very tall” යනුගලන් ෙණිවුඩයක්
දිස්වීමට ද සස්ලන්න.
Input the height of 5 students. Calculate and print the average height. If the average
height is over 6 feet then print the message “The students are very tall”.
Input 10 numbers and count how many negative numbers and how many positive
46