Chapter 1
INTRODUCTION TO ALGORITHMS
AND FLOWCHARTS
Introduction to computer science 1
Chapter 1
Algorithms
Using Flowchart to describe Algorithm
Algorithm in pseudo-code
Branching in Algorithms
Loop in Algorithms
2
1. Algorithms
An algorithm is defined as a step-by-step sequence
of instructions that describes how the data are to be
processed to produce the desired outputs.
In essence, an algorithm answers the question:
“What method will you use to solve the problem?”.
You can describe an algorithm by using flowchart
symbols. By that way, you obtain a flowchart.
Flow chart is an outline of the basic structure or
logic of the program.
Another way to describe an algorithm is using
pseudocode.
3
2. Using Flowchart to describe algorithm
Terminal
Input/output
Process Connector
Flowlines
Predefined process
Decision
Fig 1. Flowchart symbols 4
Example 2.1. Calculate the payment of an employee
Fig. 2. Start
Input Name,
Hours, Rate
Calculate Note: Name, Hours
Pay Hours Rate and Pay are variables
in the program.
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
You also can use English-like phases to desribe an
algorithm. In this case, the description is called
pseudocode.
Example: The pseudocode for the program with the
flowchart in Fig. 2.
Input the three values into the variables Name, Hours,
Rate.
Calculate Pay = Hours Rate.
Display Name and Pay.
8
4. Branching in algorithms
If test-condition then S1 If test-condition then S1
endif else S2
endif
Example 2.1
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
Pseudo-code of the program:
Input a, b, c
Calculate del = b2 + 4.a.c
if del >0 then
x1 = (-b + sqrt(del)/(2.a)
x2 = (-b - sqrt(del)/(2.a)
print x1, x2
else if del =0 then
x = -b/(2.a)
else
print “No solution”
endif
11
5. Loops on Algorithms
Many problems require repetition capability, in which
the same calculation or sequence of instructions is
repeated, over and over, using different sets of data.
Loop is a very important concept in programming.
Example 5.1. Write a program to do the task: Print a
list of the numbers from 4 to 9, next to each number,
print the square of the number.
12
Example 5.1 Note:
NUM NUM + 1 means
Start
old value of NUM + 1
NUM 4 becomes new value of NUM.
SQNUM NUM2
The algorithm can be described
Print
NUM, SQNUM
in pseudocode as follows:
NUM 4
NUM NUM + 1 do
No
SQNUM NUM2
NUM> 9?
Print NUM, SQNUM
NUM NUM + 1
Yes
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
Give the flowchart of
the program which
finds the largest
among three different
numbers. Write the
pseudo-code for the
flowchart.
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