CHAPTER - 2
PROBLEM SOLVING USING COMPUTER
2.1 Problem Solving Steps
Steps followed for developing computer software:
To develop a good quality computer program or software it is
required to go through the following steps:
1. Problem analysis: Client's problems, whose computer based
solution has to be developed, should first be properly analyzed. It
is the most important stage because most of the software failure
occur due to misinterpretation of the problem but not due to
coding. Problem analysis should clearly specify the following task.
a. Well defined objectives
b. Input and output requirements
c. Processing requirements
d. Feasibility analysis
2. Algorithm development and flowchart: In this stage, what steps
the computer should take to solve identified problem are defined.
A set of ordered steps required to develop a program is known as
an algorithm. It is written without any programming language
specific symbols/syntax.. It is a verbal form of a program.
If we represent the algorithm in a diagrammatic form that
illustrates the sequence of operations to be performed, it is known
as flowchart. Flowchart facilitate communication between
programmers and business people.
Some commonly used symbols in flowchart are as follows:
Symbol Purpose
Start/stop
Processing
Input/output
Decision
|1|
Predefined process/Function
Connector
Flow line
The advantages of a flowchart design are as follows:
i. Communication: Better way of communicating the logic of
a system to all concerned.
ii. Effective analysis: Problem can be analyzed in more
effective way.
iii. Proper documentation: Serve as a good program
documentation, which is needed for various purposes.
iv. Efficient coding: Act as a guide or blueprint during the
systems analysis and program development phase.
v. Proper debugging: Helps in debugging process.
vi. Efficient program maintenance: The maintenance of
operating program becomes easy with the help of flowchart.
It helps the programmer to put efforts more efficiently on that
part.
Basic guidelines to be followed to draw a flowchart are as follows:
i. Standard flowchart symbols should be used according to the
purpose.
ii. There should be a logical start and stop.
iii. It should be clear, neat and easy to follow. There should not
be any room for ambiguity in understanding the flowchart.
iv. The direction of the flow of a procedure or system is from left
to right or top to bottom.
v. English like language should be used in flowcharts, not
specific programming language.
vi. It is useful to test the validity of the flowchart by passing
through it with a simple test data.
3. Pseudo code: Pseudo code is an outline of a program, written in a
form that can easily be converted into real programming
statements. It is a mixture of structured English and actual code
available in specific programming language. It is only used as a
logic development tool that helps to predetermine the possible
future problems and their solution.
4. Coding: Coding is the process of transforming algorithm or
flowchart into computer understandable form. Coding can be done
in any suitable programming language.
5. Compilation and execution: Actual codes written in high level
language should be converted to low level language before an
executable program is created. Whole process is known as
compilation and execution.
The process of converting a program written in high-level
language (source program) to an executable program is called
compilation and execution.
In this process, source program is converted to object code
(low level language) through a complier at first.
Then, object code is linked with the library functions through
linker. That gives real values to all symbolic address (in
object code), there by producing an executable program.
Source Program
Compiler
Object code/file
Runtime library/
library function Linker Executable program
Fig.: Illustration of compilation and execution process.
If there is any illegal instruction is the source program/code,
compiler lists all the errors during compilation.
6. Debugging and testing: Debugging is the process of isolating and
correcting any type of errors. Programming errors are discovered
and corrected in this [Link] is a process to check program
whether it gives what actually the customer wants. Every program
does something right, but it does not mean that it is thing we want.
Types of errors:
Generally, errors are classified into following types:
|3|
i. Syntax error: Any violation of rules of the programming
language results in syntax error. Example: missing of
semicolon at the last of any statement.
ii. Run-time error: Errors such as mismatch of data types or
referencing an out of range array element go undetected by
compiler. A program with these mistakes will run but
produce the erroneous results.
iii. Logical error: These errors are related to the logic of the
program execution. Such actions as taking the wrong path,
failure to consider a particular condition and incorrect order
of evaluation of statements belong to this category.
iv. Latent error: These are hidden errors that shows up only
when a particular set of data is used.
Example: r=(x+y)/(p-q); This expression generates error
when p=q.
Debugging techniques:
i. Error isolation
ii. Tracing
iii. Watch values
iv. Breakpoints
v. Stepping
7. Program documentation: All the above processes should be
properly documented for future reference of both the original
programmer and beginner.
It is the process of making a program well structured by using
appropriate comments and other techniques.
It should start from very beginning with the help of which the
program would be easy to understand by other person (user or
other programmer).
2.2 Algorithm and Flowchart Examples
1. Write an algorithm and flow chart of the distance between two
points (x1, y1) and (x2, y2), governed by formula D2 = (x2 – x1)2 +
(y2 – y1)2, where x1, x2, y1, y2 are given by user, but should not
be zero.
Algorithm:
Step 1: Start
Step 2: Declare variable x1, x2, y1, y2, D
Step 3: Read co-ordinates of points x1, y1 & x2, y2
Step 4: If x1 = 0 or x2 = 0 or y1 = 0 or y2 = 0 go to step 3 otherwise
continue
Step 5: Calculate D = (x2-x1)2 + (y2-y1)2
Step 6: Display the value of 0
Step 7: Stop
Start
Declare variables
x1, y1, x2, y2, D
Read x1, y1, x2 & y2
Is
x1=0 or
True x2 = 0
or y1=0
or y2 = 0
False
Calculate
D= (x2 - x1)2 + (y2-y1)2
Display D
Stop
|5|
2. Draw a flowchart and algorithm to find roots of a quadratic
equation (ax2 + bx + c = 0). Include all three conditions of the
determinant.
Algorithm:
Step 1: Start
Step 2: Declare variables a, b, c, real, imag, d, x1, x2
Step 3: Read the coefficients a, b & c
Step 4: Calculate d = b2-4ac
Step 5: If d>0 then
–b+ d –b– d
Calculate, x1 = and x2 =
2a 2a
Display roots are real & unequal
Display value of x1 & x2
else if d =0 then
–b
Calculate, x1 = x2 =
2a
Display roots are real & equal
Display value of x1 & x2
else
Calculate, d = – d
–b d
Real = & imag =
2a 2a
Display roots are imaginary
Display x1 = real + (imag) i
Display x2 = real – (imag) i
Step 6: Stop
Start
Declare variables
a,b,c,x1,x2,d real,imag
Read a, b & c
Calculate
d= b2 -4ac
Is True
d>0?
False Display roots are
real & equal
True Is
d=0? Calculate
x1= -b + d
Display roots are False
2a
real & equal
Display roots are x2= -b - d
inaginary 2a
Calculate
-b
x1 = x2 = 2a
Calculate Display
d=-d x1 & x2
Display real = -b
x1 & x2 2a
imag = d
2a
Display
x1 = real + imagi
x2 = real - imagi
Stop
Correction: Calculate d=b2-4ac in the flowchart
|7|
3. Write an algorithm and draw the flowchart to calculate and
display the factorial of an integer number.
Alogrithm:
Step 1: Start
Step 2: Declare variables n,i,fact
Step 3. Read the number n
Step 4. [Initialize]
i=1, fact=1
Step 5. Repeat step 5.1 and 5.2 until i≤n
Step 5.1. fact=fact*i
Step 5.2. i=i+1
Step 6. Print fact
Step 7. Stop
Flowchart:
Note: Step 2 of algorithm is missing in the Flowchart
4. Write an algorithm to print the following pattern.
54321
5432
543
54
5
Algorithm:
Step 1: Start
Step 2: Declare variables i=1, j
Step 3: Repeat steps 3.1,3.2 and 3.3 until i≤5
Step 3.1: Initialize: j=5
Step 3.2: Repeat steps 3.2.1 and 3.2.2 until j≥i
Step 3.2.1: Display j
Step 3.2.2: Update: j=j-1
Step 3.3: Go to next line and update: i=i+1
Step 4: Stop
Note: It is useful to know about nested loops for solving this problem
5. Write and algorithm and a flowchart to read a 5 digit number
and check whether the number is palindrome or not.
Algorithm:
Step 1: Start
Step 2: Declare variables n, r, s, t
Step 3: Read a five digital number n
Step 4: Assign t=n and s=0
Step 5: Calculate,
r = n% 10
s = s*10 + r
n = n/10
Step 6: If n=0
goto step 7
else
goto step 5
Step 7: If s=t then
|9|
display the number is palindrome
else
display the number is not palindrome
Step 8: Stop
Flowchart:
Start
Declare variables
n,r,s,t
Read five digits number n
Assign t = n & s=0
Calculate
r = n%10
s= s*10+r
n=n/10
false Is
n=0?
True
Is False Display number is
s=t? not palindrome
True
Display number is
palindrome
Stop
6. Draw flowchart to check whether a number is Armstrong or
not.
Start
Declare variables
Read a number n
Initialize sum = 0
Assign t = n
r = n mod 10
sum = sum+r3
n = n/10
Yes Is
n>0?
No
Is No
t = sum?
Yes
Display number is Display number is
Armstrong not Armstrong
Stop
| 11 |