KEMBAR78
programming part 1 introduction to python.pptx
Prepared
By
hashini
Danansuriya
Programming
.eg¨ úi£ug l
%uf,aL ,sùu
Lesson 01 Grade 11 (part 1)
1
Prepared
By
hashini
Danansuriya
1.1 Analyzing a problem
(.eg¨jla úYaf,aIKh lsÍu)
2
Processing
Input Output
Raw materials Result
wdod
k
m
%;sodk
h
ieliSu
Prepared
By
hashini
Danansuriya
Example:
How to make a
cake?
3
Prepared
By
hashini
Danansuriya
Exercise:
1. Identify the inputs, process and output related to
dividing 100 toffees among 20 people
2. Identify the inputs, process and output in making a
kite
4
Prepared
By
hashini
Danansuriya
Alternative solutions(úl,am úi÷ï
y÷kd.ekSu)
5
If there are more than one solution to a given problem,
such solutions are called alternative solutions.
.eg¨jla i|yd úi÷ï tllg jvd jeäfhka mj;S kï tajd úl,am úi÷ï f,i ye¢kafõ'
All the solutions pertaining to a problem are called
solution space.
fufia .eg¨jlg mj;sk ish¨ úi÷ï" úi÷ï wjldYh f,I ye¢kafõ'
In order to create a short, simple program the
appropriate solution must be selected from the set of
alternative solutions.
Prepared
By
hashini
Danansuriya
1.2 Problem solving using Algorithms
(.eg¨ úi£u i|yd we,af.dß;u f.dvke.Su)
6
An algorithm is a method(step by step procedure) to
show the steps in solving a problem.
.eg¨jla úi£u i|yd wkq.ukh lrkq ,nk mshjr oelaùfï l%uh
we,af.dß;uhla f,i ye¢kafõ'
Algorithms are needed to present a way to solve the
problem with a plan. fuys wjYH;dj jkafka mQ¾j ie,iqula
iys; j ksjerÈ j .eg¨jlg úi÷ula fiúh yels l%shdud¾.hla
oelaùuhs'
An algorithm is a complete step-by-step
procedure for solving a problem or
accomplishing a task
Prepared
By
hashini
Danansuriya
An Algorithm must be..
7
 Be Precise - Accurate
 Be Logical
 Be Effective
 Terminate Eventually
Prepared
By
hashini
Danansuriya
Algorithms in a program must:
 Obtain a correct solution within a finite time
 be clear, precise and unambiguous(not open to more
than one interpretation) ; and
 be in a format which lends to an elegant
implementation…in a programming language
8
Prepared
By
hashini
Danansuriya
Exercise:
There are 183 students in a primary school. The
principal has decided to hold an inter-house sports
meet dividing them into three houses – Olu, Nelum,
Manel. Develop an algorithm to divide the students into
three houses.
9
Prepared
By
hashini
Danansuriya
1.2.1 Control Structures (md,k jHqy)
10
1.Sequence (wkql%uh)
2.Selection (jrKh)
3.Repetition (mqk¾lrKh)
Prepared
By
hashini
Danansuriya
Sequence wkql%uh
11
 SEQUENCE is a linear progression where one
task is performed sequentially after another…
we,af.dß;uhl msysá mshjr lsysmhla fyda ish,a, u fyda
wdrïNl mshjrlisg wjidk mshjr olajd by< isg my<g tfl,af,a
wkqms<sfj<ska l%shd;aul ùu wkql%uh f,i ye¢kafõ'
 solution steps must follow each other in a
logical sequence
 Statements are executed in the same order as
they are written.
Prepared
By
hashini
Danansuriya
Sequence wkql%uh
12
Wake Up
Get Out of Bed
Have A Shower
Dry Yourself
Get Dressed
Have Breakfast
Pack Bag/Lunch
Leave
Prepared
By
hashini
Danansuriya
Sequence wkql%uh
13
 Certain events must occur in a particular order
 for example, we should get out of bed before a
shower
 Some events must occur prior to another event(s)
 for 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
 for example, we can pack bag before or after
