Professional Documents
Culture Documents
1 CS101 Chapter 6
Chapter 6
Algorithms and Flowcharts
2 CS101 Chapter 6
Start/Stop
Input/Output
Connector
Process, Instruction
3 CS101 Chapter 6
6.4 Flowchart Examples
6.4.1 Example 1:
Example in words: Go straight on to the traffic light, turn left and take the third
turning on the right. If the red gate is open then go through it. Otherwise go through the
green light.
As a flowchart:
YES
NO
4 CS101 Chapter 6
6.4.2 Example 2, Sequential Structure Flowchart:
Given the radius and height for a cylinder find the volume and surface area. The volume
(V) of a cylinder is V = π r2h and the surface area (A) is A=2π rh.
Figure 10.3:
Flowchart for finding the
volume and surface area of
a cylinder
5 CS101 Chapter 6
Example 3:( Sequential Structure Flowchart):
Start
Read n1 , n2
Sum = n1+n2
Print Sum
End
Figure 10.4: Flowchart for the program to calculate the sum of two integers.
6 CS101 Chapter 6
6.4.3 Example 4, Selection/ Branching Flowchart Structure:
Draw a flowchart to determine what form to use to file US income tax return. Make your
decision base on marital status, age, number of independents and taxable income less than
$50,000.
7 CS101 Chapter 6
Example5, (Selection/ Branching Flowchart Structure):
Write an algorithm to change a numeric grade to pass / fail grade .then draw a
flowchart of that algorithm.
1- Start
2- Read a number num
3- If the number num is grater than or equal to 60
Then
3.1 Set the grade to pass
Else
3.2 Set the grade to fail
4- Print grade
5- End Start
Read num
F T
num
>=60
Print grade
Figure 10.6: Flowchart for Example 5
End
8 CS101 Chapter 6
6.4.4 Example 6, Repetition/ Looping Flowchart Structure:
Draw a flowchart to show how to convert between degrees Celsius and degrees Fahrenheit
9 CS101 Chapter 6
Example 7 , Write an algorithm to calculate the summation for a list of
numbers. then draw a flowchart of that algorithm:
2- The loop, which in each iteration adds a new number to the sum.
Start
Sum = 0
F
More
numbers ?
T
Sum = Sum + num
Print Sum
End
Figure 10.8: Flowchart for calculating the summation for a list of numbers.
10 CS101 Chapter 6
Figure 10.8: Flowchart for calculating the average score.
11 CS101 Chapter 6
6.5 Pseudo-Code:
Pseudo-code cannot be compiled nor executed, and there are no real formatting or syntax
rules. It is simply one step - an important one - in producing the final code. The benefit of
pseudo-code is that it enables the programmer to concentrate on the algorithms without
worrying about all the syntactic details of a particular programming language. In fact, you
can write pseudo-code without even knowing what programming language you will use for
the final implementation.
Pseudo-code should not include keywords in any specific computer languages. It should be
written as a list of consecutive phrases. You should not use flowcharting symbols, but you
can draw arrows to show looping processes. Indentation can be used to show the logic in
pseudo-code as well. One programmer should be able to take another programmer's
pseudo-code and generate a program based on that pseudo-code. Caution never write your
program before either flowcharting or pseudo-coding it.
The programming process is a complicated one. You must first understand the program
specifications, of course, then you need to organize your thoughts and create the program.
This is a difficult task when the program is not trivial (i.e. easy). You must break the main
tasks that must be accomplished into smaller ones in order to be able to eventually write
fully developed code. Writing pseudo-code WILL save you time later during the
construction and testing phase of a program's development.
Sample Pseudo-code:
When you begin to write pseudo-code for a specific program, you may want to make a list
of the main tasks that must be accomplished on a piece of scratch paper. Then, focus on
each of those tasks. Generally, you should try to break each main task down into very small
tasks that can each be explained with a short phrase. There may eventually be a one-to-one
correlation between the lines of pseudo-code and the lines of the code that you write after
you have finished pseudo-coding.
Example1 in Pseudo-Code:
12 CS101 Chapter 6
Go straight on to the traffic lights;
Turn left;
Take the third turning on the right;
If the red gate is open then
go through the red gate
else
go through the green gate
end if;
13 CS101 Chapter 6