You are on page 1of 28

CHAPTER II

ALGORITHM AND FLOWCHART


INTRODUCTION
After an overview of programming, this chapter covers algorithm and
flowcharting concepts. The chapter begins with a discussion of algorithm and the
different methods of specifying algorithm. The student will learn how to formulate
correct program logic using different methods of algorithm.

LESSON 1
ALGORITHM

Algorithm Defined

It is a set of instructions of how to carry out a process. An algorithm lists the


steps that must be followed to complete the process and to reach the
solution.

A finite set of an instruction that specifies a sequence of operation to be


carried out in order to solve a specific problem.

An unambiguous procedure specifying a finite number of steps to be taken.

B. Properties of Algorithm
1. Finiteness - there is an exact number of steps to be taken and has an end.
2. Absence of Ambiguity - means that every instruction is precisely described and
clearly specified.
3. Sequence of Execution instructions are performed from top to bottom.
4. Input and Output - defined the unknowns of the problem is specified and with the
expected outcome.
5. Effectiveness- the solution prescribed is guaranteed to give a correct answer and that
the specified process is faithfully carried out.
6. Scope Definition - applies to a specific problem or class of problem.

C. Methods of Specifying Algorithm


1. Pseudocode

Specifies the steps of algorithm using essentially natural language of


superimposed control structure.

Pseudo means pretend or false Pseudo Code is pretend or false


computer code; generic English-like terms that are somewhat like computer
code.

It is not as standardized as flowcharts, and does not facilitate the breaking


down of problems as well as a flowchart does.

2. Flowchart - a traditional graphical tool with standardized symbols. Show the


sequence of steps in an algorithm.

D. Examples
1. Write an algorithm to determine a students final grade and indicate whether it is
passing or failing. The final grade is calculated as the average of four marks.
Pseudocode:
Input a set of 4 marks
Calculate their average by summing and dividing by 4
if average is below 50
Print FAIL
else
Print PASS

Detailed Algorithm
Step 1: Input M1,M2,M3,M4
Step 2:

GRADE (M1+M2+M3+M4)/4

Step 3:

if (GRADE < 50) then

Print FAIL

else
Print PASS
endif

Write an algorithm to convert the length in feet to centimeter.


Pseudocode:
Input the length in feet (Lft)
Calculate the length in cm (Lcm) by multiplying LFT with 30
Print length in cm (LCM)
Detailed Algorithm
Step 1: Input Lft
Step 2: Lcm Lft x 30
Step 3: Print Lcm

LESSON 2
FLOWCHART

Flowchart Defined

It is a visual representation of an algorithm

It uses easy-to-understand symbols to represent actions on data and the flow


of data

It consists of a sequence of instructions linked together by arrows to show the


order in which the instructions must be carried out.

A flowchart is basically the plan to be followed when the program is written. It


acts like a road map for a programmer and guides him in proceeding from the
starting point to the final point while writing a computer program.

The process of drawing a flowchart for an algorithm is referred to as


flowcharting.

B. Flowchart Symbols
Only a few symbols are needed to indicate the necessary operations in a flowchart.
These symbols have been standardized by the American National Standards Institute
(ANSI).

Processing

A processing symbol is used in a flowchart to represent arithmetic and data movement


instructions. Thus, all arithmetic processes such as adding, subtracting, multiplying and
dividing are shown by a processing symbol. The logical process of moving data from
one location of the main memory to another is also denoted by this symbol.

Flow lines

Flow lines with arrowheads are used to indicate the flow of operation, that is, the exact
sequence in which the instructions are to be executed. The normal flow of flowchart is
from top to bottom and left to right. Arrowheads are required only when the normal top
to bottom flow is not to be followed.

Decision

The decision symbol is used in a flowchart to indicate a point at which a decision has to
be made and a branch to one of two or more alternative points is possible.

Connector

If a flowchart becomes very long, the flow lines start crisscrossing at many places that
causes confusion and reduces the clarity of the flowchart. Moreover, there are instances
when a flowchart becomes too long to fit in a single page and the use of flow lines
becomes impossible. Thus, whenever a flowchart becomes too complex that the
number and direction of flow lines is confusing or it spreads over more than one page, it
is useful to utilize the connector symbol as a substitute for flow lines. This symbol
represents an entry from, or an exit to another part of the flowchart. A connector symbol
is represented by a circle and a letter or digit is placed within the circle to indicate the
link. A pair of identically labeled connector symbols is commonly used to indicate a
continued flow when the use of a line is confusing.

Name

Symbols

Representation