having breakfast
Note:
Prepared
By
hashini
Danansuriya
1) Input length and width of a rectangle and calculate the
area and the perimeter.
2) Input area of the square and calculate the length.
3) Input the perimeter of a circle and calculate the area.
Exercise:
14
Prepared
By
hashini
Danansuriya
Selection jrKh
15
SELECTION - there may be alternative steps
that could be taken subject to a particular
condition.
we,af.dß;uhl we;=<;a fldkafoaishla ;Dma; ùu fyda
fkdùu fyda wkqj l%shd;aul l< hq;= mshjr l=ula o
hkak ;SrKh jk wjia:djla jrKhla f,I ye¢kafõ'
Selection is a situation where steps are
executed depending on whether a
condition of an algorithm is satisfied or not.
fuys § jrK folla mj;sk w;r fldkafoaish ;Dma; jkafka kï
tla jrKhla o ;Dma; fkdjkafka kï wfkla jrKh o
f;dard .efka'
Prepared
By
hashini
Danansuriya
Exercise:
Page 8 – 2nd
question
16
Prepared
By
hashini
Danansuriya
Repetition / Iteration mqk¾lrKh
17
ITERATION - certain steps may need to be
repeated while, or until, a certain condition
is true we,af.dß;uhl we;=<;a mshjrla" mshjr
lsysmhla fyda fldkafoaishla ;Dma; jk f;la fyda ;Dma;
j mj;sk ;=re fyda kej; kej; isÿùu mqk¾lrKh f,I ye
¢kafõ'
In programming we call it as a Loop
If one or several steps of an algorithm are
repeated until a condition is satisfied, it is
called repetition.
Prepared
By
hashini
Danansuriya
Repetition mqk¾lrKh
18
 Consider the example of getting out of bed
 For the step --having a shower-- the following loop
could be substituted;
test cleanliness
while I am dirty
wash myself
test cleanliness
 The loop continues until the condition of cleanliness
changes
Prepared
By
hashini
Danansuriya
Representing an Algorithm
we,af.dß;u bÈßm;a lsÍug úúO fuj,ï Ndú;h
 Flowcharts .e,Sï igyka
 Pseudo Codes jHdc fla;
19
Prepared
By
hashini
Danansuriya
Flowcharts
20
 A flowchart is a step by step
Diagrammatic representation of the
program.
we,af.dß;uh rEmuh wdldrfhka mshjfrka mshjr
f.dv kef.k fyda f.dv ke.S we;s wdldrh fyda bÈßm;a
lsÍu i|yd .e,Sï igyka fhdod .efka‘
 Each type of task is represented by a
symbol
fuys§ tla tla l%shdj oelaùu i|yd my; oelafjk ixfla;
Prepared
By
hashini
Danansuriya
Flowcharts
21
Diagram Notation Representation
Oval Start / End of a Program
wdrïNh fyda wjidkh
Parallelogram Input / Output of Data
wdodkh fyda m%;sodkh
Rectangle Processing Operation
l%shdj,sh
Prepared
By
hashini
Danansuriya
Flowcharts
22
Diagram Notation Representation
Rhombus Decision Box
;SrKh
Circle Connection
iïnkaOlh
Arrows Direction of Flow
.e,Sï ÈYdj
Prepared
By
hashini
Danansuriya
SEQUENCE
Statement 1
Statement 3
Statement 2
START
STOP
23
Prepared
By
hashini
Danansuriya
Start
Stop
Wake Up
Get Out of Bed
Have A Shower
Have Breakfast
Get Dressed
Pack Bag/Lunch
Leave
Dry Yourself
Example:
24
Prepared
By
hashini
Danansuriya
Exercise:
Draw a flow chart to find the sum and average of
two numbers
25
Prepared
By
hashini
Danansuriya
Start
Input
Number1
Input
Number2
Sum = 0
Average = 0
Average = Sum / 2
Display
Sum, Average
Stop
Sum = Numbe1 + Number2
26
Exercise
Prepared
By
hashini
Danansuriya
Selection
27
IF
Condition?
Statement sequence 1
ENDIF
Statement sequence 2
False
True
Prepared
By
hashini
Danansuriya
28
Stop
Pack Bag/Lunch
Leave
Read a Book
yes
Have Breakfast
Get Dressed
Am I
Hungry
?
Is it a
weekday
?
yes
Start
Wake Up
Get Out of Bed
Have A Shower
Am I
Dirty?
yes
No
No
Dry Yourself
No
Example:
Prepared
By
hashini
Danansuriya
29
Exercise:
Draw a flowchart to input the length and width
of a quadrilateral and state whether it is a
square/ rectangle
Prepared
By
hashini
Danansuriya
30
Start
Input the
angle
magnitude
Is angle=
90
Display
“Figure is Square/
Stop
Y
N
Display
“Figure is
rhombus/
Prepared
By
hashini
Danansuriya
31
Exercise:
Draw a flowchart to input the magnitude of
angles of a quadrilateral and state whether it is
a square or a rohmbus.
Prepared
By
hashini
Danansuriya
32
Start
Get
Length
Get
Width
Is Length
equal
Width ?
Display
“Figure is Square”
Stop
Y
N
Display
“Figure is Rectangle”
Prepared
By
hashini
Danansuriya
33
Repetition
Draw a flowchart to find the sum of first fifty
natural numbers
Start
SUM = 0
N = 0
N=N+1
SUM = SUM + N
IS N =
50?
PRINT SUM
End
Prepared
By
hashini
Danansuriya
34
Write down the output generated by the following
flowchart. (Figure 1)
Prepared
By
hashini
Danansuriya
Pseudo codes jHdc fla;
35
An algorithm in simple English form is called Pseudo
codes we,af.dß;uhla ir, bx.%Sis jpk fhdod
f.k ,sÅ; j oelaùu jHdc fla; u.ska isÿ fõ'
 Can plan the program.
 Can use pseudo code to describe the program to non-technical
