SlideShare a Scribd company logo
1 of 59
Programming
 Dr Damitha Karunaratna
Senior Lecturer
University of Colombo School
of Computing
ddk@ucsc.cmb.ac.lk
1
Algorithms/Flow Charts
 What is an algorithm?
 Different ways of representing
algorithms.
 What is a computer program?
 Main steps in solving a problem by
using computer programs.
 How can an algorithm converted
into a computer program?
2
What is an algorithm?
 An algorithm is a step-by-step
procedure (a finite number of steps)
for solving a problem.
• Assumptions
• Objective
• Starting point
• Process
• Terminal point(s)
 Algorithm to get ready to come to school.
2
Different ways of representing
algorithms
 Flow charts – Graphical representation
 Pseudo code – Text representation
• A great deal of pseudo code is a blend of
simple imperative constructs and
English
4
Symbols used in Flow charts
Process
Decision
Input/
Output
Terminator
Connector
Flow
Direction
5
Incorrect Flow charts ????
Is
remainder(X/2)
= 0 ?
Read a number (X)
Start
6
The number is odd
The number is
even
A Flow chart to
determine whether a
given number is odd
or even
Incorrect Flow charts
Y = remainder (x/2)
Is y = 0?
Read a number (X)
Start
7
Stop
Display The number is
odd
Display The
number is even
A Flow chart to
determine whether a
given number is odd
or even
yes
No
How to encode a flow chart as a
program ?
 Must know the various constructs
provided by the language.
• Reserved words
• Flow control mechanisms
 Sequencing
 Selection
 Repetition (Iteration/Loops)
• Decompose(disintegrate) a program into
a collection of subprograms (procedures)
8
Flow Charts - Selection
 Types of Flow Control Statements
 IF ..Then…Else ….End If
 Nest IF
 Select …Case …End Select
1
9
Flow Charts - Repetition
 Types of Flow Control Statements
 For..Next
 Do..While condition
 Do..Until condition
1
10
IF .. Then…Else
Condition
? Statement 1
Statement 3
Statement 2
One course-of-action
Another course-of-
action
True
False
11
IF .. Then…Else - Example
True
False
IF a Student obtained
at least 50 marks he
will pass the Exam ,
otherwise Fail.
Marks>=50
?
Get Student Marks
Print Pass the Exam
Print Fail the
Exam
12
IF .. Then…Else : Example –
Pseudo Code
Get Student Mark (M)
IF (M>=50) Then
Display/Print “Pass”
ELSE
Display/Print “Fail”
ENDIF
13
Nested IF Example
IF a Student obtained more than 80
marks he will get a Distinction else If
he obtained more than or equal to 50,
he will pass else will Fail.
14
Nested IF - Example
Marks>80
?
True
False
Marks>=50
?
True
False
Print Distinction
Print Fail
Print Pass
Get Marks
15
Nested IF - Example
Marks>=50
?
True
False
Marks>80
?
True
False
Print Pass
Print Fail
Print
Distinction
Get Marks
16
This part will
never get
executed
IF .. Then…Else : Example –
Pseudo Code
Get Student Mark (M)
IF (M>80) THEN Display “Distinction”
ELSE
IF (M>=50) THEN
Display “Pass”
ELSE
Display “Fail”
ENDIF
ENDIF
17
Select …Case …End Select
 Case can evaluate only one variable
whereas Nest – IF can evaluate multiple
variables.
 No special Flow chart symbol