Terminator (terminal)

Start or end of the


program

Preparation

Signifies the preparation


of data

Data (input/output)

Input or Output operation

Process

Computational steps or
processing function of a
program

Decision

Decision making and


branching

On-page Connector

Connector or joining of
two parts of a program

Off-page Connector

Designates entry or exit


from one page
Indicates the flow of
operation

Flow lines

C. Flowcharting Guidelines
The following are some guidelines in flowcharting:

In drawing a proper flowchart, all necessary requirements should be listed out in


logical order.

The flowchart should be clear, neat and easy to follow. There should not be any
room for ambiguity in understanding the flowchart.

The usual direction of the flow of a procedure or system is from left to right or top
to bottom.

Only one flow line should come out from a process symbol.

or

Only one flow line should enter a decision symbol, but two or three flow lines,
one for each possible answer, should leave the decision symbol.

Only one flow line is used in conjunction with terminal symbol.

If the flowchart becomes complex, it is better to use connector symbols to reduce


the number of flow lines. Avoid the intersection of flow lines if you want to make it
more effective and better way of communication.

Ensure that the flowchart has a logical start and finish.

It is useful to test the validity of the flowchart by passing through it with a simple
test data.

D. Advantages and Limitations of using Flowcharts


The benefits of flowcharts are as follows:

Communication: Flowcharts are better way of communicating the logic of a


system to all concerned.

Effective analysis: With the help of flowchart, problem can be analyzed in more
effective way.

Proper documentation: Program flowcharts serve as a good program


documentation, which is needed for various purposes.

Efficient Coding: The flowcharts act as a guide or blueprint during the systems
analysis and program development phase.

Proper Debugging: The flowchart helps in debugging process.

Efficient Program Maintenance: The maintenance of operating program becomes


easy with the help of flowchart. It helps the programmer to put efforts more
efficiently on that part

The limitations of flowcharts are as follows:

Complex logic: Sometimes, the program logic is quite complicated. In that case,
flowchart becomes complex and clumsy.

Alterations and Modifications: If alterations are required the flowchart may require
re-drawing completely.

Reproduction: As the flowchart symbols cannot be typed, reproduction of


flowchart becomes a problem.

The essentials of what is done can easily be lost in the technical details of how it
is done.

E. Basic Control Structures


1. Sequence process is executed from one to another in a straightforward manner.

Examples:
1. Draw a flowchart that will accept and display a number. Write its equivalent algorithm.

Algorithm

Step 1. Read in the value of A


Step 2. Print the value of A.

Algorithm

Step 1. Initialize sum(S) and product(P) into 0.


Step 2. Read in the values of A and B.
Step 3. Compute sum(S) by adding A and B then compute product(P) by multiplying A
and B.
Step 4. Print the value of S and P.
2. Draw a flowchart that will compute and display the sum and product of two numbers.
Write its equivalent algorithm.

Algorithm

Step 1. Initialize Area(A) into 0.


Step 2. Input the width(W) and length(L) of a rectangle.
Step 3. Calculate the A by multiplying W and L.
Step 4. Print the value of A.
END
Print
A
END
A=LxW
Input
W, L
BEGIN
3. Write an algorithm and draw a flowchart that will read the two sides of a rectangle
calculate and print its area. Formula: Area = Length x Width.

2. Selection - Once the condition is evaluated, the control flows into one of two paths.
Once the conditional execution is finished, the flows rejoin before leaving the structure.
As an alternative, the "Question" could be replaced with a Boolean expression and the
branches labelled "TRUE" and "FALSE".

Operators Commonly Used in Flowcharting


A. Arithmetic Operators
Operators

Meaning

Addition

Subtraction

Multiplication

Division

B. Relational Operators
Operators

Meaning

Equal

>

Greater than

<

Less than

<>

Not equal

>

Greater than or Equal to

<

Less than or Equal to

C. Logical Operators

Operator

Meaning

&&

AND

II

OR

NOT

Examples:
Algorithm
Step 1. Read in the values of x and y.
Step 2. Test if x is greater than y.
Step 3. If x is greater than y, x is higher. However, if x is less than y, y is higher.
Step 4. Print the number and the remark Higher.
1. Draw a flowchart that will input values for x and y. Compare two values inputted and
print which of the values is higher including the remark Higher. Write its equivalent
algorithm.

2. Draw a flowchart that will determine the students final grade and indicate weather it
is passing or failing. The final grade is calculated as the average of four marks.
Algorithm
Step 1. Initialize Grade into 0.
Step 2. Input a set of 4 marks (M1, M2, M3, M4).
Step 3. Calculate their average(Grade) by summing and dividing by 4.
Step 4. If average(Grade) is below 50
Print FAIL
else
Print PASS

