ALGORITHMS
Algorithm
• An algorithm is a finite sequence of steps for accomplishing
some computational task.
• A good algorithm must satisfy the following characteristics
• Ordered: The steps must be properly ordered and logical
Concise: The steps must be unambiguous
• Finiteness: The steps must be finite : start and end at some
point.
• Input: The inputs must be clearly specified
• Output: The expected results must be specified
• Effectiveness: The steps specified must be doable.
Developing an Algorithm
• To develop an algorithm a number of tools can be
used:
– Flowchart
– Pseudo code
– Top down chart/Hierarchy charts
– Decision tables
Flowchart
This is a graphical representation of the logical steps
needed to carry out a task.
A flowchart is a graphical representation of a process,
algorithm, or workflow, showing the steps as boxes of
various kinds, and their order by connecting the boxes
with arrows.
It is a visual tool that illustrates the sequence of
operations, decisions, and data flow within a system or
process.
Flow Chart Symbols
Uses of flowchart
Process Analysis and Improvement: Identifying inefficiencies and areas
for enhancement.
Communication: Clearly conveying process steps to diverse audiences.
Documentation: Providing easy reference for current and new team
members.
Problem Solving: Breaking down complex issues into manageable steps.
Program Design: Planning algorithms and logic before coding.
Training: Explaining workflows and procedures effectively.
Basic steps when for creating flowchart
Define the Purpose and Scope: Clearly identify what process you
are charting and what its boundaries are.
Identify the Start and End Points: Determine where the process
begins and where it concludes.
List All Steps/Activities: Brainstorm and list every single step
involved in the process in chronological order.
Identify Decisions: Determine points where decisions need to be
made, leading to different paths.
Choose Appropriate Symbols: Select the correct flowchart
symbol for each step (process, decision, input/output, etc.).
Drawing a flowchart
Start with the "Terminal" symbol.
Connect symbols with arrows to show the flow.
For decision symbols, ensure each possible outcome has a
corresponding arrow and label .
Use connectors if the chart becomes too large or spans multiple
pages.
Advantages of flowchart
– Flowcharts are a better way of communicating the logic of
the system.
– With the help of a flowchart, problem can be analyzed
easily.
– They serve as a good program documentation.
Disadvantages of flowchart
– For a complex problem, the flowchart becomes complex
and clumsy.
– If alterations are required, the flowchart may require
redrawing completely.
– The flow chart symbols cannot be typed and so
reproduction of a flowchart becomes difficulty.
Flow Chart Example
• Example: Using a flowchart design an algorithm to test
if temperature is less than 32 degree Celsius. If it
exceeds 32 then print “cover tomatoes” otherwise
“uncover tomatoes”.
Output
Pseudo code
– It is a representation an algorithm using natural language
statements e.g. structured English. It describes the entire
logic of the algorithm.
– It is not an executable code.
Pseudo code
EXAMPLE: . Calculating the Average of Three Numbers
START
// Declare variables to store the three numbers and their average
DECLARE num1, num2, num3, average
// Prompt the user to enter the numbers
OUTPUT "Enter the first number: "
INPUT num1
OUTPUT "Enter the second number: "
INPUT num2
OUTPUT "Enter the third number: "
INPUT num3
// Calculate the average
average = (num1 + num2 + num3) / 3
// Display the calculated average
OUTPUT "The average is: ", average
END
Advantages of pseudo code
– Reduced complexity
– Easy to understand
– Easy to translate to code
– No special symbols required
– Saves time spent during coding, testing and debugging.
Disadvantages of pseudo code
– Can be ambiguous because it uses natural language.
– Pseudo code has few rules and it is hard to standardize.
– The pseudo code focuses on details than the bigger picture
of the algorithm.