18
Select …Case …End Select
Example - Pseudo Code
Get Student Mark (M)
Select Case ( M)
Case 0 To 49
Display “Fail”
Case 50 To 80
Display “Pass”
Case Else
Display “Distinction”
END Select
19
Iterative Statements –Loops
20
Condition
can be at
the end of
the loop
While
condition
Statement(s)
True
False
I = 1
Do
Print I
I = I + 1
Loop while I < 5
Iterative Statements –Loops
21
Or
Condition
can be at
the start
of the loop
While
condition
Statement(s)
True
False
I = 1
Do while I < 5
Print I
I = I + 1
Loop
Iterative Statements –Loops
False
22
Statement (s)
Statement(s)
(s)
Do
While
End Do
True
Statement(s)
(s)
Statement (s)
Statement (s)
Do
Until
End Do
True
Statement (s)
False
Iterative Statements –For Next
For I= 1 To 5
Print I
Next I
I=1
I=I+1
I <=5 ?
NO
YES
Print I
I = 1
Do while I <= 5
Print I
I = I + 1
Loop
23
Iterative Statements –For Next
For I= 1 To 5
Print I
Next I
I=1
I=I+1
I <=5 ?
NO
YES
Print I
23
I I <= 5? Print I I = I + 1
1 yes 1 2
2 yes 2 3
3 yes 3 4
4 yes 4 5
5 yes 5 6
6 No
Iterative Statements –For Next
For I= 1 To 5 step 2
Print I
Next I
I=1
I=I+2
I >5 ?
NO
YES
Print I
23
I I <= 5? Print I I = I + 2
1 yes 1 3
3 yes 3 5
5 yes 5 7
7 No
Iterative Statements –Do..While Cond.
I=0
Do
I=I+1
Print I
Loop While I<5
I=0
I=I+1
I <5 ?
Yes
No
Print I
Condition at
the end of
Loop
24
i=I + 1 Print I I < 5
1 1 yes
2 2 yes
3 3 yes
4 4 yes
5 5 No
Iterative Statements –Do..While Cond.
I=0
Do While I<5
I=I+1
Print I
Loop
I=0
I=I+1
I <5 ?
No
Print I
Yes
Condition
at the top
of Loop
25
i I < 5 I = I + 1 Print I
0 yes 1 1
1 yes 2 2
2 yes 3 3
3 yes 4 4
4 yes 5 5
5 no
Iterative Statements –Do..Until Cond.
I=0
Do Until I=5
I=I+1
Print I
Loop
I=0
I=I+1
I =5 ?
Yes
Print I
No
Condition
at the top
of Loop
26
i I = 5 ? I = I + 1 Print I
0 no 1 1
1 no 2 2
2 no 3 3
3 no 4 4
4 no 5 5
5 yes
Iterative Statements –Do..Until Cond.
I=0
Do
I=I+1
Print I
Loop Until I=5
I=0
I=I+1
I =5 ?
No
Yes
Print I
I=I+1 Print I I=5
1 1 No
2 2 No
3 3 No
4 4 No
5 5 Yes
Condition at
the end of
Loop
27
Review Question (OL 2010 Q23)
Start
Set Required
Temp (R) to 26 C
Read
Current
Temp (C)
Is C>R?
Display “Hot”
End
Display
“Cold”
No
Yes
What are the outputs of
the flowchart, if the
current temp. is 280 C,
270 C and 260 C ,
respectively?
Not an Iterative
pseudo code.
R=26 C=28
C>R Display “Hot
R=26 C=27
C>R Display “Hot”
R=26 C=26
C=R Display “Cold”
28
Review Question (OL 2010 Q24)
Start
Set Required
Temp (R) to 26 C
Read
Current
Temp (C)
Is C>R?
Display “Hot”
End
Display
“Cold”
No
Yes
Which of the following control
structures are required to
convert the flowchart into a
computer program?
A- If-Then construct
B- If-Then-Else construct
C- For Loop
D- Do while loop
Not an Iterative
pseudo code.
Answer : B only
29
Review Question (OL 2010 Q24)
Start
Set Required
Temp (R) to 26 C
Read
Current
Temp (C)
Is C>R?
Display “Hot”
End
Display
“Cold”
No
Yes
Which of the following data
types is suitable for the
variables C and R in the
above flow chart?
(1)String (2) Real
(3) Boolean (4) Currency
C, R – Integer or
Real
C, R needs to
compare
Answer- Real
30
Review Question (OL 2010 Q28)
Consider the following pseudo code?
Begin
Input numberOne
Input numberTwo
Result = numberOne/numberTwo
Output Result
Output numberTwo
Output numberOne
End
Which of the following is a possible output of the
above pseudo code?
(1) 4 ,12, 3 (2) 5, 2, 10 (3) 5,10,2 (4) 4, 8 , 2
Res N2,N1 Res N2,N1 Res N2,N1 Res N2,N1
0, 3, 12 5, 2,10 0,10,2 0, 8,2
31
Review Question (OL 2009 Q27)
-Read Set of Temp. (T)
-Display “Cold” , If they
are below 26.
- Identify the correct
sequence of labels
denoted by 1,2,3,4,5,6
of the flow chart
Start
1
2
END
3
4
5
6
NO
Yes
32
Review Question (OL 2009 Q27)
-
Start
1
2
END
3
4
5
6
NO
Yes
Read Set of Temp. (T)
-Display “Cold” , If they are
below 26.
- Identify the correct
sequence of labels
(1)Read T, T<26,No, “Cold”, Yes,
Any more?
(2)Read T, T<=25,Yes, “Cold”,
No, Any more?
(3)Read T, Any more?,Yes, Cold,
T<=26
(4)Read T, Any more? ,No, T<26,
Yes, “Cold”,
33
Review Question (OL 2009 Q28)
 Begin
