Problem Solving

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Chapter-4

Introduction to Problem Solving


Q-1 What do you mean by ‘Problem Solving’.
Ans. Problem solving is the act of defining a problem,
determining the cause of the problem, identifying and selecting
alternatives for a solution; and implementing a solution.
Every day we face many problems. When problems are
straightforward and easy, we can easily find the solution. But a
complex problem requires a methodical approach to find the
right solution.
Q-2 Write Steps for Problem Solving.
Ans. Complex problem requires a methodical approach to find
the right solution. In other words, we have to apply problem
solving techniques. Problem solving begins with the precise
identification of the problem and ends with a complete working
solution in terms of a program or software.
Key steps required for solving a problem using a computer are
shown in Diagram and are discussed as follows:
1 Analyzing the problem - It is important to clearly understand a
problem before we begin to find the solution for it. If we are not
clear as to what is to be solved, we may end up developing a
program which may not solve our purpose. Thus, we need to read
and analyze the problem statement carefully in order to list the
principal components of the problem and decide the core
functionalities that our solution should have. By analyzing a
problem, we would be able to figure out what are the inputs that
our program should accept and the outputs that it should produce.
2 Developing an Algorithm -It is essential to device a solution
before writing a program code for a given problem. The solution is
represented in natural language and is called an algorithm. We
start with a tentative solution plan and keep on refining the
algorithm until the algorithm is able to capture all the aspects of
the desired solution. For a given problem, more than one algorithm
is possible and we have to select the most suitable solution.
3 Coding - After finalizing the algorithm, we need to convert the
algorithm into the format which can be understood by the
computer to generate the desired solution. Different high level
programming languages can be used for writing a program. It is
equally important to record the details of the coding
procedures followed and document the solution.
4 Testing and Debugging -The program created should be
tested on various parameters. The program should meet the
requirements of the user. It must respond within the expected
time. It should generate correct output for all possible inputs.
In the presence of syntactical errors, no output will be
obtained. In case the output generated is incorrect, then the
program should be checked for logical errors, if any.
Software industry follows standardized testing methods
like unit or component testing, integration testing, system
testing, and acceptance testing while developing complex
applications. This is to ensure that the software meets all the
business and technical requirements and works as expected.
The errors or defects found in the testing phases are debugged
or rectified and the program is again tested. This continues till
all the errors are removed from the program. Once the
software application has been developed, tested and delivered
to the user, still problems in terms of functioning can come up
and need to be resolved from time to time.
Q-3 What is an Algorithm. Describe its need.
Ans. To complete each activity, we follow a sequence of steps.
Before coding, we need to write these steps in natural language
called Algorithm. For a given problem, more than one algorithm
is possible and we have to select the most suitable solution.
A finite sequence of steps required to get the desired
output is called an algorithm. It will lead to the desired result in
a finite amount of time, if followed correctly. Algorithm has a
definite beginning and a definite end, and consists of a finite
number of steps.
Need of 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.
Writing an algorithm is mostly considered as a first step to
programming. Once we have an algorithm to solve a problem,
we can write the computer program for giving instructions to
the computer in high level language. If the algorithm is correct,
computer will run the program correctly, every time. So, the
purpose of using an algorithm is to increase the reliability,
accuracy and efficiency of obtaining solutions.
Q-4 What are characteristics of a good Algorithm?
Ans. • Precision — the steps are precisely stated or defined.
• Uniqueness — results of each step are uniquely defined and
only depend on the input and the result of the preceding steps.
• Finiteness — the algorithm always stops after a finite number
of steps.
• Input — the algorithm receives some input.
• Output — the algorithm produces some output.
Q-5 How can an Algorithm be represented?
Ans. Using their algorithmic thinking skills, the software
designers or programmers analyze the problem and identify the
logical steps that need to be followed to reach a solution. Once
the steps are identified, the need is to write down these steps
along with the required input and desired output. There are
two common methods of representing an algorithm —
Flowchart and pseudo code.
Flowchart —Visual representation of Algorithms. A flowchart is
a visual representation of an algorithm. A flowchart is a
diagram made up of boxes, diamonds and other shapes,
connected by arrows. Each shape represents a step of the
solution process and the arrow represents the order or link
among the steps.
Example - The algorithm to find square of a number can be
represented as follows:
Pseudo Code - A pseudo code 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.
The word “pseudo” means “not real,” so “pseudo code” means
“not real code”. Following are some of the frequently used
keywords while writing pseudo code:
• INPUT
• PRINT
• INCREMENT
• DECREMENT
• IF/ELSE
• WHILE
• TRUE/FALSE
Example - Write an algorithm to display the sum of two
numbers entered by user, using both pseudo code and
flowchart. Pseudo code for the sum of two numbers will be:
input num1
input num2
COMPUTE Result = num1 + num2
PRINT Result
Q-6 What are the benefits of Pseudo code?
Ans. Before writing codes in a high level language, a Pseudo
code of a program helps in representing the basic functionality
of the intended program. By writing the code first in a human
readable language, the programmer safeguards against leaving
out any important step. Besides, for non-programmers, actual
programs are difficult to read and understand, but pseudo code
helps them to review the steps to confirm that the proposed
implementation is going to achieve the desire output.
Q-7 What is Flow of Control?
Ans. The flow of control depicts the flow of events as
represented in the flow chart. The events can flow in a
sequence, or on branch based on a decision or even repeat
some part for a finite number of times.
1. Sequence - The statements are executed one after
another, in a sequence. Such algorithms where all the
steps are executed one after the other are said to execute
in sequence.
2. Selection –Depending upon the situation, we need to
select various steps to solve a problem, called Selection.
In the program we need to check one or more conditions
and perform operations depending on true or false value
of the condition. These true or false values are called
binary values.
Conditionals are written in the algorithm as follows:
If <condition> then
steps to be taken when the condition is true
There are situations where we also need to take action
when the condition is not fulfilled . So we can write:
If <condition> is true then
steps to be taken when the condition is true
otherwise
steps to be taken when the condition is false

