# Chapter 11 Algorithms and Flowcharts

Document Sample

```					                             Table of Contents
Chapter 6: Algorithms and Flowcharts

6.1 Steps in Using a Computer as a Problem-Solving Tool
6.2 Definition of Algorithm and Flowchart
6.3 Algorithmic Tools
6.4 Flowchart Examples
6.4.1 Example 1
6.4.2 Example 2, 3 Sequential Structure Flowchart
6.4.3 Example 4, 5 Selection/ Branching Flowchart Structure
6.4.4 Example 6, 7 Repetition/ Looping Flowchart Structure
6.4.5 Example 8, Combination of Flowchart Structures

6.5 Pseudo-Code

1                         CS101 Chapter 6
Chapter 6
Algorithms and Flowcharts

6.1 Steps in Using a Computer as a Problem-Solving Tool:
1.   Develop an Algorithm and a Flowchart.
2.   Write the program in a computer language. (i.e. C++, Fortran, C)
3.   Enter the program into the computer.
4.   Test and debug the program.
5.   Run the program, input data, and get the results from the computer.

6.2 Definition of Algorithm and Flowchart:
    An Algorithm is just a detailed sequence of simple steps that are needed to solve a
problem.
    An algorithm is a sequence of instructions, which are followed in order to complete
a task. An algorithm can be presented or written in words, in a program (structured
code) for a computer or as a flowchart.
    A Flowchart is a graphical representation of an algorithm.

6.3 Algorithmic Tools:
    Flowchart: (American National Standards Institute -- ANSI symbols:)
    Flowchart is a pictorial representation of program logic; it contains basic symbols
used in the representation. These are:
o Parallelogram for input/output
o Oval for start and stop
o Rectangle for processing
o Diamond for decision
o Hexagon for preparations and loops
o Circle for connector
o Arrow for flow directions.

2                         CS101 Chapter 6
Start/Stop

Question, Decision (Use in Branching)

Input/Output

Preparation and loops

Connector

Process, Instruction

Comments, Explanations, or Definitions.

Figure 10.1: Basic Flowchart Symbols

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

Figure 10.2: Flowchart for Example1

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):

Draw a flowchart of the program to calculate the sum of two integers.

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.

Figure 10.5: Flowchart for Example 4

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

grade = "Fail"                                   grade = "pass"

Print grade

End

Figure 10.6: Flowchart for Example 5

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

Figure 10.7: Flowchart for Converting from Degrees Celsius to 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:

1- Initialization of the Sum at the beginning .

2- The loop, which in each iteration adds a new number to the sum.

3- Returning the result after exiting form the loop.

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
6.4.5 Example 8, Combination of Flowchart Structures:
Draw a flowchart to show how to add the scores of 25 exams.

Figure 10.8: Flowchart for calculating the average score.

11                        CS101 Chapter 6
6.5 Pseudo-Code:
Pseudo-code is a cryptic English-like narrative of the program logic. It is an outline of a
program or algorithm, written in a form that can easily be converted into real programming
statements ... yet is not written in programming statements.

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:

Write a pseudo-code to add two numbers:

Input number1, number2
Sum = number1 + number2
Display heading message “Number1, Number2, Sum”
Display number1, number2, Sum
End

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:

Go straight on to the traffic lights;
Turn left;

12                         CS101 Chapter 6
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

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 23706 posted: 4/16/2010 language: English pages: 13
Jun Wang Dr
About Some of Those documents come from internet for research purpose,if you have the copyrights of one of them,tell me by mail vixychina@gmail.com.Thank you!