Anda di halaman 1dari 59

Programming

Dr Damitha Karunaratna Senior Lecturer University of Colombo School of Computing ddk@ucsc.cmb.ac.lk

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

Symbols used in Flow charts


Terminator
Process

Input/ Output

Decision

Connector

Flow Direction

Incorrect Flow charts ????


Start Read a number (X) A Flow chart to determine whether a given number is odd or even

Is remainder(X/2) =0?

The number is even

The number is odd

Incorrect Flow charts


Start Read a number (X) Y = remainder (x/2) yes
Is y = 0?

A Flow chart to determine whether a given number is odd or even

Display The number is even

No Display The number is odd Stop

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 ..ThenElse .End If Nest IF Select Case End Select

Flow Charts - Repetition

Types of Flow Control Statements


For..Next Do..While condition Do..Until condition

10

IF .. ThenElse
True Condition
?

Statement 1 One course-of-action

False Statement 2 Statement 3

Another course-ofaction
11

IF .. ThenElse - Example
Get Student Marks True

Marks>=50
?

Print Pass the Exam False

Print Fail the Exam

IF a Student obtained at least 50 marks he will pass the Exam , otherwise Fail.

12

IF .. ThenElse : 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

Get Marks

Nested IF - Example
True Print Distinction

Marks>80
?

False Marks>=50
?

True

Print Pass

False Print Fail

15

Get Marks

Nested IF - Example
True Print Pass

Marks>=50
?

False Marks>80
?

True

Print Distinction
This part will never get executed

False Print Fail

16

IF .. ThenElse : 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


Statement(s)

Condition can be at the end of

True

the loop

While condition False

I=1 Do Print I I=I+1 Loop while I < 5

20

Iterative Statements Loops


Or Condition can be at the start of the loop

While condition True Statement(s)

False

I=1 Do while I < 5 Print I I=I+1 Loop

21

Iterative Statements Loops


Statement (s)
False

Statement (s)
True

Do While
True

Do Until
False

Statement(s) (s) End Do Statement(s) (s)

Statement (s) End Do Statement (s)


22

Iterative Statements For Next


I=1 NO

I <=5 ?
YES Print I

For I= 1 To 5 Print I Next I


I=1 Do while I <= 5 Print I I=I+1 Loop

I=I+1

23

Iterative Statements For Next


I=1 NO

I <=5 ?
YES Print I

For I= 1 To 5 Print I Next I

I 1 2 3 4 5 6

I <= 5? yes yes yes yes yes No

Print I 1 2 3 4 5

I=I+1 2 3 4 5 6

I=I+1

23

Iterative Statements For Next


I=1 For I= 1 To 5 step 2 Print I Next I

I >5 ?
YES NO Print I

I <= 5?

Print I

I=I+2

yes
yes yes No

1
3 5

3
5 7

I=I+2

3 5 7

23

Iterative Statements Do..While Cond.


I=0

I=I+1

I=0 Do I=I+1 Print I Loop While I<5


i=I + 1 Print I 1 1 2 I<5 yes yes

Print I
Yes

I <5 ?

3
4 5

3
4 5

yes
yes No 24

No

Condition at the end of Loop

Iterative Statements Do..While Cond.


I=0
Condition at the top of Loop

I <5 ?

No
Yes

I=0 Do While I<5 I=I+1 Print I Loop


i 0 1 2 3 4 I<5 yes yes yes yes yes I = I + 1 Print I 1 2 3 4 5 1 2 3 4 5 25

I=I+1 Print I

no

Iterative Statements Do..Until Cond.


I=0
Condition at the top of Loop

I =5 ?

Yes
No

I=0 Do Until I=5 I=I+1 Print I Loop


i 0 1 2 3 4 I=5? no no no no no I = I + 1 Print I 1 2 3 4 5 1 2 3 4 5 26

I=I+1 Print I

yes

Iterative Statements Do..Until Cond.


I=0

I=I+1

I=0 Do I=I+1 Print I Loop Until I=5