X=1
Y=1
While (X=Y)
Z=X+Y
Y=2
End While
Display Z
End
What is the value
output of Z?
X Y (X=Y) Z Y Loop Cnt.
1 1 Yes 2 2 1
1 2 No
Answer =2
34
Exercise
 Begin
X=1
Y=3
While (X<=Y)
Z=X+Y
Y=Y-1
End While
Display Z
End
What is the value output
of Z?
How many times the
loop body is performed?
X Y (X<=Y) Z Loop Count
1 3 Yes 4 1
1 2 Yes 3 2
1 1 Yes 2 3
1 0 No
Answer =2, 3 times 35
Review Question (OL 2009 Q29)

N=10
M=6
Do While M>5
N=N-1
Loop
If N is an Integer
variable, how
many times the
loop will be
executed.
N=10 M=6
M>5 N Loop Count
Yes 9 1
Yes 8 2
. .
. .
Answer =Never Ends 36
Replace M>5
with N>5. How
many times the
loop will be
executed?
Review Question (OL 2011 Q22)
(1)It repeats
more than 3
times
(2) It stops when
3 is entered
for num.
(3)It displays the
total when the
counter is 3.
(4)It gets 4
numbers from
a user.
Start
Set counter to zero
Set total to zero
Get number num
Is counter
less than 3?
Display total
End
No
Yes
Add num to total
Add 1 to counter
Which of the following is
correct regarding the
flowchart?
37
Review Question (OL 2011 Q22)
(1)It repeats more
than 3 times
(2) It stops when 3 is
entered for num.
(3)It displays the total
when the counter is
3.
(4)I gets 4 numbers
from a user.
Start
Set counter to zero
Set total to zero
Get number num
Is counter
less than 3?
Display total
End
No
Yes
Add num to total
Add 1 to counter
Total=0, count=0
num total count loop count<3
1 1 1 1 yes
2 3 2 2 yes
3 6 3 3 No
Total=6 38
Review Question (OL 2011 Q23)
Which of the
following can be
used to replace “Add
num to total” in the
flowchart?
Start
Set counter to zero
Set total to zero
Get number num
Is counter
less than 3?
Display total
End
No
Yes
Add num to total
Add 1 to counter
total = total +num
39
(1) Statements within an if-Then construct
is executed when the condition is false.
(2) For-Next loop is used when the number
of repetitions is know in advance.
(3) If – Then construct cannot be used
within a For-Next loop.
(4) If- Then construct cannot be nested.
Review Question (OL 2011 Q24)
Which of the following statements
correct regarding control structures?
Only (2) is correct
40
(A) An operator performs an operation on
one or more variables.
(B) An expression may have more than
one operator.
(C) Comparison operators compare two
expressions.
Review Question (OL 2011 Q25)
Consider the following statements regarding
operators in a programming Language.
All are correct
41
Review Question (OL 2011 Part 2 Q1 ix)
Consider the set of integer numbers
S=(2,3,4,5,6,7,8,9,10). The following
pseudo code calculates the total of the
even numbers of the set S. Complete the
blanks.
42
Begin
Set Total to Zero
For (Counter =………To ………Step….)
Total=………………..
Next Counter
End
2 10 2
Total + Count
Exercise
Consider the set of integer numbers
S=(15,13,11,9,7,5,3,1). The following
pseudo code calculates the total of the
odd numbers of the set S. Complete the
blanks.
43
Begin
Set Total to Zero
For (Counter =………To ………Step….)
Total=………………..
Next Counter
End
15 1 -2
Total + Count
Exercise
Write down the
output generated
by the following
flow chart.
44
Start
Set X to 1
Display X
Is X greater
than 5?
End
Yes
No
Add 1 to X
1 2 3 4 5
Exercise
Consider the
following
algorithm:
Get A
Get B
Get C
m=A
If A>B then
m=A
else m=B
endif
If C>m then
m=C
endif
Display m
If A=2, B=6 and
C=3, what would
be the output?
Output :
6
45
Write the pseudo code for the following Flow Chart?
What would be the output?
x=1
End
No Yes
Start
Is x<=5? y=1
Is y<=3?
Yes
x=x+1
Print
x,y
No
y=y+1
46
x=1
End
No Yes
Start
Is
x<=5?
y=1
Is
y<=3?
Yes
x=x+1
Print
x,y
No
y=y+1
47
BEGIN
x=1
Do while x<=5
y=1
If y<=3
Then
BEGIN
Print x,y
x=x+1
END
Else
y=y+1
Endif
Loop
END
1,1 Output
2 1
3 1
4 1
5,1
48
BEGIN
x=1
Do While x<=5
y=1
If y<=3
Then
BEGIN
Print x,y
x=x+1
END
Else
y=y+1
Endif
Loop
END
5 times
X=6
Exercise :
How many times the
loop will be executed?
What is the value of x
after completing the
loop?
Naming Variable Names in VB
 Should start with an English Lettter.
 The characters following the first letter
