Algorithm
What is algorithm?
An algorithm is a set of rules used in calculations or problem-solving operations,
expressed in various languages like programming languages, Pseudocode, flowcharts
and control tables. They require an initial input and instructions, represented in
numbers or words.
Advantages of Algorithms
Easy to understand
Independent of programming language
Easy to Debug
Disadvantages of Algorithms
Difficult for bigger problems
Time-consuming
Not suitable for all problems
Tools of Algorithm
The two most frequently used tools for understanding the logic of an algorithm program
are pseudocode and flow charts.
Flowcharts & Pseudocode
The two methods are able to be applied as strategies for problem-solving. For simple
tasks, a flowchart is typically used, and for more complicated issues, pseudocode.
Eg.
Your science teacher has asked you to collect some dry leaves for a science project. To
do this task, you need to follow some steps. These are:
Step 01 - Go to the nearest park or garden.
Step 02 - Collect the fallen leaves.
Step 03 - Take the leaves home.
Step 04 - Wrap the leaves in an old newspaper, for a few days to dry.
Step 05 - Take the dried leaves to school.
Flowcharts
Algorithms are represented graphically in flowcharts. The steps needed to solve a
particular problem are outlined in the flowcharts, along with the order in which they are
completed. Shapes are used as symbols in flowcharts to indicate various tasks. A
summary of the symbols you'll need to make flowcharts is provided below.
Advantages of Flowchart:
Flowcharts are a better way of communicating the logic of the system.
Flowcharts act as a guide for a blueprint during a program design.
Flowcharts help in the debugging process.
With the help of flowcharts programs can be easily analyzed.
It provides better documentation.
Flowcharts serve as a good proper documentation.
Easy to trace errors in the software.
Easy to understand.
The flowchart can be reused for inconvenience in the future.
It helps to provide correct logic.
Disadvantages of Flowchart:
It is difficult to draw flowcharts for large and complex programs.
There is no standard to determine the amount of detail.
Difficult to reproduce the flowcharts.
It is very difficult to modify the Flowchart.
Making a flowchart is costly.
Some developer thinks that it is a waste of time.
It makes software processes low.
If changes are done in software, then the flowchart must be redrawn.
The following are some guidelines for flowcharting
Listing of all requirements should be done in a logical order.
Ensure that the flowchart has a logical start and finish.
The flow of information in a flowchart should be from top to bottom or from left
to right.
Only on flow line should come out from a process symbol.
In a decision symbol, only one flow line should enter it,
whereas there can be any number of outgoing flow lines.
The terminator symbol can have only one flow line.
Connectors reduce the number of flow lines. These must be used to make the
flow chart simple. Avoid the intersection of flow lines.
The flowchart should be easy to follow and neatly made.
Pseudocode
Pseudocode is a written version of an algorithm that can be expressed without
having to follow strict grammar guidelines. Even if other developers may be using
whole different languages, you can still readily express ideas and concepts to them
by learning to read and write pseudocode.
Advantages of Pseudocode
Closer to programming code, so easier to understand.
If one can write the Pseudocode, then use it as a starting point or outline for
writing real code.
Pseudocode can be constructed quickly as compared to a flowchart.
Disadvantages of Pseudocode
It is difficult to understand and manipulate.
Since standards are used, it is difficult to maintain consistency.
Eg.
You have to make a pseudocode for calculating the area of the rectangle. The
pseudocode to calculate the area of the rectangle is given below.
Begin
Input Length, Breadth
Calculate area = Length * Breadth
Display Area
End
Eg.
1. The algorithm for the flowchart about cooking rice is as follows;
Step 01. Take the rice to be cooked.
Step 02. Procure the container.
Step 03. Procure the water.
Step 04. Wash the rice in the water.
Step 05. Put the rice into the container.
Step 06. Pour water into the container.
Step 07. IF WATER LEVEL = 1 INCH ABOVE THE RICE
THEN GOTO STEP 8
ELSE GOTO STEP 6
ENDIF
Step 08. Light the burner on the stove.
Step 09. IF THE RICE IS BOILED
THEN GOTO STEP 12
ELSE GOTO STEP 10 ENDIF
Step 10. Heat the container.
Step 11. Go to step 9.
Step 12. Turn off the flame.
Step 13. Move the container off the stove.
Step 14. Distribute the cooked rice. Step 15.
STOP.
2. Construct a flowchart to show the procedure for obtaining the average of two given
numbers.
Step 01. INPUT TO A, B
Step 02. S ← A + B (Store the sum of the values in A and B and store in S)
Step 03. AVG ← S/2 (Compute the average)
Step 04. PRINT AVG (Show the average)
Step 05. STOP
Activity
Draw a flowchart and write an algorithm for the following purpose.
a) Find the product of two numbers.
b) Find the area of the four walls of a rectangular room.
c) Convert meters to kilometers.
d) Find the area of a triangle based on the length of three sides.
e) Accept the rate for a dozen bananas and the quantity required to determine the cost.