user.
 Can provide guidelines to a programmer to write the program.
 Opportunity to detect any logic error prior to actual coding.
Advantages of Pseudo Codes
Prepared
By
hashini
Danansuriya
36
BEGIN – To indicate a beginning wdrïNh
END – To indicate an end wjidkh
INPUT, READ, GET – To indicate an input wdodkh
OUTPUT, DISPLAY, SHOW – To show an output m
%;sodkh
PROCESS, CALCULATE – To indicate a process l%shdj,sh
IF … THEN … ELSE … ENDIF – Used to indicate a selection
jrKh
FOR – DO
WHILE – ENDWHILE Used to indicate a repetition
REPEAT – UNTIL mqk¾lrKh
Prepared
By
hashini
Danansuriya
Sequence wkql%uh
37
• In a sequence control structure, statements are executed in
the same order as they are written.
Pseudocode;
statement 1
statement 2
statement 3
• This sequence control structure can be used to represent
four basic computer operations: receive & output information,
perform arithmetic, and assign values
Statement 1
Statement 3
Statement 2
START
STOP
Prepared
By
hashini
Danansuriya
38
Exercise
 Write a pseudo code that inputs two numbers (a
and b) and calculates the sum of the numbers and
output the sum
Prepared
By
hashini
Danansuriya
39
BEGIN
INPUT a
INPUT b
sum = a + b
OUTPUT sum
END
Prepared
By
hashini
Danansuriya
Selection jrKh
40
 Compare and Select One of Two Alternative
Actions
 Select one path according to the condition
◦ IF …. THEN
 If the condition is true do the statements inside IF
 No operation if the condition is false
◦ IF …. THEN …. ELSE
 If the condition is true do the statements inside IF
 If the condition is false do the statements inside ELSE
Prepared
By
hashini
Danansuriya
41
IF condition
THEN
sequence-
1(statements)
ELSE
sequence-
2(statements)
ENDIF
Pseudocode:
IF
Condition?
Statement sequence 1
ENDIF
Statement sequence 2
False
True
Flow chart:
Prepared
By
hashini
Danansuriya
42
IF <condition> THEN
sequence 1
ENDIF
IF <condition> THEN
sequence 1
ELSE
sequence 2
ENDIF
 Example1:
IF a>0 THEN
Print a
END IF
 Example2:
IF a>b THEN
Print a
ELSE
Print b
END IF
Prepared
By
hashini
Danansuriya
43
Exercise:
Write a pseudo code that inputs two numbers (a
and b) and output the largest number.
Prepared
By
hashini
Danansuriya
44
BEGIN
INPUT a
INPUT b
IF a < b THEN
OUTPUT b
ELSE
OUTPUT a
END IF
END
Prepared
By
hashini
Danansuriya
Repetition mqk¾lrK
45
 Same sequence of operations need to be
repeated
 Use
◦ WHILE … ENDWHILE
◦ DO WHILE … ENDDO
◦ FOR … ENDFOR
Prepared
By
hashini
Danansuriya
46
While Loop Pseudocode:
WHILE <Condition>
Statement-
Sequence
END WHILE
While
Condition
?
Statement
sequence
true
false
EndWhile
Prepared
By
hashini
Danansuriya
47
Exercise:
Write a pseudo code that inputs ten numbers
and outputs the sum and average of them.
Prepared
By
hashini
Danansuriya
48
BEGIN
count = 1
sum = 0
WHILE count <= 10
INPUT a
sum = sum + a
count = count + 1
END WHILE
average = sum / 10
OUTPUT sum, average
END
Prepared
By
hashini
Danansuriya
49
Exercise:
Write a pseudo code that inputs ten numbers
and outputs the sum and average of them.
Prepared
By
hashini
Danansuriya
50
BEGIN
count = 0
sum = 0
DO
INPUT a
sum = sum + a
count = count + 1
WHILE count < 10
average = sum / count
OUTPUT sum, average
END
Prepared
By
hashini
Danansuriya
51
The following is a part of a pseudo code which is used to evaluate the final
grade achieved by a student in an examination, based on the average marks
obtained for two subjects.
Set Average to 0
Get Name
Get marks for Subject1
Get marks for Subject2
Average = (Subject1 + Subject2)/2
IF (Average) >= 50
display Name, “Pass”
ELSE
display Name, “Fail”
Ends if
The name and the marks for the subject are as follows.
Write the output of the above pseudo
code for each of the student.
Prepared
By
hashini
Danansuriya
52

programming part 1 introduction to python.pptx