can be a mixture of letters from the
English alphabet , numbers or the
symbol ‘_’ (Underscore)
 The Max number of characters a name
can contain is 255
 VB command names (Reserved words)
cannot be used as variable names 49
Which of the following can be
used as a VB variable name?
 ABC5
 Madhawa Diaz
 Number_of_coins
 5AB
 Name_
 Mod
50
Consider the following statements
(i) An array is a data structure that can store
several data elements of the same type.
(ii) When using an array, one can access
multiple values stored in the array by the
same name using an index which distinguish
them from one another.
(iii) An array is data structure which consists of a
group of same data type elements that are
accessed by indexing
51
Which of the above statements are
correct?
Default Operator Precedence
1. 5+4^2/2 – 5
2. 5 – 2 + 3
Default Operator Precedence
 Exponentiation (^)
 Unary identity and negation (+, –)
 Multiplication and floating-point division (*, /)
 Integer division ()
 Modulus arithmetic (Mod)
 Addition and subtraction (+, –)
High
Low
Default Operator Precedence
1. 5+4^2/2 – 5
= (5+((4^2)/2))– 5
= (5+(16/2))– 5
= (5+8)– 5
= 13 – 5
= 8
Changing the Default Operator
Precedence
1. 5+4^2/(2 – 4)
= 5+((4^2)/(2 – 4))
= 5+((4^2)/(-2))
= 5+(16/(-2))
= 5 – 8
= -3
Variables
 Variables can be considered as symbolic
names given for memory locations.
 Major properties of variables
• Should have a name
• Should have a types
• Should have a values (will change)
• ………
END
www.vidusala.com

More Related Content

Similar to Programming Algorithms Guide

Chapter 1 Study Guide
Chapter 1  Study  GuideChapter 1  Study  Guide
Chapter 1 Study Guide♥Moriah♥
 
Basic Slides on Algorithms and Flowcharts
Basic Slides on Algorithms and FlowchartsBasic Slides on Algorithms and Flowcharts
Basic Slides on Algorithms and Flowchartsmoazwinner
 
UNIT I - Algorithmic Problem Solving.pptx
UNIT I - Algorithmic Problem Solving.pptxUNIT I - Algorithmic Problem Solving.pptx
UNIT I - Algorithmic Problem Solving.pptxPradeepkumar964266
 
Lecture1-Algorithms-and-Flowcharts-ppt.ppt
Lecture1-Algorithms-and-Flowcharts-ppt.pptLecture1-Algorithms-and-Flowcharts-ppt.ppt
Lecture1-Algorithms-and-Flowcharts-ppt.pptReshuReshma8
 
Gcse revision cards checked 190415
Gcse revision cards checked 190415Gcse revision cards checked 190415
Gcse revision cards checked 190415claire meadows-smith
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchartSachin Goyani
 
pseudocode and Flowchart
pseudocode and Flowchartpseudocode and Flowchart
pseudocode and FlowchartALI RAZA
 
Stanford splash spring 2016 basic programming
Stanford splash spring 2016 basic programmingStanford splash spring 2016 basic programming
Stanford splash spring 2016 basic programmingYu-Sheng (Yosen) Chen
 
AlgorithmAndFlowChart.pdf
AlgorithmAndFlowChart.pdfAlgorithmAndFlowChart.pdf
AlgorithmAndFlowChart.pdfSusieMaestre1
 
Civil Service Review in Math.pptx
Civil Service Review in Math.pptxCivil Service Review in Math.pptx
Civil Service Review in Math.pptxEllaineEspinosa1
 
Psuedocode1, algorithm1, Flowchart1.pptx
Psuedocode1, algorithm1, Flowchart1.pptxPsuedocode1, algorithm1, Flowchart1.pptx
Psuedocode1, algorithm1, Flowchart1.pptxMattFlordeliza1
 
256958.ppt
256958.ppt256958.ppt
256958.pptBimlesh7
 
W1-L1 Negative-numbers-ppt..pptx
W1-L1 Negative-numbers-ppt..pptxW1-L1 Negative-numbers-ppt..pptx
W1-L1 Negative-numbers-ppt..pptxGhassan44
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Abou Bakr Ashraf
 
