2
2
2
Pranam K Abhyuday
Associate Consultant, Talent Transformation
Wipro Technologies
_____________________________________________________________________________________
Introduction
This tech capsule “Problem Solving by Computer” will focus on how to solve computation problems.
In order to solve computational problems we will focus a lot of how to build logic. As we go ahead and
understand logic building (basics) we will look at effective logic building (to come up with logic which is
small, consumes less time, etc.) and how to write good logic quickly.
What is LOGIC?
Logic is a collection of well-defined activities to be performed (in our case a set of instructions to be
executed by the computer) in order to solve the problem.
These steps of activities which when performed would give us the solution to our problem can be called
as LOGIC.
Logic must be represented in a form which can be understood by humans. The different ways to do so
are by writing a flowchart or a pseudo code (discussed later).
Once represented as an algorithm the same logic must be translated in a form which can be understood
by the computer. We call this form as a Program. Programs are written using any of the programming
languages like Java or .net or C or C++ (there are many more and you can choose the one you are
comfortable with).
What is Logic Building or how do we build logic to solve problems?
Logic building can be looked at a series of steps starting from understanding the problem statement,
followed by thinking of ways/ideas to solve them and eventually represent them in the form of a
solution which can be understood.
Logic building for programming problems improves with practice and by trying to solve the same
problem in different ways.
1) Algorithm Building:
Once we have understood a given problem and have a fair idea of how to solve it, we represent the
solution in the form of steps or activity which will comprise of our logic. Each step does something
constructive and all the steps put together represent the solution.
These steps or activities are called as an Algorithm. Algorithms are written in English language or
diagrammatically represented which is unambiguous and easily understandable by humans.
Example: Write an algorithm (using pseudo code representation) to determine if a student has
passed the English Language exam or not. The students mark must be entered by the user. The
pass mark for the exam is greater than 35.
Solution:
STEP1: Read student MARK from user
STEP2: if MARK is greater than 35 then
Print “PASS”
else
Print “FAIL”
These 2 steps are our logic to check if a student has passed the exam or not and they are
represented using a pseudo code.
b) Flowcharts:
Flowcharts are diagrammatic representation of an algorithm. There is a set of pre-
defined symbols which must be used to draw a flowchart.
Name Symbol Use in Flowchart
Add, Subtract
Let us rewrite the logic to check if a student has passed the English Language exam or not, now
using a flowchart
START
Read MARK
NO
IS MARK greater than
PRINT FAIL
35?
YES
PRINT PASS
STOP
2) Program Building:
The second step to a programming task is to write the PROGRAM itself. Program is a set of well-
defined instructions written using a programming language (may be C or C++ or Java or any other)
which the computer can understand.
Once we have the logic represented in the form of an algorithm (either using pseudo code or
flowchart) we rewrite the same using a programming language. Here it is very important for the
programmer to have in depth knowledge of the programming language.
In our subsequent discussions we will look at each Computational Problem in detail and understand
them by solving a lot of problems by ourselves.
The template for the Algorithm Dry Run Table is shown below. This table must be used as a reference to
create the table required for any algorithm.
The above table must be rewritten for different iterations to check if the algorithm works correctly for
different input values.
USAGE:
1) For each step, the variable content is updated based on computations performed
2) Test condition is updated for an if(condition) if true as PASS otherwise as FAIL. It is also
updated while evaluating condition within an iterative loop.
3) Output is updated with message to be displayed at different stages of the algorithm or at the
end of the algorithm.
4) In case of iterative loops and recursive algorithms, the same STEP must be rewritten in the
“Algorithm Step” column and computed again for the number of times required.
Example: Write an algorithm (using pseudo code representation) to determine if a student has passed
the English Language exam or not. The students mark must be entered by the user. The pass mark for
the exam is greater than 35.
Now we construct the Algorithm Dry Run Table to check the correctness of the above algorithm. For 2
different values of MARK entered by the user we create separate tables and check.
Iteration 1:
If value of MARK is 40
Iteration 2:
If value of MARK is 15