JLB Manufacturing Company plans to give a year-end bonus to each of its


employee. Draw a flowchart will compute the bonus of an employee. Consider the
following conditions: If the employees monthly salary is less than 10,000 pesos, the
bonus is 25% of the salary. If the employees monthly salary is greater than 10,000
pesos, the bonus is 3,000 pesos. Print the name and the corresponding bonus for
each employee. Write the equivalent algorithm.

Algorithm
Step 1. Initialize bonus to 0.
Step 2. Read in employees name and salary.
Step 3. Test if employees salary is less than 10,000.
Step 4. If salary < 10,000 then
Bonus = salary * 25 %
Else
Bonus = 3,000
Step 5. Print the employees name and bonus.

Construct a flowchart that will accept the evaluation score of a faculty and determine
its equivalent remarks. Print the name of the faculty and the remarks obtained. Write
the equivalent algorithm. Remarks are based on the following criteria:
4.50 5.00

Outstanding

4.00 4.49

Very Satisfactory

3.50 3.99

Satisfactory

3.00 3.49

Needs Improvement

2.99 below

Poor

Algorithm
Step 1. Initialize Rem into space or blanks.
Step 2. Read in the values of Name and Score.
Step 3. Test the score if it is greater than or equal to 4.50.
Step 4. If the score is greater than or equal to 4.50, Rem is Outstanding. However, if
the score is less than 4.50, do step 5.
Step 5. Test the score if it is greater than or equal to 4.00.
Step 6. If the score is greater than or equal to 4.00, Rem is Very Satisfactory.
However, if the score is less than 4.00, do step 7.

Step 7. Test the score if it is greater than or equal to 3.50.


Step 8. If the score is greater than or equal to 3.50, Rem is Satisfactory. However, if
the score is less than 3.50, do step 9.
Step 9. Test the score if it is greater than or equal to 3.00.
Step 10. If the score is greater than or equal to 3.00, Rem is Needs Improvement.
However, if the score is less than 3.00, Rem is Poor.
Step 11. Print the name and Rem.

3. Repetition - Computers are particularly well suited to applications in which


operations are repeated many times. If the same task is repeated over and over again a
loop can be used to reduce program size and complexity.

Loops and Counter


Looping
Used when it is desired to make the same calculation of more than one set of data. It
consists of repeating a program, or a section of program and substituting new data
for each repetition.
Counter
Is a setup in a program loop to keep track of a number of times the program
segment is repeated. The program can then be terminated after the completion of a
predetermined number of passes.
Steps in Loop Control
Initialization
The value of counter is initially set equal to zero or one.
Test for limit condition
Before the logic flow gets out of a loop, a loop terminating condition must first be
satisfied.
Incrementation
Often each loop is executed, 1 is added to the counter. Thus counter reflects the
number of times the operation has been performed.

Examples:
1. Construct a flowchart that will count from 1 to 10 and print each number counted using
the looping structure. Write the equivalent algorithm.

Algorithm

Step 1. Initialize the value of N to 0.


Step 2. Test if N is less than 10.
Step 3. If N is less than 10, add 1 to the value of N, print the value then go back to step
2. However, if N is greater than 10, stop processing.

2. The initial value of the radius of a circle is equal to 1 unit and each succeeding radius
is 1 unit greater than the value before it. Draw a flowchart to compute the area of a
circle starting with radius=1 up to radius = 5, then print each radius and the
corresponding area of a circle.
Formula: area = pi x r2

Algorithm
Step 1. Initialize the value of radius(r) to 1 and the value of pi to 3.1416.
Step 2. Compute the area by multiplying pi to the square of r.
Step 3. Print the value of r and the 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 to 5. However, if r is
greater than 5, stop processing.

3. Write an algorithm and draw a flowchart to calculate 2 4 using a loop approach?

Print
Product

NAME: ______________________________
Course/Year/Section : _____________ ____

SCORE: ________
Date : __________

CONCEPT REVIEW

Flowchart is a visual representation of a(n) _______________________.

______________________ structure provides process execution first before the


decision is tested.

Only one _________________ should come out from a process symbol.

___________________ is a finite set of an instruction that specifies a sequence


of operation to be carried out in order to solve a specific problem.

_____________ signifies the preparation of data.

1.
2.
3.

4.

5.

Fill in the blanks.

Identify the following symbols.

You might also like