TIU CET Review Math Session 2 by Young Einstein
TIU CET Review Math Session 2 by Young EinsteinTIU CET Review Math Session 2 by Young Einstein
TIU CET Review Math Session 2 by Young Einsteinyoungeinstein
 

Similar to Programming Algorithms Guide (20)

Chapter 1 Study Guide
Chapter 1  Study  GuideChapter 1  Study  Guide
Chapter 1 Study Guide
 
Basic Slides on Algorithms and Flowcharts
Basic Slides on Algorithms and FlowchartsBasic Slides on Algorithms and Flowcharts
Basic Slides on Algorithms and Flowcharts
 
UNIT I - Algorithmic Problem Solving.pptx
UNIT I - Algorithmic Problem Solving.pptxUNIT I - Algorithmic Problem Solving.pptx
UNIT I - Algorithmic Problem Solving.pptx
 
Lecture1-Algorithms-and-Flowcharts-ppt.ppt
Lecture1-Algorithms-and-Flowcharts-ppt.pptLecture1-Algorithms-and-Flowcharts-ppt.ppt
Lecture1-Algorithms-and-Flowcharts-ppt.ppt
 
Gcse revision cards checked 190415
Gcse revision cards checked 190415Gcse revision cards checked 190415
Gcse revision cards checked 190415
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
pseudocode and Flowchart
pseudocode and Flowchartpseudocode and Flowchart
pseudocode and Flowchart
 
Algorithmsandflowcharts1
Algorithmsandflowcharts1Algorithmsandflowcharts1
Algorithmsandflowcharts1
 
Stanford splash spring 2016 basic programming
Stanford splash spring 2016 basic programmingStanford splash spring 2016 basic programming
Stanford splash spring 2016 basic programming
 
AlgorithmAndFlowChart.pdf
AlgorithmAndFlowChart.pdfAlgorithmAndFlowChart.pdf
AlgorithmAndFlowChart.pdf
 
Civil Service Review in Math.pptx
Civil Service Review in Math.pptxCivil Service Review in Math.pptx
Civil Service Review in Math.pptx
 
Psuedocode1, algorithm1, Flowchart1.pptx
Psuedocode1, algorithm1, Flowchart1.pptxPsuedocode1, algorithm1, Flowchart1.pptx
Psuedocode1, algorithm1, Flowchart1.pptx
 
Python for Beginners(v2)
Python for Beginners(v2)Python for Beginners(v2)
Python for Beginners(v2)
 
256958.ppt
256958.ppt256958.ppt
256958.ppt
 
Class 8 Lecture Notes
Class 8 Lecture NotesClass 8 Lecture Notes
Class 8 Lecture Notes
 
W1-L1 Negative-numbers-ppt..pptx
W1-L1 Negative-numbers-ppt..pptxW1-L1 Negative-numbers-ppt..pptx
W1-L1 Negative-numbers-ppt..pptx
 
CST2403 NOTES
CST2403 NOTESCST2403 NOTES
CST2403 NOTES
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
TIU CET Review Math Session 2 by Young Einstein
TIU CET Review Math Session 2 by Young EinsteinTIU CET Review Math Session 2 by Young Einstein
TIU CET Review Math Session 2 by Young Einstein
 

Recently uploaded

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 

Recently uploaded (20)

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 

