Fop XP 02

You might also like

Download as pps, pdf, or txt
Download as pps, pdf, or txt
You are on page 1of 21

Representing Algorithms

Pre-assessment Questions
1. List the types of programming languages.

2. Which of the following characters is used by a machine language:


a. 15
b. A
c. 0
d. %

3. Which language was designed for processing business related data?


a. BASIC
b. FORTRAN
c. COBOL
d. PASCAL

4. What is the main difference between a compiler and an interpreter?

5. When was FORTRAN developed and by whom?

©NIIT Representing Algorithms Lesson 1B/ Slide 1 of 21


Representing Algorithms

Solutions to Pre-assessment Questions


Ans 1. There are three types of programming languages:
• Machine language
• Assembly language
• High-level language

Ans 2. 0

Ans 3. COBOL

Ans 4. A compiler translates the entire program first and generates the object code, which
can be later executed, whereas an interpreter translates and executes one line of
source code at a time.

Ans 5. FORTRAN was developed in 1957 by John Backus and his team at International
Business Machines (IBM) Corporation.

©NIIT Representing Algorithms Lesson 1B/ Slide 2 of 21


Representing Algorithms

Objectives
In this lesson, you will learn about:

• Defining an algorithm and its characteristics


• Developing algorithms in different levels
• Identifying flowchart symbols
• Drawing flowchart and identifying its advantages and disadvantages
• Drawing decision table and identifying its advantages and disadvantages
• Developing pseudocode and identifying its advantages and disadvantages

©NIIT Representing Algorithms Lesson 1B/ Slide 3 of 21


Representing Algorithms

Introducing Algorithms
• An algorithm is a sequence of steps required to accomplish a task.

• The steps can be categorized into the following three phases:


• Input phase
• Process phase
• Output phase

• These phases together form the Input-Process-Output (IPO) cycle

©NIIT Representing Algorithms Lesson 1B/ Slide 4 of 21


Representing Algorithms

Characteristics of an Algorithms
• An algorithm has the following five characteristics:
• An algorithm ends after a fixed number of steps.

• Each step in an algorithm clearly specifies the actions to be performed.

• The steps in an algorithm specify basic operations. These operations could


include mathematical calculations, input/output functions, and logical
comparisons.

• The algorithm should accept input data, in a defined format, before it can be
processed by the given instructions.

• An algorithm generates one or more outputs after the input is processed.

©NIIT Representing Algorithms Lesson 1B/ Slide 5 of 21


Representing Algorithms

Levels of Algorithm
• The two levels of algorithm are:

• Macro-level: An algorithm that contains brief steps about a process is called a


macro-level algorithm.

• Micro-level: An algorithm that contains detailed steps about a process is called a


micro-level algorithm.

©NIIT Representing Algorithms Lesson 1B/ Slide 6 of 21


Representing Algorithms

Representing Algorithms
• Algorithms can be represented in the following different ways:

• Flowcharts

• Decision tables

• Pseudocodes

©NIIT Representing Algorithms Lesson 1B/ Slide 7 of 21


Representing Algorithms

Flowcharts
• Flowcharts are the graphical representation of algorithms.
• A flowchart consists of symbols, which represent the steps or stages in an
algorithm. Each symbol represents a type of activity.
• The different symbols used in a flowchart are:
• Input/Output
• Process
• Decision
• Procedure/Subroutine
• Flow line
• Start and Stop
• On Page Connector
• Off Page Connector
• Annotation
• Display

©NIIT Representing Algorithms Lesson 1B/ Slide 8 of 21


Representing Algorithms

Flowcharts (Contd.) Start

• This flowchart accepts two numbers,


Accept two numbers
computes the product and displays the product

Calculate the product of


the two numbers

Display the product

Stop

Flowchart to Calculate the Product of Two Numbers

©NIIT Representing Algorithms Lesson 1B/ Slide 9 of 21


Representing Algorithms

Flowcharts (Contd.)
• The following flowchart displays the greater of two numbers after accepting two
numbers and comparing them.
Start

Accept two numbers

Calculate the product of


the two numbers

Display the product

Stop

Flowchart to display the Greater of Two Numbers

©NIIT Representing Algorithms Lesson 1B/ Slide 10 of 21


Representing Algorithms

Rules of Flowcharting
• American National Standards Institute (ANSI) recommends a number of rules that
need to be followed when drawing flowcharts. Some of these rules and guidelines are
given below:
• The entire logic of a flowchart should be represented using the standard symbols.
• The flowchart should be clear, precise, and easy to follow.
• Flowcharts can have only one start point and one end point.
• The steps in a flowchart should follow top-to-bottom or left-to-right approach.
• All necessary inputs should be listed out in logical order.
• The start and stop symbols should have only a single flow line.
• The input, process, output, and display symbols should have two flow lines
connecting to the previous symbol and to the next symbol.
• The decision symbol should have one flow line connecting to the previous symbol
but two flow lines connecting to the next symbol for each possible solution.

