Professional Documents
Culture Documents
Program Design Tools: Algorithms, Flow Charts, Pseudo Codes and Decision Tables
Program Design Tools: Algorithms, Flow Charts, Pseudo Codes and Decision Tables
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)
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 -2 instruction -3 :
Instruction -1 Instruction -2 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.
Pseudo-Code - Selection
if then Endif selection structure:
: : If ( expression ) then statement Endif : :
expression
true
false
statement
Pseudo-Code - Selection
if then Else Endif selection structure:
: : false If ( expression ) then statement -1 Else statement -2 Statement-2 Endif : :
expression
true
Statement-1
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.
Pseudo-Code - Iterative
For Endfor iterative structure:
: : For k=r to s by t do statement Endfor : :
For k=r to s by t do
statement
k It uses an index variable k to control the loop. Here r is called the initial value, s is called the final value and t is called the step size, which may be positive Designed by Parul Khurana, LIECA. (increment) or negative ( decrement ).
Pseudo-Code - Iterative
While Endwhile iterative structure:
: : While ( expression ) do statement Endwhile : :
false expression
true statement
Pseudo-Code - Iterative
Do While iterative structure:
: : Do statement While ( expression ) : :
Designed by Parul Khurana, LIECA.
statement
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.
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
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.
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 C1 Condition Stub Action Stub R2 R3 ... Rn Condition Entries
Decision-Table
C2 .
. Ck A1
Condition Row
Action Entries
A2 . . Am
Action Row
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 depositors age is 21 or above and if the deposit is Rs. 1000/- or more, classify the account as account A. If the depositors age is under 21 and if the deposit is Rs. 1000/- or more, classify the account as account B. If the depositors age is 21 or above and if the deposit is below Rs. 1000/- , classify the account as account C. If the depositors 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.
Observe that the condition-1 and condition-3 are not independent, rather they are complementary, i.e., if the answer to the question Is depositors age 21? is NO, the answer to the question Is depositors age < 21? is Yes, and vice versa. Similarly, condition 2 and condition-4 are complementary.
C2 :Deposit 1000 ?
A1:Classify account as account A A2:Classify account as account B A3:Classify account as account C A4:Do not open account
Yes
X -
Yes
X -
No
X -
No
X
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.