Problem Solving
Problem Solving
Introduction
Learning to write computer program is very much like learning any skill. First, we should understand
the problems well and then try to solve it in a logical manner. For example: We have read many books
available in the market for describing the car driving methods. However, we can learn driving once
we actually get into the car and start driving it. The same logic is applied in computer programming
also. Computer programming is the process of writing, testing, troubleshooting, debugging and
maintaining of a computer program.
An effective program is that which gives result of all different inputs, including wrong input also.
While creating program, we need to follow certain systematic approach. This systematic approach
comprises two steps/things, viz., program structure and program presentation. The program structure
is implemented by using top-down or bottom-up approach and is known as “popular approach‟, while
the program representation plays an important role in making the program more readable and
understandable.
What is a Good Program?
A Good Program means that it should produce correct and faster results, taking into account all the
memory constraints. While making good program, we need to follow certain guidelines of
programming language for creating a successful program.
Problem solving methodology
As we all know, there are many methods/approaches available to solve a particular problem.
However, the efficient way is to adopt a systematic method of problem solving. The use of systematic
method of problem solving is crucial when we use a computer to solve a problem. We introduce here
a seven steps problem solving method, which is closely related to the software life cycle (the various
stages in the life of a program), that can be adapted by each person to solve the problem in their own
style. They are given as under:
1. Problem Definition
2. Problem Analysis
3. Design the problem
4. Coding
5. Program Testing and Debugging
6. Documentation
7. Program Maintenance
Example
Let us take one simple day-to-day example by writing algorithm for making “Maggi Noodles‟ as a
food.
Step 1: Start
Step 2: Take pan with water
Step 3: Put pan on the burner
Step 4: Switch on the gas/burner
Step 5: Put maggi and masala
Step 6: Give two minutes to boil
Step 7: Take off the pan
Step 8: Take out the maggi with the help of fork/spoon
Step 9: Put the maggi on the plate and serve it
Step 10: Stop.
Further, the way of execution of the program shall be categorized into three ways: (i) sequence
statements; (ii) selection statements; and (iii) iteration or looping statements. This is also called as
“control structure‟.
Sequence statements: In this program, all the instructions are executed one after another. Just as in
the above case.
Example
Write an algorithm to find area of a rectangle.
Step 1: Start
Step 2: Take length and breadth and store them as L and B?
Step 3: Multiply by L and B and store it in area
Step 4: Print area
Step 5: Stop
Selective Statements: In this program, some portion of the program is executed based upon the
conditional test. If the conditional test is true, compiler will execute some part of the program,
otherwise it will execute the other part of the program.
Example
Write an algorithm to check whether given number is +ve, -ve or zero.
Step 1: Start
Step 2: Take any number and store it in n.
Step 3: Check n value, if n > 0 then go to step 5 else go to step 4
Step 4: Check n value, if n < 0 then go to step 6 else go to step 7
Step 5: Print “Given number is +ve” and go to step 8
Step 6: Print “Given number is -ve” and go to step 8
Step 7: Print “Given number is zero”
Step 8: Stop
Iterative statements: In some programs, certain set of statements are executed again and again based
upon conditional test. i.e. executed more than one time. This type of execution is called “looping or
iteration‟.
Example
Write an algorithm to print all natural numbers up to “n‟.
Step 1: Start
Step 2: Take any number and store it in n. =5
Step 3: Store 1 in I I=1
Step 4: Check I value, if I<=n then go to step 5 else go to step 8
Step 5: Print I (1 2 3 4 5)
Step 6: Increment I value by 1 I=1+1=2 =3=4=5=6
Step 5: Go to step 4
Step 8: Stop
Flowchart
In the previous section, we have learnt to write algorithms, i.e. step-by step process of solving a
problem. We can also show these steps in graphical form by using some symbols. This is called
flowcharting.
Flowchart Symbols
Some of the standard symbols along with respective function(s) that are used for making flowchart are
as follows:
Example
Draw a flowchart to find bigger number among two numbers (selective)
Solution: