Lesson 6

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Algorithm

Algorithm is a finite, well defined, ambiguous sequence of instructions for


completing a task. Algorithm is an English depiction of the logic used to solve the problem.
This is a step-by-step method for solving a problem or a task. The measures are to be
ordered in number, unambiguous and finite.
Different algorithms can be written to accomplish one particular task. The different
algorithms differ as to their time and space requirements. The programmer selects the
algorithm best suited to solve the given problem.

Let’s now look at two simple algorithms to find the greatest among three numbers,
as follows:

ALGORITHM 1.

Step 1: Start
Step 2: Read the three numbers A, B, C
Step 3: Compare A and B. If A is greater perform step 4 else perform step 5.
Step 4: Compare A and C. If A is greater, output “A is greatest” else output “C is greatest”.
Perform step 6.
Step 5: Compare B and C. If B is greater, output “B is greatest” else output “C is greatest”.
Step 6: Stop

ALGORITHM 2.

Step 7: Start
Step 8: Read the three numbers A, B, C
Step 9: Compare A and B. If A is greater, store A in MAX, else store B in MAX.
Step 10: Compare MAX and C. If MAX is greater, output “MAX is greatest” else output “C is
greatest”.
Step 11: Stop

The algorithms both achieve the same goal, but in different ways. The programmer
selects the algorithm, based on each algorithm’s advantages and disadvantages. For
example, the first algorithm has more number of comparisons, whereas in the second
algorithm, an additional variable MAX is required.
FLOWCHART

Flowcharts are graphical representation of the solution to the problem. Flowcharting is a


common choice among programmers for algorithm presentation purposes. Using a
flowchart, the programme’s meaning is expressed even easier. Because flowchart is a
diagrammatic representation it forms a common communication medium.

Flowchart Symbols

A flowchart is drawn using symbols of various types. Symbols such as ovals,


rectangles, and diamonds are used in the flowchart, each denoting specific action and
purpose.

Symbol Name Shape Use Example


To set the start or stop
Terminal points; beginning or BEGIN
ending spots
To set the initial value
Initialization ctr= 0
of a variable/s
To receive input from Input age
user or display an
Input/Output (I/O)
output to the output Display sum
screen

To set an execution of ctr = ctr + 1


a statement or
Process
operation; also called temp = x % 2
the action symbol
To evaluate a
condition that will lead
Decision ctr > 0?
to either a True or a
False status
To link to a portion in
On-page the flowchart labeled
Connector accordingly and found A
within the same page
To link to a portion in
Off-Page the flowchart found on B
Connector another page

To set the direction


Flow lines and and flow of movement ctr > 0?
arrow heads from one statement to
another
Preparing a Flowchart

A flowchart may be simple or complex. The most common symbols that are used to
draw a flowchart are—Process, Decision, Data, Terminator, Connector and Flow lines. While
drawing a flowchart, some rules need to be followed

(1) A flowchart should have a start and end,


(2) The direction of flow in a flowchart must be from top to bottom and left to right, and
(3) The relevant symbols must be used while drawing a flowchart. While preparing the
flowchart, the sequence, selection or iterative structures may be used wherever
required.

Control Structures in Flowchart

We see that in a sequence, the steps are executed in linear order one after the other.
In a selection operation, the step to be executed next is based on a decision taken. If the
condition is true (yes) a different path is followed than if the condition evaluates to false
(no). In case of iterative operation, a condition is checked. Based upon the result of this
conditional check, true or false, different paths are followed. Either the next step in the
sequence is executed or the control goes back to one of the already executed steps to make a
loop.
Examples

• The first flowchart computes the product of any two numbers and gives the result.
The flowchart is a simple sequence of steps to be performed in a sequential order.
• The second flowchart compares three numbers and finds the maximum of the three
numbers. This flowchart uses selection. In this flowchart, decision is taken based
upon a condition, which decides the next path to be followed, i.e. If A is greater than
B then the true (Yes) path is followed else the false (No) path is followed. Another
decision is again made while comparing MAX with C.
• The third flowchart finds the sum of first 100 integers. Here, iteration (loop) is
performed so that some steps are executed repetitively until they fulfill some
condition to exit from the repetition. In the decision box, the value of I is compared
with 100. If it is false (No), a loop is created which breaks when the condition
becomes true (Yes).

Flowcharts have their own benefits; however, they have some limitations too. A
complex and long complex may run into multiple pages, which becomes difficult to
understand and follow. In addition, updating a flowchart with the changing
requirements is a challenging job.
Difference between Algorithm and Flowchart

If you compare a flowchart to a movie, then an algorithm is the story of that movie. In other
words the essence of a flowchart is an algorithm. In addition, there are many variations
between the algorithm and the flowchart in the field of computer programming about
different aspects, such as the precision, the way they show and how people feel about them.
Below is a table illustrating the differences between them in details.

Algorithm Flowchart
It is a procedure for solving problems. It is a graphic representation of a process.
The process is shown in step-by-step The process is shown in block-by-block
instruction. information diagram.
It is complex and difficult to understand. It is intuitive and easy to understand.
It is convenient to debug errors. It is hard to debug errors.
The solution is showcased in natural
The solution is showcased in pictorial format.
language.
It is somewhat easier to solve complex
It is hard to solve complex problem.
problem.
It costs more time to create an
It costs less time to create a flowchart.
algorithm.
Examples

1. Calculate the interest of a bank deposit.

Algorithm:

Step 1: Read amount


Step 2: Read years
Step 3: Read rate
Step 4: Calculate the interest with formula “Interest=Amount*Years*Rate/100
Step 5: Print interest

Flowchart
2. Determine whether a temperature is below or above the freezing point

Algorithm

Step 1: Input temperature,


Step 2: If it is less than 32, then print “below freezing point”, otherwise print “above freezing
point”

Flowchart
Flowchart Examples - Medical Services

This is a hospital flowchart example that shows how clinical cases shall be processed. This
flowchart uses decision shapes intensively in representing alternative flows.
Flowchart Example – Simple Algorithm

A flowchart can also be used in visualizing algorithms, regardless of its complexity. Here is
an example that shows how flowchart can be used in showing a simple summation process.

References

Lynch, A.(2020). “Examples of Algorithm Flowchart”.


Retrieved from https://www.edrawsoft.com/algorithm-flowchart-examples.html
https://www.edrawsoft.com/explain-algorithm-
flowchart.html#:~:text=Algorithm%20and%20flowchart%20are%20two,th
e%20process%20of%20a%20program.&text=An%20algorithm%20is%20a
%20step,program%20in%20a%20graphical%20way.
Mahdi, A.(2013). Algorithm and Flow Chart
Retrieved from https://faradars.org/wp-
content/uploads/2015/07/Algorithm-and-Flow-Chart.pdf
Programiz (2020). Flowchart in Programming
Retrieved from https://www.programiz.com/article/flowchart-programming

You might also like