Intr To Problem Solving
Intr To Problem Solving
Introduction to
Problem Solving
4.1 INTRODUCTION “Computer Science is a
Today, computers are all around us. We use them science of abstraction
for doing various tasks in a faster and more accurate -creating the right model for
manner. For example, using a computer or smartphone, a problem and devising the
we can book train tickets online.
appropriate mechanizable
India is a big country and we have an enormous
techniques to solve it.”
railway network. Thus, railway reservation is a complex
task. Making reservation involves information about –A. Aho and J. Ullman
many aspects, such as details of trains (train type, types
of berth and compartments in each train, their schedule,
etc.), simultaneous booking of tickets by multiple users
and many other related factors.
It is only due to the use of computers that today,
the booking of the train tickets has become easy. Online
booking of train tickets has added to our comfort by
enabling us to book tickets from anywhere, anytime.
We usually use the term computerisation to indicate
the use of computer to develop software in order to
automate any routine human task efficiently. Computers In this chapter
are used for solving various day-to-day problems » Introduction
and thus problem solving is an essential skill that a » Steps for Problem
computer science student should know. It is pertinent Solving
to mention that computers themselves cannot solve a » Algorithm
problem. Precise step-by-step instructions should be » Representation of
given by us to solve the problem. Thus, the success of a Algorithms
computer in solving a problem depends on how correctly » Flow of Control
and precisely we define the problem, design a solution
» Verifying Algorithms
(algorithm) and implement the solution (program) using
» Comparison of
a programming language. Thus, problem solving is the Algorithm
process of identifying a problem, developing an algorithm
» Coding
for the identified problem and finally implementing the
» Decomposition
algorithm to develop a computer program.
2022-23
2022-23
2022-23
Activity 4.1 answering the queries of the user and even serving the
request for addition or modification of features.
What sequence of
steps will you follow to
compute the LCM of 4.3 ALGORITHM
two numbers?
In our day-to-day life we perform activities by following
certain sequence of steps. Examples of activities include
getting ready for school, making breakfast, riding a
bicycle, wearing a tie, solving a puzzle and so on. To
complete each activity, we follow a sequence of steps.
Suppose following are the steps required for an activity
‘riding a bicycle’:
1) remove the bicycle from the stand,
2) sit on the seat of the bicycle,
3) start peddling,
4) use breaks whenever needed and
5) stop on reaching the destination.
Let us now find Greatest Common Divisor (GCD) of two
numbers 45 and 54.
Note: GCD is the largest number that divides both the
given numbers.
Step 1: Find the numbers (divisors) which can divide the
given numbers
Divisors of 45 are: 1, 3, 5, 9, 15, and 45
Divisors of 54 are: 1, 2, 3, 6, 9, 18, 27,
and 54
Step 2: Then find the largest common number from these
two lists.
The origin of the term Therefore, GCD of 45 and 54 is 9
Algorithm is traced to Hence, it is clear that we need to follow a sequence
Persian astronomer and of steps to accomplish the task. Such a finite sequence
mathematician, Abu of steps required to get the desired output is called
Abdullah Muhammad
ibn Musa Al-Khwarizmi an algorithm. It will lead to the desired result in a finite
(c. 850 AD) as the amount of time, if followed correctly. Algorithm has a
Latin translation of Al- definite beginning and a definite end, and consists of a
Khwarizmi was called finite number of steps.
‘Algorithmi’.
4.3.1 Why do we need an Algorithm?
A programmer writes a program to instruct the computer
to do certain tasks as desired. The computer then follows
the steps written in the program code. Therefore, the
programmer first prepares a roadmap of the program
to be written, before actually writing the code. Without
2022-23
2022-23
2022-23
Activity 4.2
Draw a flowchart
that represents the
attainment of your
career goal.
2022-23
4.4.2 Pseudocode
A pseudocode (pronounced Soo-doh-kohd) is another
way of representing an algorithm. It is considered as a
non-formal language that helps programmers to write
algorithm. It is a detailed description of instructions
that a computer must follow in a particular order. It is
intended for human reading and cannot be executed
directly by the computer. No specific standard for writing
a pseudocode exists. The word “pseudo” means “not
real,” so “pseudocode” means “not real code”. Following
are some of the frequently used keywords while writing
pseudocode:
• INPUT
• COMPUTE
• PRINT
• INCREMENT
• DECREMENT
• IF/ELSE
• WHILE
• TRUE/FALSE
Example 4.3: Write an algorithm to display the
sum of two numbers entered by
Activity 4.3 user, using both pseudocode and
Write a pseudocode for flowchart.
creating a scoreboard Pseudocode for the sum of two numbers will be:
for a hockey match. INPUT num1
INPUT num2
COMPUTE Result = num1 + num2
PRINT Result
The flowchart for this algorithms is given in Figure 4.4.
Result
Print Result
2022-23
2022-23
2022-23
2022-23
4.5.3 Repetition
When giving directions to go someplace, we say something
like, “walk 50 steps then turn right”. Or “Walk till the next
2022-23
2022-23
Activity 4.4
Let us answer the
following questions
using the pesudocode
given in example 4.9:
1) What will the sum
when the input are 6,
7, 4, 8, 2, 5, 0, 3, 1.
2) What will be the
value of count?
3) Why did we use the
input statement to Figure 4.11: Flowchart to accept numbers till the user enters 0
enter num twice?
4) Why did we divide In this example, we do not know how many numbers
sum by count? a user is going to enter before entering 0. This is handled
5) Can there be any by checking the condition repeatedly till the condition
other approach?
becomes false.
2022-23
2022-23
2022-23
4.8 CODING
Once an algorithm is finalised, it should be coded in
a high-level programming language as selected by the
programmer. The ordered set of instructions are written
in that programming language by following its syntax.
Syntax is the set of rules or grammar that governs the
formulation of the statements in the language, such as
spellings, order of words, punctuation, etc.
The machine language or low level language
consisting of 0s and 1s only is the ideal way to write a
computer program. Programs written using binary digits
are directly understood by the computer hardware,
but they are difficult to deal with and comprehend
by humans. This led to the invention of high-level
languages which are close to natural languages and are
easier to read, write, and maintain, but are not directly
understood by the computer hardware. An advantage of
using high-level languages is that they are portable, i.e.,
they can run on different types of computers with little
2022-23
4.9 DECOMPOSITION
Sometimes a problem may be complex, that is, its
solution is not directly derivable. In such cases, we need
to decompose it into simpler parts. Let us look at the
Railway reservation system we talked about earlier. The
complex task of designing a good railway reservation
system is seen as designing the different components of
the system and then making them work with each other
effectively.
The basic idea of solving a complex problem by
decomposition is to 'decompose' or break down a
complex problem into smaller sub problems as shown
2022-23
SUMMARY
• An algorithm is defined as a step-by-step procedure
designed to perform an operation which will lead to
the desired result, if followed correctly.
• Algorithms have a definite beginning and a definite
end, and a finite number of steps.
• A good algorithm, which is precise, unique and
finite, receives input and produces an output.
2022-23
EXERCISE
1. Write pseudocode that reads two numbers and
divide one by another and display the quotient.
2. Two friends decide who gets the last slice of a cake
by flipping a coin five times. The first person to
win three flips wins the cake. An input of 1 means
player 1 wins a flip, and a 2 means player 2 wins
a flip. Design an algorithm to determine who takes
the cake?
3. Write the pseudocode to print all multiples of 5
between 10 and 25 (including both 10 and 25).
4. Give an example of a loop that is to be executed a
certain number of times.
5. Suppose you are collecting money for something.
You need ` 200 in all. You ask your parents, uncles
and aunts as well as grandparents. Different people
may give either ` 10, ` 20 or even ` 50. You will collect
till the total becomes 200. Write the algorithm.
6. Write the pseudocode to print the bill depending
upon the price and quantity of an item. Also print
2022-23
Process Step
Start/Stop of the
Process
2022-23
NOTES
Data
Decision Making
2022-23
NOTES Accept_1to100_Algo
INPUT Number
IF (0<= Number) AND (Number <= 100)
ACCEPT
Else
REJECT
a) On what values will this algorithm fail?
b) Can you improve the algorithm?
2022-23