Sie sind auf Seite 1von 16

ALGORITHM AND FLOWCHART

ALGORITHM
A. 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

FLOWCHART
A. 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

C. 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.
1. 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.
1. 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.
1. 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

Terminator (terminal)

Symbols

Representation

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

Flow lines

Indicates the flow of


operation

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.

BEGIN
Read A

Algorithm

Step 1. Read in the value of A


Step 2. Print the value of A.

Print A

2. Draw a flowchart
ENDthat will compute and display the sum and product of two numbers.
Write its equivalent algorithm.
BEGIN

Sum=0;
Product =0
P=0
Read Num1,
Num2
Sum = Num1+Num2
Product = Num1*Num2
P= A*B
Print Sum,
Product

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.

END

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.

BEGIN

Area=0

Algorithm

Step 1. Initialize Area(A) into 0.


Step 2. Input the width(W) and
length(L) of a rectangle.

Input
W, L

Step 3. Calculate the A by


multiplying W and L.
Step 4. Print the value of A.

Area = L x W

END
Print
Area

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 END
"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:
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.
Algorithm
Step 1. Read in the values of x and y.
Step 2. Test if x is greater than y.
BEGIN

Step 3. If x is greater than y, x is higher. However, if x is


less than y, y is higher.
Input

x, y
Step 4. Print the
number and the remark Higher.

x>y
x>y

Print x, Higher

F
Print y, Higher

END

2. Draw a flowchart that will determine the students final grade and indicate whether it
is passing or failing. The final grade is calculated as the average of four marks.

Algorithm
Step 1. Initialize Grade
into 0.
BEGIN
Step 2. Input a set of 4 marks (M1, M2, M3, M4).
Step 3. Calculate their average(Grade)
by summing and
Grade=0
dividing by 4.
Step 4. If average(Grade) is below 50
Input
Print FAIL M1,M2,M3,M4

else
PrintGrade=(M1+M2+M3+M4)/4
PASS

T
IS GRADE<50

Print
Pass

Print

Fail

END

3. 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 or equal 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.

BEGIN

bonus=0
begin

Read Name,
Salary

Salary
< =10,000
T
Bonus = 0.25*Salary

Print Name,
Bonus

en end
dEND

Bonus = 3,000

2. 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
3.

4.
5.
6. Loops and Counter
7. Looping
8. 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.
9.
10. Counter
11. 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.
12.
13. Steps in Loop Control
14. Initialization
15. The value of counter is initially set equal to zero or one.
16.
17. Test for limit condition

18. Before the logic flow gets out of a loop, a loop terminating condition must first be
satisfied.
19.
Incrementation
20. Often each loop is executed, 1 is added to the counter. Thus counter reflects the
number of times the operation has been performed.
21.
22.
23. Examples:
24. 1. Construct a flowchart that will count from 1 to 10 and print each number
counted using the looping structure. Write the equivalent algorithm.
25.
26.
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.
27.
28.
29.
30.
31.
32.

BEGIN

N=0

N<10

END

T
N=N +1

Print N

33. 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.
34. Formula: area = pi x r2
35.
36.
37.
38.
39.
Algorithm
40.
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.

begin
BEGIN
Pi=3.1416 r=1

area=pi * r *r

Print r, area
T
r=r+1

end
END
R<5

Das könnte Ihnen auch gefallen