0% found this document useful (0 votes)
20 views12 pages

Chapter_4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views12 pages

Chapter_4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Chapter-4

Problem solving
1. What is problem solving?
Problem solving is the process of identifying a problem, developing
an algorithm and finally implementing the algorithm to develop a
computer program.

2. What are the steps for problem solving?


Step 1: Analysing the problem-
• Analysing the problems means understand a problem clearly
before we begin to find the solution for it.
• Analysing a problem helps to figure out what are the inputs that
our program should accept and the outputs that it should produce.
Step 2: Developing an Algorithm:
• It is essential to find a solution before writing a program code for a
given problem. The solution is represented in natural language and
is called an algorithm.
• Algorithm: A set of exact steps which when followed to solve
the problem.
Step 3: Coding:
• Coding is the process of converting the algorithm into the
program which can be understood by the computer to generate
the desired solution.
• You can use any high level programming languages for writing a
program.
Step 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 Testing methods are
• unit or component testing,
• integration testing,
• system testing, and
• acceptance testing
Debugging: T he 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.

3. Algorithm
Algorithm is a set of sequence which followed to solve a problem.
Algorithm 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.

4. Algorithm for Computing GCD of two numbers .


GCD is the largest number that divides both the given numbers.
Ex: GCD of two numbers-45 & 54.
Step 1: Find the numbers (divisors) which can divide the given numbers.
Divisors of 45 are: 1, 3, 5, 9, 15, 45
Divisors of 54 are: 1, 2, 3, 6, 9, 18, 27, 54
Step 2: Then find the largest common number from these two lists.
Therefore, GCD of 45 and 54 is 9

5. Why do we need algorithm/ Importance of algorithm:


• The purpose of using an algorithm is to increase the reliability,
accuracy and efficiency of obtaining solutions.
• Algorithm is like a roadmap, the programmer may not be able to
clearly visualise the instructions to be written and may end up
developing a program which may not work as expected.
6. Characteristics of a good algorithm
• 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.
While writing an algorithm, it is required to clearly identify the
following:
• The input to be taken from the user.
• Processing or computation to be performed to get the desired result.
• The output desired by the user.
7. Representation of Algorithms
There are two common methods of representing an algorithm —
• Flow chart- A flowchart is a visual representation of an algorithm
• Pseudocode- It is a detailed description of instructions that a
computer must follow in a particular order.

8. 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. There are standardized
symbols to draw flowcharts.

Question: Write an algorithm to find the square of a number.


Algorithm to find square of a number.
Step 1: Input a number and store it to num
Step 2: Compute num * num and store it in square
Step 3: Print square
The algorithm to find square of a number can be represented
pictorially using flowchart

9. Pseudocode
• A pseudocode 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”.

Keywords are used in pseudocode:


INPUT
COMPUTE
PRINT
INCREMENT
DECREMENT
IF/ELSE
WHILE
TRUE/FALSE
Question : Write an algorithm to calculate area and perimeter of a
rectangle, using both pseudocode and flowchart.
Pseudocode for calculating area and perimeter of a rectangle.
INPUT length
INPUT breadth
COMPUTE Area = length * breadth
PRINT Area
COMPUTE Perim = 2 * (length + breadth)
PRINT Perimeter
The flowchart for this algorithm
10. Benefits of Pseudocode
• A pseudocode 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.
• For non-programmers, actual programs are difficult to read and
understand, but pseudocode helps them to review the steps to
confirm that the proposed implementation is going to achieve the
desire output.
11. Flow of Control:
The flow of control depicts the flow of process as represented in the flow
chart. The process can flow in
• Sequence
• Selection
• Repetition
Sequence
In a sequence steps of algorithms (i.e. statements) are executed one after
the other.
Selection
In a selection, steps of algorithm is depend upon the conditions i.e. any one
of the alternatives statement is selected based on the outcome of a
condition.
Conditionals are used to check possibilities. The program checks one or
more conditions and perform operations (sequence of actions) depending
on true or false value of the condition.
Conditionals are written in the algorithm as follows:
If is true then
steps to be taken when the condition is true/fulfilled
otherwise
steps to be taken when the condition is false/not fulfilled.

Repetition
Repetitions are used, when we want to do something repeatedly, for a
given number of times.

12. Verifying Algorithm:


• After writing an algorithm it is necessary to verify that it is working
as expected.
• To verify, we have to take different input values and go through all
the steps of the algorithm to yield the desired output for each input
value.
• 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
3. 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.

13. Why is verification of algorithm an important step in problem


solving?
To increase the reliability, accuracy and efficiency of obtaining solutions

14. Comparing algorithm:


• Algorithms are compared by checking their time and space
complexity.
• Time complexity is the amount of processing time an algorithm
requires to run.
• Space complexity is the amount of memory that is needed to
exeute an algorithm.

Question : 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”

Pseudocode 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”
Question : Write pseudocode and draw flowchart to accept numbers
till the user enters 0 and then find their average.

Pseudocode is as follows:
Step 1: Set count = 0, sum = 0
Step 2: Input num
Step 3: While num is not equal to 0, repeat Steps 4 to 6
Step 4: sum = sum + num
Step 5: count = count + 1
Step 6: Input num
Step 7: Compute average = sum/count
Step 8: Print average
15. 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.
The syntax is the set of rules or grammar that governs the formulation of
the statements in the language, such as spelling, order of words,
punctuation, etc.
Source Code: 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.
16. Decomposition
Decomposition is a process to ‘decompose’ or break down a complex
problem into smaller subproblems. It is helpful when we have to solve any
big or complex problem.
• Breaking down a complex problem into sub problems also means
that each subproblem can be examined in detail.
• Each subproblem can be solved independently and by different
persons (or teams).
• Having different teams working on different sub-problems can also
be advantageous because specific sub-problems can be assigned to
teams who are experts in solving such problems.
Once the individual sub-problems are solved, it is necessary to test them
for their correctness and integrate them to get the complete solution.

Example:
******************************************************

You might also like