0% found this document useful (0 votes)
114 views

Module 8

Uploaded by

24-07156
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

Module 8

Uploaded by

24-07156
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

COMPUTER

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.

Also by means of algorithm a computer can control a manufacturing process or


coordinate the reservations of an airline as they are received from ticket offices all over the
country .Algorithms for such large-scale processes are, of course very complex but they are
built up from pieces as in the example given below.

If we can devise an algorithm for a process, we can usually do so in many different


ways, here is one algorithm for the usual process of changing a flat tire.

1. Jack up the car


2. Unscrew the lugs
3. Remove the wheel
4. Put on the spare
5. Screw on the lugs
6. Jack the car down

FLOWCHARTS

A flowchart is a diagram representing the logical sequence in which a combination of


steps or operations is to be performed. It consists of labelled geometrical symbols that are
interconnected to provide a pictorial representation of a data processing procedure. A flowchart
is actually a visual representation of an algorithm.

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.

Among its limitations are:


1. Flowcharts do not represent a programming language and are more of a person-to-
person than person-to-computers means of communication .A computer cannot accept a
program describe in flowcharting form.
2. Since thinking in graphic terms is not normal flowchart cannot be viewed as a natural
means of communication
3. Certain details often require along sequence of interconnected symbol which could
easily be described in just a few lines of explanations.
4. It does not convey why a given set of operation is made.
5. Flowchart do nat5 highlight the important details since each step receives as much
attention in a flowchart as any other.

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.

1. Input / Output Symbol

Represents an instruction to a input or an output device.

2. Processing Symbol

Represents a group of program instructions that perform a processing function of the


program such as to perform arithmetic operations, or to compare, sort, etc.

3. Decision Symbol

4
Denotes a point in the program where more than one path can be taken.

4. Preparation Symbol

Used to represent an instruction or group of instruction that will alter, or modify a


program’s course of execution.

5. Terminal Symbol

Used to designate the beginning and the end of a program, or a point of interruption.

6. Predefined Process Symbol

Represents a named operation or programmed step not explicitly detailed in the program
flowchart.

7. On-page Connector

Non-processing symbol which is used to connect one part of a flowchart to another


without drawing flowlines.

8. Flow Direction Indicators

Show the direction of processing or data flow.

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

NOTATION MEANING NOTATION MEANING

+ Addition = or EQ Equal to

- Subtraction ‘ > or GT Greater than

* Multiplication < or LT Less than

/ Division < > or ≠ or NE Not equal to

** or Π Exponentation ≥ or GE Greater than or Equal to

: Comparison < or LE Less than or Equal to

( ) Grouping Y Yes

| Logical or N No

& Logical and EOF End of File

b blank

Illustrative Problems on Simple Logic Flow

The following problems in flowcharting illustrative simple logic flow.


1. Example A: The radius of a circle is equal to one unit. Draw a flowchart to compute the
corresponding area of the circle and print out the value of the radius and the area.

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.

The algorithm for this problem could be:


STEP 1. Read in the values of A,B, and C.
STEP 2. Determine the sum of the three values read.
STEP 3. Compute the average by dividing the sum by 3.
STEP 4. Multiply the first value by second value then by the third value to
determine the product of the three values.
STEP 5. Print out the computed values (sum, average, and the product).

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.

The algorithm for this problem could be:


STEP 1. Read in the employee’s name and salary.
STEP 2. Test if the employee’s monthly salary is less than P1000.00.
STEP 3. If the salary is less than P1000.00, the bonus is 50% of the salary.
However, if the salary is more than P1000.00, the bonus id P1000.00
STEP 4. Print out the employee’s name and bonus

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.

Following is the algorithm:


STEP 1. Read in the the values of X and Y.
STEP 2. Subtract Y from X.
STEP 3. 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
STEP 4. Print out the values of X, Y, and R.

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.

The algorithm of such a problem may be summarized as follows:


STEP 1. Set up two counters and initialize their values to zero (one counter, NNEG, is
used for counting the number of negative values and the other, NPOS, for
counting positive values).
STEP 2. Input the first value, NO.
STEP 3. Test and determine if the value read is positive or negative
STEP 4. If the value of NO is negative (or less than zero), increment the NNEG counter
by one. However, if it is positive (or equal to or greater than zero), increment
the NPOS counter.
STEP 5. Determine the number of values encountered (NVAL) by adding the two
counters (NNEG and NPOS).
STEP 6. Test if NVAL is equal to 5.
STEP 7. If the value of NVAL is not equal to 5, repeat steps 2 through 5. However, if it is
equal to 5, print out the current value stored in these counters (NNEG and
NPOS).

14

You might also like