Module 8
Module 8
PROGRAMMING
MODULE 8
ALGORITHM &
FLOWCHARTS
ALGORITHMS
An algorithm is a list of instructions for carrying out some process step by step. A recipe
in a cookbook is an excellent example of an algorithm. The preparation of a complicated dish is
broken down into simple steps that every person experienced in cooking can understand.
Another good example of an algorithm is the choreography for a classical ballet .An intricate
dance is broken down into a succession of basic steps and position of ballet. The number of
these basic steps and position is every small but putting them together in different ways, and
endless variety of dances can be devised.
FLOWCHARTS
2
ADVANTAGES AND LIMITATION OF FLOWCHARTS
There are advantages and limitation in the use of flowcharts. Among its advantages
are:
1. Since flowcharts are language independent they can be learned and applied without
formal knowledge of a programming language.
2. It enforces users to give attention to significant matters over the less significant ones.
3. Being graphically portrayed, flowcharts provide an alternative to the usual narrative
description of a system or a program.
4. Flowcharts are easier to understand than a program written in a particular programming
language.
Types of Flowcharts
The two major types of flowcharts are the program and system flowcharts.
1. Program flowchart
A program flowchart describes graphically in detail the logical operations and
steps within a program and the sequence in which these steps are to be
executed for the transformation of data to produce the needed output.
3
2. System Flowchart
A graphic representation of the procedures involved in converting data on input
media to data in output form. It illustrates which data is used or produced at
various points in a sequence of operations. System flowcharts portray the
interaction among data, hardware, and personnel. The emphasis is on the media
used and the work stations through which the data passes. Since it illustrates
more on the components of the system used, little is shown about how
processing will be accomplished.
Flowcharting Template
A special plastic or metallic card called a template is used to draw the symbols in a
flowchart .The template is used to stencil the required symbols .It contain both system and
program flowcharting symbols as cut out forms.
2. Processing Symbol
3. Decision Symbol
4
Denotes a point in the program where more than one path can be taken.
4. Preparation Symbol
5. Terminal Symbol
Used to designate the beginning and the end of a program, or a point of interruption.
Represents a named operation or programmed step not explicitly detailed in the program
flowchart.
7. On-page Connector
5
9. Off-page Connector
Used instead of the on-page connector to designate entry to or exit from a page when a
flowchart requires more than one page.
10. Flowlines
Used to show reading order or sequence in which flowchart symbols are to be read.
Drawing flowcharts
It has been explained in the previous lesson that a flowchart is a plan on how a problem or
any activity with which we are faced is to be solved. The solution to any problem or undertaking
provide there is a possible solution ,regardless of whether the problem is to perform an activity
or arrive at a mathematical solution can be properly organized into a logical sequences of steps
as portrayed by a flowchart.
Sample flowchart: The figure shows how some people spend their working days in the office.
6
NOTATIONS USED IN FLOWCHARTING
+ Addition = or EQ Equal to
( ) Grouping Y Yes
| Logical or N No
b blank
7
A suitable algorithm for this problem for this problem would be as follows:
STEP 1. Define the value of the radius (R) and Pi (equal to 3.1416).
STEP 2. Calculate the area of the circle, AREA=PI*R*R.
STEP 3. Print out the radius and its corresponding area.
8
2. Example B: Given three numbers A, B, and C. Draw a flowchart to compute and print out
the sum, the average, and the product of these values.
9
3. Example C: The ABC Manufacturing Company plans to give a year-end bonus to each
of its employees. Draw a flowchart which will compute the bonus of an employee.
Consider the following criteria: If the employee’s monthly salary is less than P1000.00,
the bonus is 50% of the salary; for employees with salaries greater than P1000.00, the
bonus is P1000.00. Print out the name and the corresponding bonus of an employee.
10
4. Example C: Given two numbers X and Y. Draw a flowchart to determine the difference
between X and Y. If X – Y is negative, compute R=X+Y; If X – Y is zero, compute
R=2X+2Y; and If X – Y is negative, compute R=X+Y; If X – Y is positive, compute
R=X*Y.
11
LOOPS AND COUNTERS
Frequently a problem will require that a process be repeated a certain number of times.
One way to accomplish this repetition is for the algorithm to transfer control of logic flow from
the end of the process back to its beginning .The flowchart of such an algorithm will contain a
loop or a sequence of steps to be executed a specified number of times.
Looping is used when it is desired to make the same calculation on more than one set of
data. It consists of repeating program or a selection of a and substituting new data for each
repetition .Program loops play an important part in data processing since they provide efficient
means of processing various sets of data with same program segment.
COUNTERS
Somewhere in any loop a loop-terminating condition must reside, and the loop is then
executed until the decision is made by the loop terminating condition to exit from the loop.
A counter is set up in a program loop to keep track of the number of times the program
segment is repeated. The program can then be terminated after the completion of a
predetermined number of passes. Prior to the execution of counting, all counters should be
initialized (usually to zero) before the loop is executed.
ILLUSTRATIVE PROBLEMS
The following problems illustrative the principle of loops and their control by counters
1. Example A: The initial value of the radius of a circle is equal to one unit and each
succeeding radius is one unit greater than the value before it. Draw a flowchart to
compute the area of the circle starting with R=5.0, then print out each radius and the
corresponding area of the circle.
The algorithm for satisfying the requirements of the above stated problem could be as
follows:
12
STEP 1. Initialize the value of R to 1 and the value of PI to 3.1416.
STEP 2. Compute area by multiplying PI by the square area.
STEP 3. Print out the value of R and the computed area.
STEP 4. Increment the value of R by 1.
STEP 5. Test if R is less than or equal to 5.
STEP 6. If R is less than or equal to 5, loop back and repeat steps 2 through 5.
However, if R is greater than 5, stop processing.
R=1.0
PI=3.1416
13
2. Example B: Given a set of 5 numbers that include both positive and negative data
values. Draw a flowchart to read in these values one at a time and count then number of
positive values (including zero values) and the number of negative values found in the
set. After the required values are determined, print out these counted values.
14