Programming Algorithms Guide

  • 1. Programming  Dr Damitha Karunaratna Senior Lecturer University of Colombo School of Computing ddk@ucsc.cmb.ac.lk 1
  • 2. Algorithms/Flow Charts  What is an algorithm?  Different ways of representing algorithms.  What is a computer program?  Main steps in solving a problem by using computer programs.  How can an algorithm converted into a computer program? 2
  • 3. What is an algorithm?  An algorithm is a step-by-step procedure (a finite number of steps) for solving a problem. • Assumptions • Objective • Starting point • Process • Terminal point(s)  Algorithm to get ready to come to school. 2
  • 4. Different ways of representing algorithms  Flow charts – Graphical representation  Pseudo code – Text representation • A great deal of pseudo code is a blend of simple imperative constructs and English 4
  • 5. Symbols used in Flow charts Process Decision Input/ Output Terminator Connector Flow Direction 5
  • 6. Incorrect Flow charts ???? Is remainder(X/2) = 0 ? Read a number (X) Start 6 The number is odd The number is even A Flow chart to determine whether a given number is odd or even
  • 7. Incorrect Flow charts Y = remainder (x/2) Is y = 0? Read a number (X) Start 7 Stop Display The number is odd Display The number is even A Flow chart to determine whether a given number is odd or even yes No
  • 8. How to encode a flow chart as a program ?  Must know the various constructs provided by the language. • Reserved words • Flow control mechanisms  Sequencing  Selection  Repetition (Iteration/Loops) • Decompose(disintegrate) a program into a collection of subprograms (procedures) 8
  • 9. Flow Charts - Selection  Types of Flow Control Statements  IF ..Then…Else ….End If  Nest IF  Select …Case …End Select 1 9
  • 10. Flow Charts - Repetition  Types of Flow Control Statements  For..Next  Do..While condition  Do..Until condition 1 10
  • 11. IF .. Then…Else Condition ? Statement 1 Statement 3 Statement 2 One course-of-action Another course-of- action True False 11
  • 12. IF .. Then…Else - Example True False IF a Student obtained at least 50 marks he will pass the Exam , otherwise Fail. Marks>=50 ? Get Student Marks Print Pass the Exam Print Fail the Exam 12
  • 13. IF .. Then…Else : Example – Pseudo Code Get Student Mark (M) IF (M>=50) Then Display/Print “Pass” ELSE Display/Print “Fail” ENDIF 13
  • 14. Nested IF Example IF a Student obtained more than 80 marks he will get a Distinction else If he obtained more than or equal to 50, he will pass else will Fail. 14
  • 15. Nested IF - Example Marks>80 ? True False Marks>=50 ? True False Print Distinction Print Fail Print Pass Get Marks 15
  • 16. Nested IF - Example Marks>=50 ? True False Marks>80 ? True False Print Pass Print Fail Print Distinction Get Marks 16 This part will never get executed
  • 17. IF .. Then…Else : Example – Pseudo Code Get Student Mark (M) IF (M>80) THEN Display “Distinction” ELSE IF (M>=50) THEN Display “Pass” ELSE Display “Fail” ENDIF ENDIF 17
  • 18. Select …Case …End Select  Case can evaluate only one variable whereas Nest – IF can evaluate multiple variables.  No special Flow chart symbol 18
  • 19. Select …Case …End Select Example - Pseudo Code Get Student Mark (M) Select Case ( M) Case 0 To 49 Display “Fail” Case 50 To 80 Display “Pass” Case Else Display “Distinction” END Select 19
  • 20. Iterative Statements –Loops 20 Condition can be at the end of the loop While condition Statement(s) True False I = 1 Do Print I I = I + 1 Loop while I < 5
  • 21. Iterative Statements –Loops 21 Or Condition can be at the start of the loop While condition Statement(s) True False I = 1 Do while I < 5 Print I I = I + 1 Loop
  • 22. Iterative Statements –Loops False 22 Statement (s) Statement(s) (s) Do While End Do True Statement(s) (s) Statement (s) Statement (s) Do Until End Do True Statement (s) False
  • 23. Iterative Statements –For Next For I= 1 To 5 Print I Next I I=1 I=I+1 I <=5 ? NO YES Print I I = 1 Do while I <= 5 Print I I = I + 1 Loop 23
  • 24. Iterative Statements –For Next For I= 1 To 5 Print I Next I I=1 I=I+1 I <=5 ? NO YES Print I 23 I I <= 5? Print I I = I + 1 1 yes 1 2 2 yes 2 3 3 yes 3 4 4 yes 4 5 5 yes 5 6 6 No
  • 25. Iterative Statements –For Next For I= 1 To 5 step 2 Print I Next I I=1 I=I+2 I >5 ? NO YES Print I 23 I I <= 5? Print I I = I + 2 1 yes 1 3 3 yes 3 5 5 yes 5 7 7 No
  • 26. Iterative Statements –Do..While Cond. I=0 Do I=I+1 Print I Loop While I<5 I=0 I=I+1 I <5 ? Yes No Print I Condition at the end of Loop 24 i=I + 1 Print I I < 5 1 1 yes 2 2 yes 3 3 yes 4 4 yes 5 5 No
  • 27. Iterative Statements –Do..While Cond. I=0 Do While I<5 I=I+1 Print I Loop I=0 I=I+1 I <5 ? No Print I Yes Condition at the top of Loop 25 i I < 5 I = I + 1 Print I 0 yes 1 1 1 yes 2 2 2 yes 3 3 3 yes 4 4 4 yes 5 5 5 no
  • 28. Iterative Statements –Do..Until Cond. I=0 Do Until I=5 I=I+1 Print I Loop I=0 I=I+1 I =5 ? Yes Print I No Condition at the top of Loop 26 i I = 5 ? I = I + 1 Print I 0 no 1 1 1 no 2 2 2 no 3 3 3 no 4 4 4 no 5 5 5 yes
  • 29. Iterative Statements –Do..Until Cond. I=0 Do I=I+1 Print I Loop Until I=5 I=0 I=I+1 I =5 ? No Yes Print I I=I+1 Print I I=5 1 1 No 2 2 No 3 3 No 4 4 No 5 5 Yes Condition at the end of Loop 27
  • 30. Review Question (OL 2010 Q23) Start Set Required Temp (R) to 26 C Read Current Temp (C) Is C>R? Display “Hot” End Display “Cold” No Yes What are the outputs of the flowchart, if the current temp. is 280 C, 270 C and 260 C , respectively? Not an Iterative pseudo code. R=26 C=28 C>R Display “Hot R=26 C=27 C>R Display “Hot” R=26 C=26 C=R Display “Cold” 28
  • 31. Review Question (OL 2010 Q24) Start Set Required Temp (R) to 26 C Read Current Temp (C) Is C>R? Display “Hot” End Display “Cold” No Yes Which of the following control structures are required to convert the flowchart into a computer program? A- If-Then construct B- If-Then-Else construct C- For Loop D- Do while loop Not an Iterative pseudo code. Answer : B only 29
  • 32. Review Question (OL 2010 Q24) Start Set Required Temp (R) to 26 C Read Current Temp (C) Is C>R? Display “Hot” End Display “Cold” No Yes Which of the following data types is suitable for the variables C and R in the above flow chart? (1)String (2) Real (3) Boolean (4) Currency C, R – Integer or Real C, R needs to compare Answer- Real 30
  • 33. Review Question (OL 2010 Q28) Consider the following pseudo code? Begin Input numberOne Input numberTwo Result = numberOne/numberTwo Output Result Output numberTwo Output numberOne End Which of the following is a possible output of the above pseudo code? (1) 4 ,12, 3 (2) 5, 2, 10 (3) 5,10,2 (4) 4, 8 , 2 Res N2,N1 Res N2,N1 Res N2,N1 Res N2,N1 0, 3, 12 5, 2,10 0,10,2 0, 8,2 31
  • 34. Review Question (OL 2009 Q27) -Read Set of Temp. (T) -Display “Cold” , If they are below 26. - Identify the correct sequence of labels denoted by 1,2,3,4,5,6 of the flow chart Start 1 2 END 3 4 5 6 NO Yes 32
  • 35. Review Question (OL 2009 Q27) - Start 1 2 END 3 4 5 6 NO Yes Read Set of Temp. (T) -Display “Cold” , If they are below 26. - Identify the correct sequence of labels (1)Read T, T<26,No, “Cold”, Yes, Any more? (2)Read T, T<=25,Yes, “Cold”, No, Any more? (3)Read T, Any more?,Yes, Cold, T<=26 (4)Read T, Any more? ,No, T<26, Yes, “Cold”, 33
  • 36. Review Question (OL 2009 Q28)  Begin X=1 Y=1 While (X=Y) Z=X+Y Y=2 End While Display Z End What is the value output of Z? X Y (X=Y) Z Y Loop Cnt. 1 1 Yes 2 2 1 1 2 No Answer =2 34
  • 37. Exercise  Begin X=1 Y=3 While (X<=Y) Z=X+Y Y=Y-1 End While Display Z End What is the value output of Z? How many times the loop body is performed? X Y (X<=Y) Z Loop Count 1 3 Yes 4 1 1 2 Yes 3 2 1 1 Yes 2 3 1 0 No Answer =2, 3 times 35
  • 38. Review Question (OL 2009 Q29)  N=10 M=6 Do While M>5 N=N-1 Loop If N is an Integer variable, how many times the loop will be executed. N=10 M=6 M>5 N Loop Count Yes 9 1 Yes 8 2 . . . . Answer =Never Ends 36 Replace M>5 with N>5. How many times the loop will be executed?
  • 39. Review Question (OL 2011 Q22) (1)It repeats more than 3 times (2) It stops when 3 is entered for num. (3)It displays the total when the counter is 3. (4)It gets 4 numbers from a user. Start Set counter to zero Set total to zero Get number num Is counter less than 3? Display total End No Yes Add num to total Add 1 to counter Which of the following is correct regarding the flowchart? 37
  • 40. Review Question (OL 2011 Q22) (1)It repeats more than 3 times (2) It stops when 3 is entered for num. (3)It displays the total when the counter is 3. (4)I gets 4 numbers from a user. Start Set counter to zero Set total to zero Get number num Is counter less than 3? Display total End No Yes Add num to total Add 1 to counter Total=0, count=0 num total count loop count<3 1 1 1 1 yes 2 3 2 2 yes 3 6 3 3 No Total=6 38
  • 41. Review Question (OL 2011 Q23) Which of the following can be used to replace “Add num to total” in the flowchart? Start Set counter to zero Set total to zero Get number num Is counter less than 3? Display total End No Yes Add num to total Add 1 to counter total = total +num 39
  • 42. (1) Statements within an if-Then construct is executed when the condition is false. (2) For-Next loop is used when the number of repetitions is know in advance. (3) If – Then construct cannot be used within a For-Next loop. (4) If- Then construct cannot be nested. Review Question (OL 2011 Q24) Which of the following statements correct regarding control structures? Only (2) is correct 40
  • 43. (A) An operator performs an operation on one or more variables. (B) An expression may have more than one operator. (C) Comparison operators compare two expressions. Review Question (OL 2011 Q25) Consider the following statements regarding operators in a programming Language. All are correct 41
  • 44. Review Question (OL 2011 Part 2 Q1 ix) Consider the set of integer numbers S=(2,3,4,5,6,7,8,9,10). The following pseudo code calculates the total of the even numbers of the set S. Complete the blanks. 42 Begin Set Total to Zero For (Counter =………To ………Step….) Total=……………….. Next Counter End 2 10 2 Total + Count
  • 45. Exercise Consider the set of integer numbers S=(15,13,11,9,7,5,3,1). The following pseudo code calculates the total of the odd numbers of the set S. Complete the blanks. 43 Begin Set Total to Zero For (Counter =………To ………Step….) Total=……………….. Next Counter End 15 1 -2 Total + Count
  • 46. Exercise Write down the output generated by the following flow chart. 44 Start Set X to 1 Display X Is X greater than 5? End Yes No Add 1 to X 1 2 3 4 5
  • 47. Exercise Consider the following algorithm: Get A Get B Get C m=A If A>B then m=A else m=B endif If C>m then m=C endif Display m If A=2, B=6 and C=3, what would be the output? Output : 6 45
  • 48. Write the pseudo code for the following Flow Chart? What would be the output? x=1 End No Yes Start Is x<=5? y=1 Is y<=3? Yes x=x+1 Print x,y No y=y+1 46
  • 49. x=1 End No Yes Start Is x<=5? y=1 Is y<=3? Yes x=x+1 Print x,y No y=y+1 47 BEGIN x=1 Do while x<=5 y=1 If y<=3 Then BEGIN Print x,y x=x+1 END Else y=y+1 Endif Loop END 1,1 Output 2 1 3 1 4 1 5,1
  • 50. 48 BEGIN x=1 Do While x<=5 y=1 If y<=3 Then BEGIN Print x,y x=x+1 END Else y=y+1 Endif Loop END 5 times X=6 Exercise : How many times the loop will be executed? What is the value of x after completing the loop?
  • 51. Naming Variable Names in VB  Should start with an English Lettter.  The characters following the first letter can be a mixture of letters from the English alphabet , numbers or the symbol ‘_’ (Underscore)  The Max number of characters a name can contain is 255  VB command names (Reserved words) cannot be used as variable names 49
  • 52. Which of the following can be used as a VB variable name?  ABC5  Madhawa Diaz  Number_of_coins  5AB  Name_  Mod 50
  • 53. Consider the following statements (i) An array is a data structure that can store several data elements of the same type. (ii) When using an array, one can access multiple values stored in the array by the same name using an index which distinguish them from one another. (iii) An array is data structure which consists of a group of same data type elements that are accessed by indexing 51 Which of the above statements are correct?
  • 54. Default Operator Precedence 1. 5+4^2/2 – 5 2. 5 – 2 + 3
  • 55. Default Operator Precedence  Exponentiation (^)  Unary identity and negation (+, –)  Multiplication and floating-point division (*, /)  Integer division ()  Modulus arithmetic (Mod)  Addition and subtraction (+, –) High Low
  • 56. Default Operator Precedence 1. 5+4^2/2 – 5 = (5+((4^2)/2))– 5 = (5+(16/2))– 5 = (5+8)– 5 = 13 – 5 = 8
  • 57. Changing the Default Operator Precedence 1. 5+4^2/(2 – 4) = 5+((4^2)/(2 – 4)) = 5+((4^2)/(-2)) = 5+(16/(-2)) = 5 – 8 = -3
  • 58. Variables  Variables can be considered as symbolic names given for memory locations.  Major properties of variables • Should have a name • Should have a types • Should have a values (will change) • ………