I=I+1 Print I I=5 1 1 No 2 2 No 3 3 No 4 4 No 5 5 Yes
27

Print I
No

I =5 ? Yes

Condition at the end of Loop

Review Question (OL 2010 Q23)


Start

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

Set Required Temp (R) to 26 C Read Current Temp (C)


No

Display Cold

Is C>R?
Yes

Display Hot
End 28

Review Question (OL 2010 Q24)


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
Display Cold
Start

Set Required Temp (R) to 26 C Read Current Temp (C)


No

Not an Iterative pseudo code. Answer : B only

Is C>R?
Yes

Display Hot
End 29

Review Question (OL 2010 Q24)


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
Start

Set Required Temp (R) to 26 C Read Current Temp (C)


No

C, R Integer or Real C, R needs to compare Answer- Real

Display Cold

Is C>R?
Yes

Display Hot
End 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)


Start

-Read Set of Temp. (T) -Display Cold , If they are below 26. - Identify the correct sequence of labels Yes denoted by 1,2,3,4,5,6 of the flow chart

1
3

2 5 6
NO

END
32

Review Question (OL 2009 Q27)


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, Start

1
3

2
Yes

5 6
NO

END
33

Review Question (OL 2009 Q28)


What is the value output of Z?
X Y (X=Y) 1 1 Yes 1 2 No Z Y Loop Cnt. 2 2 1

Answer =2

Begin X=1 Y=1 While (X=Y) Z=X+Y Y=2 End While Display Z End
34

Exercise
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 Begin X=1 Y=3 While (X<=Y) Z=X+Y Y=Y-1 End While Display Z End

Answer =2, 3 times

35

Review Question (OL 2009 Q29)


If N is an Integer variable, how many times the loop will be executed.
M>5 Yes Yes . . N=10 M=6 N Loop Count 9 1 8 2 . .

N=10 M=6 Do While M>5 N=N-1 Loop


Replace M>5 with N>5. How many times the loop will be executed?
36

Answer =Never Ends

Review Question (OL 2011 Q22)


Which of the following is correct regarding the flowchart? (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

Add num to total Add 1 to counter


Yes

Is counter less than 3?

No

Display total
End 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.
Yes 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 Is counter less than 3? No Start

Set counter to zero Set total to zero


Get number num

Add num to total Add 1 to counter

Display total
End 38

Total=6

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

Add num to total Add 1 to counter


Yes Is counter less than 3? No

total = total +num

Display total
End 39

Review Question (OL 2011 Q24)


Which of the following statements correct regarding control structures?

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

Only (2) is correct


40

Review Question (OL 2011 Q25)


Consider the following statements regarding operators in a programming Language.

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

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.

Begin Set Total to Zero 2 10 2 For (Counter =To Step.) Total + Count Total=.. Next Counter End
42

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.

Begin Set Total to Zero 15 1 -2 For (Counter =To Step.) Total + Count Total=.. Next Counter End
43

Exercise
Write down the output generated by the following flow chart.

Start

Set X to 1

Display X Add 1 to X

12345

No

Is X greater than 5?
Yes
End 44

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?
Start

x=1
No Yes

Is x<=5?

y=1
Yes

Is y<=3?
No

Print
x,y

y=y+1

x=x+1

End

46

Start

x=1 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
No
Is x<=5? Yes

y=1
Yes
Is y<=3? No

Print
x,y

x=x+1

y=y+1

END

End

1,1 21 31 41 5,1

Output

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

Exercise : How many times the loop will be executed? What is the value of x after completing the loop?

5 times X=6

END

48

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
Which of the above statements are correct?
51

Default Operator Precedence


1. 2.

5+4^2/2 5 52+3

Default Operator Precedence


High

Exponentiation (^) Unary identity and negation (+, ) Multiplication and floating-point division (*, /) Integer division (\) Modulus arithmetic (Mod) Addition and subtraction (+, )

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)) =58 = -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

Anda mungkin juga menyukai