Here is the pseudo code for the given flow chart:
Begin
x = 1
y = 1
While (x <= 5) do
While (y <= 3) do
Print x, y
y = y + 1
EndWhile
x = x + 1
y = 1
EndWhile
End
The output would be:
1,1
1,2
1,3
2,1
2,2
2,3
3,1
3,2
3,3
4,1
4,2
4,3
5,1
5,2
5,3
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
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
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
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
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?
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)
• ………