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?
Input/ Output
Decision
Connector
Flow Direction
Is remainder(X/2) =0?
10
IF .. ThenElse
True Condition
?
Another course-ofaction
11
IF .. ThenElse - Example
Get Student Marks True
Marks>=50
?
IF a Student obtained at least 50 marks he will pass the Exam , otherwise Fail.
12
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
15
Get Marks
Nested IF - Example
True Print Pass
Marks>=50
?
False Marks>80
?
True
Print Distinction
This part will never get executed
16
17
Case can evaluate only one variable whereas Nest IF can evaluate multiple variables. No special Flow chart symbol
18
True
the loop
20
False
21
Statement (s)
True
Do While
True
Do Until
False
I <=5 ?
YES Print I
I=I+1
23
I <=5 ?
YES Print I
I 1 2 3 4 5 6
Print I 1 2 3 4 5
I=I+1 2 3 4 5 6
I=I+1
23
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
I=I+1
Print I
Yes
I <5 ?
3
4 5
3
4 5
yes
yes No 24
No
I <5 ?
No
Yes
I=I+1 Print I
no
I =5 ?
Yes
No
I=I+1 Print I
yes
I=I+1
Print I
No
I =5 ? 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
Display Cold
Is C>R?
Yes
Display Hot
End 28
Is C>R?
Yes
Display Hot
End 29
Display Cold
Is C>R?
Yes
Display Hot
End 30
(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
-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
1
3
2
Yes
5 6
NO
END
33
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
35
No
Display total
End 37
Display total
End 38
Total=6
Display total
End 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.
(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.
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
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
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
50
5+4^2/2 5 52+3
Exponentiation (^) Unary identity and negation (+, ) Multiplication and floating-point division (*, /) Integer division (\) Modulus arithmetic (Mod) Addition and subtraction (+, )
Low
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