©NIIT Representing Algorithms Lesson 1B/ Slide 11 of 21


Representing Algorithms

Flowcharts (Contd.)
• Advantages of flowcharts:
• Flowcharts are a better method of communicating logic.
• The flowcharts help in analyzing the problems effectively.
• The flowcharts act as a guide during the program development phase.
• It is easier to debug errors in logic using a flowchart.
• The flowcharts help in maintaining the programs.

©NIIT Representing Algorithms Lesson 1B/ Slide 12 of 21


Representing Algorithms

Flowcharts (Contd.)
• Disadvantages of flowcharts:
• A lengthy flowchart may extend over multiple pages, which reduces
readability.
• As flowcharts symbols cannot be typed, drawing a flowchart using any graphic
tool is a time consuming process.
• The changes made to a single step may cause redrawing the entire flowchart.
• A flowchart representing a complex algorithm may have too many flow lines.
This reduces readability, and it is time-consuming to draw and understand the
logic Readability.

©NIIT Representing Algorithms Lesson 1B/ Slide 13 of 21


Representing Algorithms

Decision Tables
• Decision tables represent algorithms that involve complex decision-making.
• A decision table consists of four components, Condition Stub, Condition Applied,
Action Stub, and Action Taken, as shown in the following table :

Condition Stub Conditions Applied


 
 

Action Stub Action Taken


 
 

Decision Table

©NIIT Representing Algorithms Lesson 1B/ Slide 14 of 21


Representing Algorithms

Decision Table (Contd.)

• Condition Stub
• Consists of the conditions based on which a decision is made.
• Condition Applied
• This component contains the condition alternatives as Y for the satisfied
condition and N for the unsatisfied condition.
• Action Stub
• Contains the actions to be taken for the combination of conditions specified in
the Condition Applied component.
• Action Taken
• Consists of the action alternatives as Y or N. Here, Y or N specify whether or
not the action mentioned in the Action Stub has to be taken.

©NIIT Representing Algorithms Lesson 1B/ Slide 15 of 21


Representing Algorithms

Decision Table (Contd.)

• To create a decision table, the following points should be considered:


• Specify an appropriate name for the table, describing its objective.
• Write the condition statement/statements in the Condition Stub, based on
which a decision is to be made.
• List all the combinations corresponding to the conditions specified in the
Condition Stub components.
• Specify all the actions that can be made in conjunction with each combination.

©NIIT Representing Algorithms Lesson 1B/ Slide 16 of 21


Representing Algorithms

Decision Table (Contd.)


• Advantages of decision tables are:
• They are useful in cases in which complex algorithms having lot of branching
need to be represented.
• Decision tables are used as an alternative tool to simplify the decision-making
tasks that are involved in a process.

• Disadvantage of decision tables are:


• The decision table can depict only the decision-making tasks that are involved
in a process.
• Beginners prefer flowcharts to decision tables to depict the complete sequence
of steps to accomplish a task.

©NIIT Representing Algorithms Lesson 1B/ Slide 17 of 21


Representing Algorithms

Pseudocode
• Pseudocode represents an algorithm in English language.
• Pseudocode is used as an alternative to a flowchart.
• The statements used in pseudocode are simple and written in a sequential manner.
• There are a number of keywords used in pseudocode:
• begin…end
• accept
• display
• if…else

©NIIT Representing Algorithms Lesson 1B/ Slide 18 of 21


Representing Algorithms

Pseudocode (Contd.)
• Advantages of pseudocode
• It is easier and faster to write as compared to a flowchart.
• You can detect errors and make changes to it easily.
• It does not need to be re-written if any changes are made.
• It can be converted to a program using any programming language.

• Disadvantages of pseudocode
• Pseudocode does not provide a graphical representation of an algorithm.
• Pseudocode depicting too many nested conditions may be difficult to
understand

©NIIT Representing Algorithms Lesson 1B/ Slide 19 of 21


Representing Algorithms

Summary
Inthis lesson, you learned:
• An algorithm is a sequence of steps required to solve a problem.
• Algorithms have two levels, macro and micro.
• Algorithms can be represented in different ways, such as using flowcharts, decision
tables, and pseudocode.
• A flowchart is a graphical representation of the algorithm.
• A decision table is used to depict the decision-making tasks that are involved in a
process.

©NIIT Representing Algorithms Lesson 1B/ Slide 20 of 21


Representing Algorithms

Summary (Contd.)
• A decision table consists of four components:
• Condition Stub
• Conditions Applied
• Action Stub
• Action Taken
• Pseudocode represents an algorithm in English language.

©NIIT Representing Algorithms Lesson 1B/ Slide 21 of 21

You might also like