You are on page 1of 19

Chapter 2

Problem Solving Using Computer

By Er. Rupesh Kumar Nidhi 1


Problem Solving

Ø Computers are used to solve real-life problems of business and


science

Ø Any problem which can be solved manually can also be solved by the
computer, but the computer has to be programmed to solve the
problem

Ø Before programming, we must know how to solve the problem, so


that we can program it

By Er. Rupesh Kumar Nidhi 2


Problem Analysis

Ø The process where we break down problems into its components so


that the problems can easily be understood

Ø The way to do this is to write an ALGORITHM of the problem.

Ø Problem Definition: The problem can be defined on the basis of the


following,
vType of input variable required
vType of output variable required

By Er. Rupesh Kumar Nidhi 3


Problem Analysis…

Ø Suppose you are assigned to solve an arithmetic problem and you


are not familiar with steps involved in solving that problem. In such
a case. You will not be able to solve the problem.

Ø The same applies to writing computer programs. A computer


programmer cannot write the instructions to be followed by a
computer unless he knows how to solve the problem manually.

By Er. Rupesh Kumar Nidhi 4


Algorithm
Ø An effective step-by-step procedure for solving a problem in a finite
number of steps

Ø A finite set of well-defined instructions or step-by-step description of the


procedure written in human readable language for solving a given
problem

Ø Division of a problem into small steps which are ordered in sequence


and easily understandable.

By Er. Rupesh Kumar Nidhi 5


Algorithm…

Characteristics of algorithm:

Ø Finiteness: An algorithm should have finite number of steps and it should end
after a finite time

Ø Input: An algorithm may have many inputs or no inputs at all

Ø Output: It should result at least one output

Ø Definiteness: Each step must be clear, well-defined and precise. There should be
no any ambiguity

Ø Effectiveness: Each step must be simple and should take a finite amount of time.

By Er. Rupesh Kumar Nidhi 6


Algorithm…

Guidelines for Developing an Algorithm:

Ø An algorithm will be enclosed by START (or BEGIN) and STOP (or END)

Ø To accept data from user, generally used statements are INPUT, READ, GET or
OBTAIN

Ø To display result or any message, generally used statements are PRINT, DISPLAY, or
WRITE

Ø Generally, COMPUTE or CALCULATE is used while describing mathematical


expressions and based on situation relevant operators can be used.

By Er. Rupesh Kumar Nidhi 7


Algorithm…

Example:

Calculation of Simple Interest,

Step 1: Start
Step 2: Read principle (P), time (T) and rate (R)
Step 3: Calculate I = P*T*R/100
Step 4: Print I as Interest
Step 5: Stop

By Er. Rupesh Kumar Nidhi 8


Algorithm…

Advantages of an Algorithm:
Ø Effective Communication: Since algorithm is written in English like
language, it is simple to understand step-by-step solution of the
problems.
Ø Easy Debugging: Well-designed algorithm makes debugging easy so
that we can identify logical error in the program.
Ø Easy and Efficient Coding: An algorithm acts as a blueprint of a
program and helps during program development.
Ø Independent of Programming Language: An algorithm is independent
of programming languages and can be easily coded using any high
level language.

By Er. Rupesh Kumar Nidhi 9


Algorithm…

Disadvantages of an Algorithm:

Ø Developing algorithm for complex problems would be time


consuming and difficult to understand

Ø Understanding complex logic through algorithms can be very


difficult.

By Er. Rupesh Kumar Nidhi 10


Flowchart

Ø A pictorial or diagrammatic representation of an algorithm using


standard symbols

Ø A graphical representation that explains the sequence of operations


to be performed in order to solve a problem under consideration.

By Er. Rupesh Kumar Nidhi 11


Flowchart…

By Er. Rupesh Kumar Nidhi 12


Flowchart…
Guidelines for Preparing Flowchart:
Ø Standard symbols should be used while drawing flowchart
Ø Ensure that flowchart has START (or BEGIN) and STOP (or END).
Ø Should be neat, clean and easy to follow, no any ambiguity.
Ø The usual direction of flowchart is from top to bottom or from left to right.
Ø The terminal symbol, that is, START/BEGIN or STOP/END should have only one
flow line.
Ø Only one flow line should come out from process symbol.
Ø Only one flow line should enter a decision symbol, but two or three flow-lines, one
for each possible answer, can leave the decision symbol.
Ø If the flowchart is lengthy and complex connector symbol should be used to reduce
the number of flow lines.
Ø Avoid intersection of flow lines.
Ø Use annotation symbol to describe steps more clearly.

By Er. Rupesh Kumar Nidhi 13


Flowchart…
Example:
Flowchart example for calculating simple interest,

By Er. Rupesh Kumar Nidhi 14


Compilation and Execution

Ø The process by which source codes of a computer (programming)


language are translated into machine codes is known as compilation

Ø After compilation if everything is ok, the code is going under other


process that is known as execution. We can get the required output
after execution process.

By Er. Rupesh Kumar Nidhi 15


Debugging and Testing

Ø The process of finding and removing errors (also sometimes called


bugs) from a program is known as debugging.

Ø One simple method of debugging is to place print statements


throughout the program to display the values of variables.

vDisplays the dynamics of a program and allows us to examine


and compare the information at various points.

vOnce the location of an error is identified and the error is


corrected, the debugging statements may be removed.

By Er. Rupesh Kumar Nidhi 16


Debugging and Testing…
Generally programmers commit three types of errors,
Syntax errors: Those errors which are raised from violating the rules
of programming language. On encountering these errors, a computer
displays error message. It is easy to debug.
Logic errors: Those which raised when programmers proceed the
logic process in wrong way or miss the some statements. It is difficult
to debug such errors because the computer does not display them.
Run-time errors: Those which occur when programmers attempt to
run ambiguous instructions. They occur due to infinite loop statement,
device errors, software errors, Some of runtime errors are Division by
zero, Null pointer assignment, Data over flow, etc.

By Er. Rupesh Kumar Nidhi 17


Debugging and Testing…
Ø Testing is the process of reviewing and executing a program with the
intent of detecting errors

Ø Testing can be done manually and computer based testing

Ø Manual Testing is an error-detection process and is done before the


computer based testing begins. Manual testing includes code
inspection by the programmer, code inspection by a test group and a
review by a peer group.

Ø Computer based testing is done by computer with the help of compiler


(a program that changes source codes into machine codes word by
word).
By Er. Rupesh Kumar Nidhi 18
Program Documentation
Ø Program Documentation refers to the details that describe a program

Ø While writing programs ,it is good programming practice to make a brief explanatory
note on the programmer program segments. This explanatory note is called
comment. It explains how the program works and interact with it. Thus ,it helps other
programmers to understand the program.

Ø There are two types of documentation:

v Internal documentation: Selection of meaningful variable names and use of


comments. For example, Area = Breadth * Length instead of a, b, c.

v External documentation: An executable statement in a program It may be


message to the user to respond to the program requirement. For example,
printf(“Enter a number”);

By Er. Rupesh Kumar Nidhi 19

You might also like