Program Design Tools: Algorithms, Flow Charts, Pseudo Codes and Decision Tables

You might also like

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

PROGRAM DESIGN TOOLS

Algorithms, Flow Charts, Pseudo


codes and Decision Tables

Designed by Parul Khurana, LIECA.


Pseudo-Code
• Pseudo-code is another programming tool that is
used for planning the program.
• The word pseudo means imitation or false and
the word code refers to the instruction written in
a programming language.

• Pseudo-code, therefore is an imitation of actual


computer instructions. These pseudo instructions
are phrases written in English like statements.
Designed by Parul Khurana, LIECA.
Pseudo-Code

• Instead of using symbols to describe the logic of


the program, as in flowcharts, pseudo-code uses
a structure that resembles computer instructions.

• Because, it emphasizes the design of the


program, pseudo-code is also called Program
Design Language ( PDL ).

Designed by Parul Khurana, LIECA.


Pseudo-Code

• Pseudo-code is made up of the following basic


logic structures that have been proved to be
sufficient for writing any computer program:

» Sequence
» Selection (If … then … Endif, If … then … Else … Endif)
» Iteration (For … Endfor, While … Endwhile, Do … While)

Designed by Parul Khurana, LIECA.


Pseudo-Code - Sequence
• Sequence logic is used for performing
instructions one after another in sequence. Thus,
for sequence logic, pseudo-code instructions are
written in the sequence in which they are to be
executed. The flow of logic is from top to bottom.
:
: Instruction -1

instruction -1 Instruction -2
instruction -2
instruction -3 Instruction -3

: Designed by Parul Khurana, LIECA.


Pseudo-Code - Selection
• Selection logic, also known as decision logic, is
used for making decision. It is used for selecting a
proper path out of the alternative paths in the
programming logic.

• Decision logic is depicted as either if … then …


Endif or an If … then … Else… Endif structure.

Designed by Parul Khurana, LIECA.


Pseudo-Code - Selection
• if … then … Endif selection structure:

:
expression true
:
If ( expression ) then false
statement
Endif statement
:
:

Designed by Parul Khurana, LIECA.


Pseudo-Code - Selection
• if … then … Else …Endif selection structure:
:
:
false true
expression
If ( expression ) then
statement -1
Else
statement -2
Statement-2 Statement-1
Endif
:
:
Designed by Parul Khurana, LIECA.
Pseudo-Code - Iterative
• Iterative logic, is used to produce loops when one
or more instructions are to be executed several
times depending on some expressions.

• It uses three loop structures called For … Endfor,


While … Endwhile and Do … While.

Designed by Parul Khurana, LIECA.


Pseudo-Code - Iterative
• For … Endfor iterative structure:
:
:
For k=r to s by t do For k=r to s by t do
statement
Endfor
: statement

:
• It uses an index variable k to control the loop. k

• Here r is called the initial value, s is called the


final value and t is called the step size, which may be positive
(increment) or negative ( decrement
Designed ).
by Parul Khurana, LIECA.
Pseudo-Code - Iterative
• While … Endwhile iterative structure:

: false
expression
:
While ( expression ) do
true
statement statement
Endwhile
:
:

Designed by Parul Khurana, LIECA.


Pseudo-Code - Iterative
• Do … While iterative structure:

:
statement
:
Do
true
statement expression

While ( expression ) false


:
:

Designed by Parul Khurana, LIECA.


Pseudo-Code - Description
• The format for the formal presentation of the
pseudo-code consists of two parts:

– The first part, which is optional, describes the input data, the purpose
of the algorithm and identifies the variables used in the pseudo-code.

– The second part is composed of sequence of instructions that lead to


the solution of the problem.

Designed by Parul Khurana, LIECA.


Pseudo-Code - Example
• Pseudo-code to display the nature of roots of a
quadratic equation of the type:

ax2 + bx + c = 0 provided a ≠0

Designed by Parul Khurana, LIECA.


Pseudo-Code - Example
Begin
Read: a, b, c
Set disc = b2-4ac
If ( disc < 0 ) then
Print: “roots are imaginary”
Else
If ( disc > 0) then
Print: “roots are real and distinct”
Else
Print: “roots are real and equal”
Endif
Endif
End
Designed by Parul Khurana, LIECA.
Decision-Table

• A decision table is another program development


tool that is used as a supplement along with
flowcharts.

• This tool is particularly useful in problems which


involve complex decision making.

Designed by Parul Khurana, LIECA.


Decision-Table
• A decision table shown in Figure, is a special table
that is divided into four parts by a pair of
horizontal and vertical lines.
R1 R2 R3 ... Rn
C1

Condition
Condition

Entries
Stub

C2 Condition Row
.
.
Ck
A1
Action

Entries
Action
A2 Action Row
Stub

.
.
Am Designed by Parul Khurana, LIECA.
Decision-Table
• The part above horizontal double lines pertains to
the conditions being listed, i.e., questions being
asked and, the outcomes of these tests, i.e., the
answers to these questions.

• The part of the left of vertical double lines in the


top portion, i.e., above horizontal double lines,
lists the conditions (CiS) is known as the Condition
Stub.

Designed by Parul Khurana, LIECA.


Decision-Table
• The part of the right of vertical double lines in the
top portion lists the answers to these questions,
namely Yes or No ( which are usually abbreviated
as Y and N), and is known as Conditions Entries.

• Each row in the top half of the table listing a


