Chap1 Introductiontocomputing
Chap1 Introductiontocomputing
INTRODUCTION TO ALGORITHMS
AND FLOWCHARTS
§ Algorithms
§ Using Flowchart to describe Algorithm
§ Algorithm in pseudo-code
§ Branching in Algorithms
§ Loop in Algorithms
2
1. Algorithms
3
2. Using Flowchart to describe algorithm
Terminal
Input/output
Process Connector
Flowlines
Predefined process
Decision
Fig. 2. Start
Input Name,
Hours, Rate
Dislay
Name, Pay
End
5
Example 2.2: Calculate the average of 3 numbers
Fig. 3.
6
Example 2.3: How to compute profit and loss.
Fig. 4.
7
3. Algorithms in pseudo-code
Dislay
Name, Pay
8
End
4. Branching in algorithms
Pseudo-code of the
program:
Input A
if A > 0 then
calculate B = sqrt(A)
print B
else
Print “ A is negative”
endif
10
Branching
Example 4.2: Solving
the quadratic
equation:
ax2 + bx +c = 0
11
5. Loops on Algorithms
12
Example 5.1 Note:
NUM ¬ NUM + 1 means
Start
old value of NUM + 1
NUM ¬ 4 becomes new value of NUM.
SQNUM ¬ NUM2
No
SQNUM¬ NUM2
NUM> 9?
Print NUM, SQNUM
Yes
NUM ¬ NUM + 1
STOP while (NUM <= 9)
13
Example 5.2
The algorithm sums
all the even numbers
between 1 and 20
inclusive and then
displays then sum.
Programming Fundamentals 14
Pseudo-code of Example 5.2
sum = 0
count = 1
do
if count is even then
sum = sum + count
endif
count = count + 1
while count <= 20
Display sum
15
Exercise
16
Ans.
Input a,b,c
if a > b then
if a > c then
print a
endif
else
if b > c then
print b
else
print c
endif
endif
17