Unit-2
Unit-2
Quality of Algorithm
There are few factors that determine the quality of a given algorithm
An algorithm should be relatively fast
Any algorithm should require minimum computer memory to produce the desired output in an
acceptable amount of time
Methods of Representing Algorithm
The following methods could be used to represent an algorithm.
Methods of English like form
Methods of flowchart
Methods of Pseudo code
Methods of Decision table
Methods of Data flow Diagram(DFD)
Algorithm can be represented in two different forms:
1. Flow chart
2. Pseudo code
More Examples
Algorithm of factorial of a number
Step 1: Start
Step 2: Read a number n
Step 2: Initialize variables: i = 1, fact = 1
Step 3: if i <= n go to step 4 otherwise go to step 7
Step 4: Calculate: fact = fact * i
Step 5: Increment the i by 1 (i=i+1) and go to step 3
Step 6: Print fact
Step 7: Stop
Algorithm to find greatest number of three given numbers:
Step-1: Ask the user to enter three integer values.
Step-2: Read the three integer values in num1, num2, and num3 (integer variables).
Step-3: Check if num1 is greater than num2.
Step-4: If true, then check if num1 is greater than num3.
1. If true, then print ‘num1’ as the greatest number.
2. If false, then print ‘num3’ as the greatest number.
Step-5: If false, then check if num2 is greater than num3.
1. If true, then print ‘num2’ as the greatest number.
2. If false, then print ‘num3’ as the greatest number.
Pseudo code
To solve any problem first algorithm should be designed. Then one can draw the flow chart to see the flow of
the program. In between program and algorithm something the intermediate is known as ‘Pseudo code’.
Pseudo code is an artificial and informal language that helps programmer to develop program. There is no real
language for Pseudo code. To develop program the developer designs algorithm. Pseudo code is just the
modification of algorithm.
It is a text base detail design tool and is an intermediary between algorithm and implemented program. By
following it the programmer can write program easily.
Pseudo code is an informal way of programming description that does not require any strict programming
language syntax or underlying technology considerations.
It is used for creating an outline or a rough draft of a program. Pseudo code summarizes a program’s
flow, but excludes underlying details.
Pseudo code is made up of two words: Pseudo and code. Pseudo means imitation and code refers to
instructions, written in a programming language. As the name suggests, Pseudo code is not a real
programming code, but it models and may even look like programming code.
Pseudo code uses plain English statements rather than symbols to represent the processes of a computer
program.
It is also known as Program Design Language (PDL) as it emphasizes more on the design aspect of a
computer program.
Usually pseudo code instructions are written in normal English, but in a structured way.
System designers write pseudo code to ensure that programmers understand a software project's
requirements and align code accordingly.
Advantages of pseudo code
• Pseudo code is understood by the programmers of all types.
• It enables the programmer to concentrate only on the algorithm part of the code development.
• It cannot be compiled into an executable program.
Example:1
Java code:
if (i < 10)
{
i++ ;
}
Pseudo code:
if i is less than 10, increment i by 1.
Example: 2
Pseudo code: To find the greatest among three numbers.
BEGIN
Declare three integer variables a, b, c
If a is greater than b,
If a is greater than c,
Print "a is largest"
Else
Print "c is largest"
Else
If b is greater than c,
Print "b is largest"
Else
Print "c is largest"
END
Flowchart
Flowchart is a graphical representation of an algorithm that illustrates the sequence of operations to be
performed to get a solution.
Programmers often use it as a program-planning tool to solve a problem.
It makes use of symbols which are connected among them to indicate the flow of information and
processing.
The Symbols represent operations and the arrows represent the sequence in which the operations are
implemented.
The primary purpose of the flowchart is to help the programmer in understanding the logic of the
program.
The process of drawing a flowchart for an algorithm is known as “flowcharting”.
There are four basic shapes used in a flow chart. Each shape has a specific use:
oval:
Parallelogram:
Rectangle:
Diamond:
Input/Output: A parallelogram denotes any function of input/output type. Program instructions that take
input from input devices and display output on output devices are indicated with parallelogram in a
flowchart.
Processing: A box represents arithmetic instructions. All arithmetic processes such as adding, subtracting,
multiplication and division are indicated by action or process symbol.
Decision Diamond symbol represents a decision point. Decision based operations such as yes/no question
or true/false are indicated by diamond in flowchart.
Connectors: Whenever flowchart becomes complex or it spreads over more than one page, it is useful to
use connectors to avoid any confusions. It is represented by a circle.
Flow lines: Flow lines indicate the exact sequence in which instructions are executed. Arrows represent
the direction of flow of control and relationship among different symbols of flowchart.
Arrows connect the basic shapes in a flowchart. The shapes and arrows of a flowchart describe the flow of a
program from start to end. Flowcharts typically flow from the top to the bottom or flow from the left to the
right.
Advantages of Flowchart:
Flowcharts are a better way of communicating the logic of the system.
Flowcharts act as a guide for blueprint during program designed.
Flowcharts help in debugging process.
With the help of flowcharts programs can be easily analyzed.
It provides better documentation.
Flowcharts serve as a good proper documentation.
Easy to trace errors in the software.
Easy to understand.
The flowchart can be reused for inconvenience in the future.
It helps to provide correct logic.
Disadvantages of Flowchart:
It is difficult to draw flowcharts for large and complex programs.
There is no standard to determine the amount of detail.
Difficult to reproduce the flowcharts.
It is very difficult to modify the Flowchart.
Making a flowchart is costly and time taking.
It makes software processes low.
If changes are done in software, then the flowchart must be redrawn
Example -1:
Two is added to the number. Next, the resulting sum is printed. Finally, the program ends.
Start
Enter a number
Stop
Example-2 :
Add two numbers and print the result
Start
t
Declare Num1, Num2, Sum
Read Num1,Num2
Sum=Num1+Num2
Stop
The Python code that corresponds to this flow chart is:
# Store input numbers
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
# Add two numbers
sum = float(num1) + float(num2)
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Example- 3:
To check if the number is greater than Zero or Less than Zero or Equal to Zero
The program starts. Next, the program asks a user for a number. If the number is greater than zero, the program
prints "Greater than 0". If the number is less than zero, the program prints "Less than 0". Then the program
prints "Done" and the program ends.
A flowchart that describes this program is below:
Start
Enter a number
If Print
num>0 “Greater than 0”
? True
False
If Print
num<0 True “Less than 0”
?
False
Print
“Equal to 0”
Stop
Python code that corresponds to this flow chart is:
# start
num = input('Enter a number: ')
num = float(num)
if num>0:
print('num greater than zero')
if num<0:
print('num less than zero')
print('Done')
# end
Example-4:
Draw a flowchart to input two numbers from the user and display the largest of two numbers
Start
True
If
num>num2
?
False
Display num2
Display num1
Stop
Though flowcharts can be useful for writing and analyzing a program, drawing a flowchart for complex
programs can be more complicated than writing the program itself. Hence, creating flowcharts for complex
programs is often ignored.
Decision table
A decision table is used to represent conditional logic by creating a list of tasks depicting business level rules.
Decision tables can be used when there is a consistent number of a condition that must be evaluated and
assigned a specific set of actions to be used when the conditions are finally met.
The purpose of a decision table is to structure logic by generating rules derived from the data entered in the
table itself. A decision table lists causes (business rule condition) and effects (business rule action and expected
results), which are represented through the use of a matrix where each column represents a unique combination.
If there are rules within a business that can be expressed through the use of templates and data then a decision
table is one technique that can be used to accomplish this. Each row of a decision table collects and stores its
data separately and then combines the data with a specific or customized template to generate a rule. Decision
tables should not be used if the rules in question do not follow a set of templates.
T – Correct username/password
F – Wrong username/password
E – Error message is displayed
H – Home screen is displayed
Now let’s understand the interpretation of the above cases:
Case 1 – Username and password both were wrong. The user is shown an error message.
Case 2 – Username was correct, but the password was wrong. The user is shown an error message.
Case 3 – Username was wrong, but the password was correct. The user is shown an error message.
Case 4 – Username and password both were correct, and the user is navigated to the homepage.
So, this was an example of building a decision table in software testing.