question and its answer is known as Condition
Row. The bottom half of the decision table, i.e.,
the part below the pair of horizontal lines, lists
the actions to be performed.
Designed by Parul Khurana, LIECA.
Decision-Table
• The left part of the bottom portion is known as
Action Stub, and in it the actions (AiS) to be carried
out are listed.
• The right part of the bottom portion has crosses ‘X’
and dashes ‘-’ to be carried out are listed.
• A cross indicates that the action in that row is to be
performed and a dash indicates that the action is not
to be performed. These are called Action Entries.
• Each row in the lower portion is called an Action
Row. The columns to the right of the vertical pair of
double lines are called Rules (RiS).
Designed by Parul Khurana, LIECA.
Types of Decision-Tables
• Decision tables are of the following types:
– Limited Entry Decision Tables
• A decision table in which question is written in the condition stub
and their answers in the condition entry part, is called limited
entry decision table.
– Extended Entry Decision Tables
• There are number of problems in which a question can have
multiple answers, then it can be expressed more precisely if the
question is extended into the condition entry part of the decision
table. Such a decision table in which a question is written in the
condition stub and their answers in the condition entry part, is
called extended entry decision table.
– Mixed Entry Decision Tables
• A decision table in which some questions are extended into the
condition entry part and others are limited to the condition stub,
is called mixed entry decision table.
Designed by Parul Khurana, LIECA.
Example- Decision-Table
• A commercial bank uses the following rules to classify new accounts:

– If the depositor’s age is 21 or above and if the deposit is Rs. 1000/- or


more, classify the account as account A.

– If the depositor’s age is under 21 and if the deposit is Rs. 1000/- or


more, classify the account as account B.

– If the depositor’s age is 21 or above and if the deposit is below Rs.


1000/- , classify the account as account C.

– If the depositor’s age is under 21 and if the deposit is below Rs. 1000/-
, do not open the account.
Construct a limited entry decision table corresponding to the verbal
statement given above to classify a new account.
Designed by Parul Khurana, LIECA.
Example- Decision-Table - Solution
• In order to obtain a decision table, first separate, from each sentence,
parts that specify conditions, and another part that specify a specific
actions that are to be performed based on the results obtained by testing
these conditions. For the above problem, the relevant condition clauses
and actions are separated and marked as shown below:

– Rule 1: If the depositor’s age is 21 or above (Condition 1) and if the


deposit is Rs. 1000/- or more (Condition 2), classify the account as
account A (Action 1).

– Rule 2: If the depositor’s age is under 21 (Condition 3) and if the


deposit is Rs. 1000/- or more (Condition 2), classify the account as
account B(Action 2).

Designed by Parul Khurana, LIECA.


Example- Decision-Table - Solution
– Rule 3: If the depositor’s age is 21 or above (Condition 1) and if the
deposit is below Rs. 1000/- (Condition 4), classify the account as
account C (Action 3).

– Rule 4: If the depositor’s age is under 21 (Condition 3) and if the


deposit is below Rs. 1000/- (Condition 4), do not open the
account(Action 4).

Designed by Parul Khurana, LIECA.


Example- Decision-Table - Solution
• The total conditions tested and actions to be
taken are isolated and rewritten below in the
concise form: • Observe that the
condition-1 and
– Condition 1: Depositor’s age ≥ 21 condition-3 are not
– Condition 2: Deposit ≥ Rs. 1000/- independent, rather
– Condition 3: Depositor’s age < 21 they are complementary,
i.e., if the answer to the
– Condition 4: Deposit < Rs. 1000/- question – Is depositor’s
age ≥ 21? – is NO, the
– Action 1: Classify the account as account A. answer to the question –
– Action 2: Classify the account as account B. Is depositor’s age < 21? –
– Action 3: Classify the account as account C. is Yes, and vice versa.
Similarly, condition 2 and
– Action 4: Do not open account . condition-4 are
complementary.
Designed by Parul Khurana, LIECA.
Example- Decision-Table - Solution
• The purpose of this illustration is that, there is no need to include the
complementary conditions. Therefore, instead of testing four conditions,
the problem can be solved by testing just following two conditions:

– Condition 1: Depositor’s age ≥ 21


– Condition 2: Deposit ≥ Rs. 1000/-
Using this, rules can be expressed in limited entry decision table as
shown below:
R1 R2 R3 R4
C1 :Depositor’s age ≥ 21 ? Yes No Yes No
C2 :Deposit ≥ 1000 ? Yes Yes No No
A1:Classify account as account A X - - -
A2:Classify account as account B - X - -
A3:Classify account as account C - - X -
A4:Do not open account - - - X
Designed by Parul Khurana, LIECA.
Practice Questions
• Generate the pseudo-code to compute sum and average
of 5 values.
• Generate the pseudo-code to determine whether the
number is positive or negative or zero.
• Write the pseudo-code to find the largest of three
numbers.
• Write the pseudo-code to check if the given number is
prime or not.
• Write the pseudo-code to check if the given number is
even or odd; without using 2 anywhere in the code.

Designed by Parul Khurana, LIECA.


Practice Questions
• Sweet Corporation Ltd. markets chocolates and candies to stores,
wholesalers and government agencies, with the following discount
policies:-
– If the order is from a store for amount up to Rs. 5000/-, the discount is
5%, otherwise 7.5%.
– If the order is from wholesalers for amount up to Rs. 10000/-, the
discount is 7.5%, otherwise 12%.
– The discount is 6% for government agencies irrespective of the amount
of order.
– On chocolates, a flat discount of 5% is given irrespective order amount
and customer type.
Construct limited, extended and mixed entry decision tables
corresponding to this.

Designed by Parul Khurana, LIECA.

You might also like