Example- write an algorithm to check whether a number is odd


or even.
• Input: Any number
• Process: Check whether the number is even or not
• Output: Message “Even” or “Odd”
Pseudo code of the algorithm can be written as follows:
PRINT "Enter the Number"

INPUT number

IF number MOD 2 == 0 THEN

PRINT "Number is Even"

ELSE

PRINT "Number is Odd"


The flowchart representation of the algorithm as follows:

3. Repetitions - These are the kind of statements we use,


when we want something to be done repeatedly.
Repetition is also known as iteration or loop. A loop in an
algorithm means execution of some program statements
repeatedly till some specified Condition is satisfied.
Q-8 What is the need to verify any Algorithm?
Ans. When we are told any formula, we check this for small
numbers, for which we can manually calculate or check. For
example sum of N numbers is N(N+1)/2.
In the same way, when we have an algorithm, we can
verify that is it working as expected or not. To verify, we have
to take different input values and go through all the steps of
the algorithm to get the desired output for each input value
and may modify or improve as per the need.
The method of taking an input and running through the
steps of the algorithm is sometimes called Dry run. Such a dry
run will help us to:
1. Identify any incorrect steps in the algorithm
2. Figure out missing details or specifics in the algorithm
It is important to decide the type of input value to be used
for the simulation. In case all possible input values are not
tested, then the program will fail.

Q-9 How can Algorithm be compared?


Ans. There can be more than one approach to solve a problem
using computer and hence we can have more than one
algorithm. Then question arises that which algorithm should be
used?
Algorithms can be compared and analyzed on the basis of
the amount of processing time they need to run and the
amount of memory that is needed to execute the algorithm.
These are termed as time complexity and space complexity,
respectively. The choice of an algorithm over another is done
depending on how efficient they are in terms of processing
time required (time complexity) and the memory they utilize.

Q-10 Explain ‘Coding’.


Ans. Once an algorithm is finalized, it should be coded in a high-
level programming language. The ordered set of instructions is
written in a 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 or no modifications. Low-level programs can run on only
one kind of computer and have to be rewritten in order to run
on another type of system. A wide variety of high-level
languages, such as FORTRAN, C, C++, Java, Python, etc.
A program written in a high-level language is called Source
code. We need to translate the source code into machine
language using a compiler or an interpreter, so that it can be
understood by the computer.

Q-11 What is Decomposition?


Ans. Sometimes a problem may be complex, so its solution is
not directly derivable. In such cases, we need to decompose it
into simpler parts.
The basic idea of solving a complex problem by
decomposition is to 'decompose' or break down a complex
problem into smaller sub problems.
These sub problems are relatively easier to solve than the
original problem. Finally, the sub problems are combined in a
logical way to obtain the solution for the bigger, main problem.
Breaking down a complex problem into sub problems also
means that each sub problem can be examined in detail. Each
sub problem can be solved independently and by different
persons (or teams). Once the individual sub problems are
solved, it is necessary to test them for their correctness and
integrate them to get the complete solution.
Q-12 Write a short notes on following topics
(A) Classification of Programming Languages
(B) Language Translators
Ans. Classification of Programming Languages - It is very hard
to write instructions in the form of 1s and 0s. So different types
of computer programming languages are developed to simplify
the coding. Two major categories of computer programming
languages are low-level languages and high-level languages.
Low-level languages are machine dependent languages
and include machine language and assembly language.
Machine language uses 1s and 0s to write instructions
which are directly understood and executed by the computer.
But writing a code in machine language is difficult as one has to
remember all operation codes and machine addresses. Also
finding errors in the code written in machine language is
difficult.
To simplify the writing of code, assembly language was
developed that allowed usage of English-like words and
symbols instead of 1s and 0s. But one major drawback of
writing a code in this language is that the code is computer
specific, i.e., the code written for one type of CPU cannot be
used for another type of CPU. High level languages are machine
independent and are simpler to write code into.
Instructions are using English like sentences and each high
level language follows a set of rules, similar to natural
languages. However, these languages are not directly
understood by the computer. Hence, translators are needed to
translate high-level language codes into machine language.
Examples of high level language include C++, Java, Python, etc.

Language Translators - As the computer can understand only


machine language, a translator is needed to convert program
written in assembly or high level language to machine
language. The program code written in assembly or high-level
language is called source code. The source code is converted by
a translator into the machine understandable form called
object (machine) code.
As there are different types of computer languages, different
translators are needed to convert the source code to machine
code. The three types of translators used in computing systems
are assembler, compiler and interpreter. The translator used to
convert the code written in assembly language to machine
language is called assembler. Each assembler can understand a
specific microprocessor instruction set only and hence, the
machine code is not portable.
We also need translators to convert codes written in high
level language (source code) to machine understandable form
(machine code) for execution by the computer. Compiler
converts the source code into machine code. If the code follows
all syntactic rules of the language, then it is executed by the
computer. Once translated, the compiler is not needed.
An interpreter translates one line at a time instead of the
whole program at one go. Interpreter takes one line, converts it
into executable code if the line is syntactically correct, and then
it repeats these steps for all lines in the source code. Hence,
interpreter is always needed whenever a source code is to be
executed.

You might also like