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
n 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.
n In essence, an algorithm answers the question:
“What method will you use to solve the problem?”.
n You can describe an algorithm by using flowchart
symbols. By that way, you obtain a flowchart.
n Flow chart is an outline of the basic structure or
logic of the program.
n 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
n You also can use English-like phrases to describe an
algorithm. In this case, the description is called
pseudocode.
n Example: The pseudocode for the program with the
flowchart in Fig. 2.
Input the three values into the variables Name, Hours,
Rate. Start
Calculate Pay = Hours ´ Rate.
Input Name,
Hours, Rate
Display Name and Pay. Calculate
Pay ¬ Hours ´ Rate
Dislay
Name, Pay
8
End
4. Branching in algorithms
If test-condition then S 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
n Many problems require repetition capability, in which
the same calculation or sequence of instructions is
repeated, over and over, using different sets of data.
n Loop is a very important concept in programming.
n 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
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 the 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 1
n Given flowchart of
program finding the
largest among 3
different numbers.
n Write the pseudo-
code.
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
Exercise 2
n Given pseudo-code of program finding the sum of 5
numbers. Draw the flowchart.
1. Initialize sum = 0 and count = 0
2. Enter n
3. Find sum + n and assign it to sum
4. Increment count by 1
5. Is count < 5?
6. If YES go to step 2
else
Print sum
Programming Fundamentals 18
Exercise 3
n Write pseudo-code and draw a flowchart to read length
and width of a rectangle and calculate its area and
perimeter.
Programming Fundamentals 19
Exercise 4
n Write pseudo-code and draw a flowchart to calculate the
factorial of N.
N! = 1.2….(N-1).N
Programming Fundamentals 21
Exercise 5
n Draw a flowchart for log in function:
q Input user name, password
q Username is not existed: Invalid Username à Re-try
q Password is incorrect: Wrong Password à Re-try
q Has 5 attempts: lock account
q Correct Username and Password: Access to
homepage.
Programming Fundamentals 23
Exercise 6
n Write pseudo-code and draw a flowchart to find the
largest number in a list of numbers.
Programming Fundamentals 25