0% found this document useful (0 votes)
24 views

Unit-2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Unit-2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Unit-2

Techniques of Problem Solving: Flowcharting, decision table, algorithms, Structured


programming concepts, Programming methodologies viz. top-down and bottom-up
programming.

Techniques of Problem Solving


There are a number of techniques of problem solving. Some of the important techniques are:
 Algorithms
 Flowcharting
 Pseudo code and
 Decision table
Algorithms
Algorithms are one of the most basic tools that are used to develop the problem solving logic. An
algorithm is defined as a finite sequence of explicit instructions that when provided with a set of input
values produces an output then terminates.
Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain
order to get the desired output.
Algorithms can have steps that repeat (iterate) or require decisions (logic & comparison) until the task is
completed.
Algorithms are generally created independent of underlying languages, i.e. an algorithm can be
implemented in more than one programming language.
Basic Components of an algorithm are:
1. Input
2. Process
3. Output
How to Write an Algorithm?
There are no well-defined standards for writing algorithms. Rather, it is problem and resource dependent.
Algorithms are never written to support a particular programming code.
As we know that all programming languages share basic code constructs like loops (do, for, while), flow-
control (if-else), etc. These common constructs can be used to write an algorithm.
We write algorithms in a step-by-step manner, but it is not always the case. Algorithm writing is a process and
is executed after the problem domain is well-defined. That is, we should know the problem domain, for which
we are designing a solution.
Example
Let's try to learn algorithm-writing by using an example.
Problem − Design an algorithm to add two numbers and display the result.
step 1 − START
step 2 − declare three integers a, b & c
step 3 − define values of a & b
step 4 − add values of a & b
step 5 − store output of step 4 to c
step 6 − print c
step 7 – STOP
Algorithms tell the programmers how to code the program. Alternatively, the algorithm can be written as −
step 1 − START
step 2 − get values of a & b
step 3 − c ← a + b
step 4 − display c
step 5 − STOP
In design and analysis of algorithms, usually the second method is used to describe an algorithm. It makes it
easy for the analyst to analyze the algorithm ignoring all unwanted definitions. One can observe what
operations are being used and how the process is flowing.
Writing step numbers (Step-1,step-2….), is optional.
We design an algorithm to get a solution of a given problem. A problem can be solved in more than one ways.
Characteristics of a good Algorithm
An algorithm should have the following characteristics −
 Precise: Algorithm should be accurate i.e. Instructions should be precise..
 Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and their
inputs/outputs should be clear and must lead to only one meaning.
 Input − An algorithm should have 0 or more well-defined inputs.
 Output − An algorithm should have 1 or more well-defined outputs and Outputs are derived from the
input by applying the algorithm. The outputs should match the desired output.
 Finiteness − Algorithms must terminate after a finite number of steps & it should have finite numbers of
inputs. Operations should be done exactly and in finite amount of time
 Feasibility − should be feasible with the available resources.
 Independent − An algorithm should have step-by-step directions, which should be independent of any
programming code.
 Uniqueness: No steps should be repeated.

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:

Rules for Creating Flowchart:


A flowchart is a graphical representation of an algorithm. It should follow some rules while creating a
flowchart
Rule 1: Flowchart opening statement must be ‘start’ keyword.
Rule 2: Flowchart ending statement must be ‘end’ keyword.
Rule 3: All symbols in the flowchart must be connected with an arrow line.

Basic Symbols and Notations used in Flowchart Designs


1. Terminal: The oval symbol indicates Start, Stop and Halt in a program’s logic flow. A pause/halt is
generally used in program logic under some error conditions. Terminal is the first and last symbols in the
flowchart.

 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.

A flowchart that describes this program is shown.

Start

Enter a number

Add 2 to the number

Print the Result

Stop

The Python code that corresponds to this flow chart is:


# start
num = input("Enter a number: ")
num = float(num)
num_plus_2 = num + 2
print(num_plus_2)
# end

Example-2 :
Add two numbers and print the result

Start
t
Declare Num1, Num2, Sum

Read Num1,Num2

Sum=Num1+Num2

Print the Sum

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

Input num1, num2

True
If
num>num2
?

False

Display num2
Display num1

Stop

The Python code that corresponds to this flow chart is:

a = int(input("Enter the first number: "))


b = int(input("Enter the second number: "))
if(a >= b):
print(a, "is greater")
else:
print(b, "is greater")
More Examples
Find the largest among three different numbers entered by the user.

Find all the roots of a quadratic equation ax2+bx+c=0


Find the Fibonacci series till term≤1000.

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.

Why is Decision Table Important?


A decision table is an outstanding technique used for testing and requirements management. Some of the
reasons why the decision table is important include:
 Decision tables are very much helpful in test design technique.
 It helps testers to search the effects of combinations of different inputs and other software states that
implement business rules.
 It provides a regular way of stating complex business rules which benefits the developers as well as the
testers.
 It assists in the development process with the developer to do a better job. Testing with all combination
might be impractical.
 It the most preferable choice for testing and requirements management.
 It is a structured exercise to prepare requirements when dealing with complex business rules.
 It is also used in model complicated logic.
Advantages of Decision Table in Software Testing
There are different advantages of using the decision table in software testing such as:
 Any complex business flow can be easily converted into the test scenarios & test cases using this
technique.
 Decision tables work iteratively. Therefore, the table created at the first iteration is used as the input
table for the next tables. The iteration is done only if the initial table is not satisfactory.
 Simple to understand and everyone can use this method to design the test scenarios & test cases.
 It provides complete coverage of test cases which help to reduce the rework on writing test scenarios &
test cases.
 These tables guarantee that we consider every possible combination of condition values. This is known
as its completeness property.
Decision Table: Example
A Decision Table is a tabular representation of inputs versus rules, cases or test conditions. Let’s take an
example and see how to create a decision table for a login screen:
The condition states that if the user provides the correct username and password the user will be redirected to
the homepage. If any of the input is wrong, an error message will be displayed.
Conditions Rule 1 Rule 2 Rule 3 Rule 4
Username F T F T
Password F F T T
Output E E E H
In the above example,

 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.

You might also like