Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Chapter-5
PROBLEM SOLVING METHODOLOGY
Introduction
The term problem solving is used in many disciplines, sometimes with different perspectives and
often with different terminologies.
The problem-solving process starts with the problem specification and end with a correct program.
The steps to follow in the problem-solving process are:
Problem definition
Problem Analysis
Algorithm development
Coding
Testing & Debugging
Documentation & Maintenance
The stages of analysis, design, programming, implementation and maintenance form the life cycle
of the system.
Problem definition:
This step defines the problem thoroughly. This step includes understanding the problem very well.
Here we define the problem. In practice to solve any complex real life problem, first we have to
define the problem by answering to the following questions.
What the computer program does?
What tasks will it perform?
What kind of data will it use and where will it get data from?
What will be the output of the program?
How does the program interact with the computer user?
Specifying the problem requirements makes you to state the problem clearly and unambiguously and
to gain a clear understanding of what is required for its solution. Your objective is to eliminate
unimportant aspects and to focus on the root problem, and this may not be as easy as it sound.
1|Page
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Problem Analysis:
Analyzing the problem or analysis involves identifying the following:
Inputs, i.e. the data you have to work with.
Outputs i.e. the desired results.
Any additional requirements or constraints on the solutions.
ALGORITHM
An Algorithm is a step-by-step finite sequence of instructions to solve a given problem.
The word algorithm originates from the word ‘algorism’ which means process of doing arithmetic
with Arabic numerals.
In 9th-century Arab Mathematician, Mohammed Al-Khwarizmi, who developed methods for
solving problems which used specific step-by-step instructions.
Characteristics of algorithm:
A well defined algorithm has the five basic characteristics; as follows
1. Input: Algorithm starts with procedural steps to accept input data. The algorithm must
accept one or more data to be processed.
2. Definite: Each operational step or operation must be definite i.e. each and every instruction
must clearly specify that what should be done.
3. Effective: Each operational step can at least in principle is carried out by a person using a
paper and pencil in a minimum number of times.
4. Terminate: After some minimum number operation algorithm must come to an end.
5. Output: An algorithm is written to solve the problem, therefore it must produce one or
more computed result or answer called output.
Example: An algorithm to find the area of a rectangle can be expressed as follows:
Given the length l and the breadth b, this algorithm finds the area of rectangle
rec. Step 1: START
Step 2: [Read the vales of l, b]
INPUT l, b
Step 3: [Calculate are of rectangle]
rec = l * b
Step 4: [Print the area of rectangle]
OUTPUT rec
Step 5: [End of Algorithm]
STOP
2|Page
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
In the above example, we used = that represents assignment.
Design an algorithm to find the average of four numbers
Step 1: START
Step 2: INPUT A, B, C, D
Step 3: [Calculate] AVG = (A+B+C+D)/4
Step 4: OUTPUT AVG
Step 5: STOP
Design an algorithm to calculate the Simple Interest, given the Principal (P), and Rate (R)
and Time (T)
Step 1: START
Step 2: INPUT P, T, R
Step 3: [Calculate] SI = (P*T*R)/100
Step 4: OUTPUT SI
Step 5: STOP
. Given the principal amount p, rate of interest r and time period p, this algorithm finds the
Compound interest (CI)
Step 1: START
Step 2: [Read the values of p, r, n]
INPUT p, r, n
Step 3: [Calculate compound interest]
CI = p * pow(1+ r/100, t) - p
Step 4: [print the computed results]
OUTPUT CI
Step 5: [End of Algorithm]
STOP
Design an algorithm to find the greatest of three number (A, B, C)
Step 1: START
Step 2: INPUT A, B, C
Step 3: [Assign A to
large] Large = A
Step 4: [Compare large and B]
If( B > large )
Large = B
endif
3|Page
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Step 5: [Compare large and C]
If( C > large )
Large = C
endif
Step 6: [Print the largest number]
OUTPUT large
Step 7: STOP
Design an algorithm to find factorial of a number ( n )
Step 1: START
Step 2: INPUT n
Step 3: [Initialize factorial to 1]
Fact = 1
Step 4: [compute the factorial by successive multiplication]
Repeat for I = 1 to n
Fact = Fact * I
[End of Step 4 for loop]
Step 5: [Print factorial of given number]
OUTPUT Fact
Step 6: STOP
Design an algorithm to find Fibonacci series ( n )
Step 1: START
Step 2: INPUT n
Step 3: [Initialize the
variables] First = 0
Second = 1
Term = 2
Step 4: [Print the values of first and second]
PRINT First, Second
Step 5: Third = First + Second
Step 6: Repeat while ( term <= n)
PRINT Third
First = Second
Second = Third
Third = First + Second
Term = Term + 1
[End of While loop]
Step 7: STOP
4|Page
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Design an algorithm to find the GCD of two numbers ( A, B )
Step 1: START
Step 2: INPUT A, B
Step 3: Repeat while ( B != 0 )
rem = A % B
A=B
B = rem
[End of While loop]
Step 4: [Print the last divisor]
PRINT A
Step 5: STOP
Advantage of Algorithm
1. It is a step-by-step representation of a solution to a given problem, which is very easy to
understand.
2. It has got a definite procedure, which can be executed within a set period of time.
3. It is independent of programming language.
4. It is easy to debug as every step has got its own logical sequence.
Disadvantage of Algorithm
It is time-consuming
An algorithm is developed first which is converted into a flowchart and then into a computer
program.
Analysis of Algorithm
There may be more than one approach to solve a problem. The choice of a particular algorithm
depends on the following performance analysis and measurements.
o Space complexity: The amount of memory needed by the algorithm to complete its run.
o Time Complexity: The amount of time, the algorithm needed to complete its run.
When we analyze an algorithm depends on input data, there are three cases
o Best case : The amount of time a program might be expected to take on best possible input
data.
o Average case: The amount of time a program might be expected to take on typical (or average)
input data.
o Worst case: The amount of time a program would take on the worst possible input
5|Page
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
configuration.
FLOWCHART
A Flowchart is a pictorial or graphical representation of an algorithm.
Flowchart plays an important role in the programming of a problem and helpful in understanding
the logic of program.
Once the flow chart is drawn, it becomes easy to write program in any high level language.
Flowcharts are classified into two categories:
1. Program Flowchart
2. System Flowchart
Program flowchart specifies only the flow of operations of an algorithm, pictorially. But program
Flowchart never specify the computer environment in which program will be executed.
System flowchart is a flowchart which gives an overall view of computer system environment in
addition to the flow of operations for an algorithm or program to be solved. It provides information
such as whether program is stored on magnetic disk, whether data is coming from keyboard or also
the result are printed out on a printer.
Importance of Flowchart
1. Communication: Flowcharts are better way of communication of the logic of a program.
2. Effective Analysis: With the help of flowchart, problem can be analyzed in more effective
way.
3. Proper documentation: Program flowcharts serve as a good program documentation, which is
needed for various programs.
4. Efficient coding: The flowchart acts as guide or blueprint during the system analysis and
program development phase.
5. Proper Debugging: The flow chart helps in debugging process.
6. Efficient program maintenance: The maintenance of a program becomes easy with the help
of flowcharts.
6|Page
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Symbols Used In Flowcharts
7|Page
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Ex 1 : Flowchart to interchange the values of two variables
Ex 2: Flowchart to find the area and circumference of a circle
Ex 3: Flowchart to find the area of triangle (sides)
8|Page
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Ex 4: Write a program, design a flow chart and an algorithm to find the larger of two numbers.
Step 1: Start
Step 2: Input A and B
Step 3: If(A>B) then
Output A
Else
Output B
[End if]
Step 4: Stop
9|Page
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
5: Write a Flowchart to find the sum and average of numbers.
6: Write a Flowchart to find the sum of digits in a given number.
10 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
7: Write a Flowchart to check whether a number is prime or not
Pseudo Code:
Pseudo code a short hand notation for the control structure and other elements of a programming
language. It is neither an algorithm nor a program and is near to program. Thus it is easy to convert pseudo
code into a program.
Development of programs or computer solution
In this step, we develop the complete solution in the form of a computer program.
Coding or programming
It is the process of translating the algorithm or flowchart into the syntax of a given programming
language. You must convert each step of the algorithm into one or more statements in a programming
language such as C, C++ etc.
11 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Testing and Debugging
Testing means running the program, executing all its instructions/functions and checking the logic by
entering sample data to check the output.
Debugging is the process of identifying and correcting or removing the Bugs (errors).
There are four types of errors. They are
Syntax errors
Run-time errors
Semantic errors
Logic errors (bugs)
Syntax Error
Syntax is the set of rules which should followed while creating the statements of the program.
The grammatical mistakes in the statements of the program are called syntax errors.
Example:
void main( )
{
int a, b;
cout << ‘Enter the numbers” ;
cin >> a >> b;
cout << a + b
}
In the example program, the fourth statement produces an syntax error as the missing semicolon.
Run-time Error
During execution of the program, some errors may occur. Such errors are called run-time
errors.
Example: Divide by zero.
Semantic Error
An error, which occurs due to improper use of statements in programming language.
Consider an expression C = A + B, indicating the values of the variable A and B are added and
assigned to variable C.
If we written A + B = C, through the values of A and B are added, it cannot be assigned to variable
C written to the right of = Sign.
This is semantic error.
12 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Logical Error
Logical errors occur when there are mistakes in the logic of the program.
Unlike other errors logical errors are not displayed while compiling because the compiler does not
understand the logic of the program.
Example: To find the area of the circle, the formula to be used is area = 3.14 * r * r. But if we
written area = 3.14 * 2 * r, then the required output is not obtained even though the program is
successfully executed.
Documentation and Maintenance
Documentation is a reference material which explains the use and maintenance of the
program application for which it has been written.
There are two types of documentation.
o Internal Documentation
o External Documentation.
Internal Documentation:
This is also known as technical documentation.
It is meant for the programmer who may update the program code at later stages.
It is done by:
o Defining meaningful variable names.
o Including comments in program code.
o Presenting the program code clearly.
External Documentation:
The program or application is supported with additional textual information about the application.
It is useful for the user, administrator or developer.
Accordingly there are 3 types of documents:
a. User’s manual
b. Administrator’s manual
c. Developer’s manual
User’s manual contains the operating instructions of the application for the user.
Administrator’s manual contains instructions for the user who is going to manage the application in
future.
Developer’s manual contains low level flowcharts, data dictionaries, decision tables, files and complete
system chart so that the application can be easily updated.
13 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Maintenance:
Program maintenance means periodic review of the programs and modifications based on
user’s requirements.
Maintenance is a continuous task
Documentation plays an important role in program maintenance. It helps speedy and efficient
maintenance.
Programming Constructs
A programming constructs is a statement in a program.
There are 3 basic programming constructs.
o Sequential Constructs
o Selection Constructs
o Iteration Constructs
Sequential Constructs:
The program statements are executed one after another, in a sequence.
There will be no backward or forward references.
Single Entry
s1
S2
s3
s4
Single exit
s1, s2, s3 and s4 are simple statements which could be any one of the following:
o Input Statement
o Assignment Statement
o Output Statement
Input Statement
This statement is used to input values into the variables from the input device.
Example: INPUT A, B, C
This input statement indicates that 3 values should be read from the input device will be put into the
variables a, b and c respectively. Variable refers to a specific memory locations.
14 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Assignment Statement
This statement is used to store a value in a variable.
In many languages ‘=’ is used as the assignment operator.
Example: a = b; (The value of the variable b is assigned to variable a.)
c= 10; (This means that value 10 is assigned to the variable c.)
z=x + y; (The values of the variables x and y are added and the sum is stored in variable
into the variable z.)
Output Statement
This statement is used to display the values of variables on the standard output device.
The word OUTPUT, DISPLAY, WRITE and PRINT are used for this purpose.
Example: OUTPUT C;
Selection construct
It is also known as conditional construct.
This structure helps the programmer to take appropriate decision.
There are five kinds of selection constructs, viz.
o Simple – if
o if – else
o if – else – if
o Nested – if
o Multiple Selection
Simple - if :
This structure helps to decide the execution of a particular statement based on a condition.
This statement is also called as one-way branch.
15 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
The general form of simple – if statement is:
if (Test Condition) // This Condition is true
Statement 1;
Statement 2;
Here, the test condition is tested which results in either a TRUE
or FALSE value. If the result of the test condition is TRUE then
the Statement 1 is executed. Otherwise, Statement 2 is executed.
Ex: if(a>b){
print a is greater;
print b is greater;
}
if – else statement :
This structure helps to decide whether a set of statements should be executed or another set of
statements should be executed.
This statement is also called as two-way branch.
The general form of if – else statement is:
if (Test Condition)
Statement 1;
else
Statement 2;
Here, the test condition is tested. If the test-condition
is TRUE, statement-1 is executed. Otherwise
Statement 2 is executed.
Ex: if (n%2=0)
Print n, “is even”;
else
Print n, “is odd”;
16 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
if – else - if statement :
This structure helps the programmer to decide the execution of a statement from multiple
statements based on a condition.
There will be more than one condition to test.
This statement is also called as multiple-way branch.
The general form of if – else – if statement is:
if (Test Condition 1)
Statement 1;
else
if (Test Condition 2)
Statement 2;
else
………..
else
if( test Condition N)
Statement N;
else
default Statement;
Here, Condition 1 is tested. If it is TRUE, Statement 1 is executed control is transferred out of the
structure. Otherwise, Condition 2 is tested. If it is TRUE, Statement 2 is executed control is
transferred out of the structure and so on.
If none of the condition is satisfied, a statement called default statement is executed.
17 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Example:
if( marks > = 85 )
PRINT “Distinction”;
else
if( marks > = 60 )
PRINT “First Class”;
else
if( marks > = 50 )
PRINT “Second Class”;
else
if( marks > = 35 )
PRINT “Pass”;
else
PRINT “Fail”;
Nested if statement :
The statement within the if statement is another if statement is called Nested – if statement.
The general form of Nested – if statement
is: if (test condition 1)
if (test condition 2)
statement -1;
else
statement -2;
else
if( test condition -3)
statement- 3;
else
statement- 4;
18 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Ex: To find the greatest of three numbers a, b and c.
if ( a>b )
if ( a > c )
OUTPUT a;
else
OUTPUT c;
else
if ( b > c )
OUTPUT b;
else
OUTPUT c;
Multiple Selection Construct or Switch statement :
If there are more than two alternatives to be selected, multiple selection construct is used.
The general form of Switch statement
is: Switch ( Expression )
{
Case Label-1: Statement 1;
Break;
Case Label-2: Statement 1;
Break;
…………..
Case Label-N: Statement N;
Break;
Default : Default- Statement;
}
19 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Ex: To find the name of the day given the day number
Switch ( daynumber )
{
Case 1: PRINT “Sunday”;
Break;
Case 2: PRINT “Monday”;
Break;
Case 3: PRINT “Tuesday”;
Break;
Case 4: PRINT “Wednesday”;
Break;
Case 5: PRINT “Thursday”;
Break;
Case 6: PRINT “Friday”;
Break;
Case 7: PRINT “Saturday”;
Break;
default: PRINT “Invalid Day Number”;
}
Iterative Constructs or Looping
The process of repeated execution of a sequence of statements until some condition is
satisfied is called as iteration or repetition or loop.
Iterative statements are also called as repetitive statement or looping statements.
There are two iterative constructs, viz.
o Conditional Looping
o Unconditional Looping
Conditional Looping :
This statement executes a group of instructions repeatedly until some logical condition is satisfied.
The number of repetitions will not be known in advance.
The two conditional looping constructs are:
o While
o do while
Unconditional Looping :
This statement executes a group of instructions is repeated for specified number of times.
The unconditional looping constructs is for statement.
20 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
while Constructs:
This is a pre-tested loop structure.
This structure checks the condition at the beginning of the structure.
The set of statements are executed again and again until the condition is true.
When the condition becomes false, control is transferred out of the structure.
The general form of while structure
is While ( Test Condition)
Statement 1;
Statement 2;
……..
Statement n;
End of While
Is F
test-
c Ondition?
statement-
1
statement-
2
Example:
i = 1;
While ( i < = 5)
PRINT i;
i = i + 1;
end of while
Output: 1 2 3 4 5
21 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
do -while Constructs:
This is a post-tested loop structure.
This structure checks the condition at the end of the structure.
The set of statements are executed again and again until the condition is true.
When the condition becomes false, control is transferred out of the structure.
The general form of while structure
is do
Statement 1;
Statement 2;
……..
Statement n;
while ( Test Condition)
;;End of While
Example:
sum
=0 ;;;;i =
1;
do
sum = sum + i;
i = i + 1;
while ( i < = 100);
for Constructs:
This structure is the fixed execution structure.
This structure is usually used when we know in advance exactly how many times a set of
statements is to be repeatedly executed again and again.
This structure can be used as increment looping or decrement looping structure.
The general form of for structure is as follows:
for ( Expression 1; Expression 2; Expression 3)
{
Statement 1;
Statement 2;
Statement N;
}
Where, Expression 1 represents Initialization
Expression 2 represents Condition
22 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Expression 3 represents Increment/Decrement
23 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Ex:
sum = 0;
for ( i=1; i<=10; i++)
sum = sum + i;
Characteristics of a good program:
The best program to solve a given problem is one that requires less space in
memory, takes less execution time, easy to modify and portable.
Modification: A good program is the one which allows any modifications
easily whenever needed.
Portability: A good program is the one which can be run on different type of
machine with a minimum or no change.
Life cycle of a computerized process
24 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
Approaches to problem solving:
1. Top-down design:
Top-down design involves dividing a problem into sub-problems and further dividing the sub-
problems into smaller sub-problems until it leads to sub-problems that can be implemented as
program statements.
Where A is the main problem and remaining are the sub-problems.
The top-down approach is taken for program design; the programs can be developed easily,
quickly, committing a minimum of errors.
Example: Convert a given temperature in Fahrenheit to and Centigrade and vice-versa.
25 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
2. Stepwise refinement:
The process of breaking down the problem at each stage to obtain a computer solution is
called stepwise refinement.
3. Bottom-up design:
This approach is the reverse of top-down design.
The process starts with identification of set of modules which are either available or to be
constructed.
In this case, small subtasks are implemented and tested first and then such sub-problems are
combined together to give a larger solution to the entire problem.
An attempt is made to combine the lower level modules to form modules of high level.
Examples include object oriented programming using C++.
Programming techniques:
Programming methods in practice since evolution of computer programming are as follows
i. Unstructured programming
ii. Procedural programming
iii. Modular programming
iv. Object-oriented programming
26 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
i. Unstructured programming
Usually, people start learning Programming by writing small and simple programs consisting only
of one main program. Here “main program” stands for a sequence of commands or statements which
modify data which is global throughout the whole program. This can illustrate as shown in the following
figure.
Program
Main program
data
Unstructured programming. The main program directly operates on global data
ii. Procedural programming
This method allows us to combine the returning sequences of statements into one single
place.
A procedure call is used to invoke the procedure.
After the sequence is processed, flow of control proceeds right after the position where
the call was made.
The above figure represents a single program which is divided into small pieces called procedures.
To enable usage of general procedures or groups of procedures also in other programs, they must be
separately available. For that reason, modular programming allows grouping of procedures into
modules.
iii. Structured programming
Structured programming is a method of programming by using the following types of code
structures to write program:
[Link] of sequentially executed statements.
b. Conditional execution of statements (i.e., if statements)
c. Looping or iteration (i.e., for, do-while and while statements)
d. Structured subroutines calls (i.e., functions)
27 | P a g e
Chapter 5- Problem Solving Methodology Sri SaiRam PU College
i. Modular programming:
The process of splitting the lengthier and complex programs into number of smaller units
(modules) is called modularization and programming with such an approach is called
modular programming.
This technique provides grouping of procedures which are of common functionality into separate
modules .
Each module can have its own data.
Advantages of modular programming:
1. Reusability: If a particular set of instructions is to be executed repeatedly from several
different places within the program, then we can make this group of instructions as one module
and call it whenever necessary. This avoids rewriting the s ame function every time we need it.
2. Debugging is easier: Since each module is smaller and clearer, the user can easily locate the
errors and correct them.
3. Building library: It is possible for the programmer to build his/her own library of the most
commonly used functions. This reduces the time and space requirements of the programs.
4. Portability: Since the functions and library are common across different Operating Systems, it
is easier for us to port and execute them without much or no modifications.
CHAPTER 5– PROBLEM SOLVING METHODOLOGY BLUE PRINT
VSA (1 marks) SA (2 marks) LA (3 Marks) Essay (5 Marks) Total
01 Question 01 Question 01 Question 01 Question 11 Marks
***************
28 | P a g e