0% found this document useful (0 votes)
19 views

6. Introduction to Problem Solving

Chapter 6 introduces problem solving in programming, emphasizing the importance of understanding problems and developing algorithms, flowcharts, and pseudocode to find solutions. It outlines the steps for problem solving, including requirements specifications, problem analysis, algorithm design, implementation, testing, and documentation. The chapter also discusses the advantages and disadvantages of algorithms and flowcharts, and provides examples of algorithms and pseudocode for various problems.

Uploaded by

somakushwahsoma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

6. Introduction to Problem Solving

Chapter 6 introduces problem solving in programming, emphasizing the importance of understanding problems and developing algorithms, flowcharts, and pseudocode to find solutions. It outlines the steps for problem solving, including requirements specifications, problem analysis, algorithm design, implementation, testing, and documentation. The chapter also discusses the advantages and disadvantages of algorithms and flowcharts, and provides examples of algorithms and pseudocode for various problems.

Uploaded by

somakushwahsoma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.

com

Chapter-6 Introduction to Problem solving

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. Computer programming is the process
of writing, testing, troubleshooting, debugging and maintaining of a
computer program.

You will learn the following topics in this tutorial


S. No. Topics
1 Introduction to Problem Solving

2 Problem Analysis

3 Algorithms

4 Flowchart: Visual Representation of Algorithms

5 Pseudocode

6 Example 1: Write an algorithm to find area of a rectangle

7 Example 2: Write an algorithm and draw the flowchart for finding the average
of two numbers
8 Example 3: To check greatest of two numbers

9 Example 4: Write an algorithm (pseudocode) and draw a flowchart to


calculate 24 .
10 Example 5: Let us write an algorithm (pseudocode) to check whether a
number is odd or even.
11 Example 6: Write pseudocode and draw flowchart to accept numbers till the
user enters 0 and then find their average.
12 Coding

13 Testing and Debugging

14 Documentation

Page 1 of 16
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter-6 Introduction to Problem solving

Introduction to Problem Solving

 Problem solving is a process of transforming the description of a


problem into the solution of that problem by using our knowledge of
the problem domain and by relying on our ability to select
and use appropriate problem-solving Strategies, Techniques and
Tools.
 Problem solving (within the context of developing programs) refers
to analyzing a problem with the intention of deriving a solution for
the problem.
Steps for Problem Solving:

 Requirements specifications
 Problem analysis.
 Design the algorithm to solve
the problem.
 Implementation.
 Testing and verification.
 Documentation.

Page 2 of 16
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter-6 Introduction to Problem solving

Requirements Specifications:
- State the problem clearly and unambiguously (Doubtless) to understand
exactly:
 What the problem is?
 What is needed to solve it?
 What the solution should provide
 If there are constraints and special conditions.

Problem Analysis:
It is important to clearly understand a problem before we begin to find
the solution for it. In the analysis phase, we should identity the following:
 Inputs: To the problem, their form and the input media to be
used.
 Outputs: Expected from the problem, their form and the output
media to be used.
 Special constraints or (necessity) conditions (if any)
 Formulas or equations to be used. Example:
Problem: Compute and display the total cost of apples given the number
of kilograms (kg) of apples purchased and the cost per kg of apples.

 Input: Quantity of Apples purchased (in kg).


 Cost per kg of Apples (in Rs. per kg).
 Output: Total cost of Apples (in Rs.).
 Constraint: N/A
 Formula: Total cost = Cost per kg x Quantity.
Page 3 of 16
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter-6 Introduction to Problem solving

Algorithms:
 An algorithm is a precise specification of a sequence of instructions
to be carried out in order to solve a given problem.
 An Algorithm is a technique to solve a given problem systematically.
 An Algorithm is a step-by-step approach to solve a given problem in
finite number of steps.
 It takes a set of input values and processes it and gives the output.
 Each instruction tells what task is to be performed.

Properties (characteristics) of an Algorithm:

 It should be simple.
 It may accept zero or more inputs.
 It should be precise with no ambiguity.
 It should have finite number of steps.
 It should produce at least one output.

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”.
Page 4 of 16
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter-6 Introduction to Problem solving

Sequence statements: Steps that execute


in sequence are represented by symbols
that follow each other top to bottom or
left to right.
Top to bottom is the standard.

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.

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”.
Page 5 of 16
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter-6 Introduction to Problem solving

Need for an Algorithm:


1. Efficient Communication: Since an algorithm is written in English like
language, it seems to be the better way of communicating the logic.
i.e.., a procedure to solve a given problem to the users.
2. Efficient Analysis: With the help of an algorithm problem can be
analysed effectively. There can be number of algorithms for solving a
given problem. Based on the analysis, a suitable and efficient algorithm
can be selected.
3. Proper Documentation: The selected algorithm has to be documented,
which will be useful to identify the logical errors.
4. Easy and efficient coding: the algorithm act as blue print during
program development. It will be easier to write efficient program using
high level language.
5. Program Maintenance: Maintaining the software or set of programs
becomes easy.

Disadvantages of an Algorithm:

(i) Developing algorithms for large and complex problems is time


consuming and difficult to understand.
(ii) Understanding complex logic is tedious.

Page 6 of 16
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter-6 Introduction to Problem solving

There are two common methods of representing an algorithm:


 Flowchart
 Pseudocode

Flowchart: Visual Representation of Algorithms


 A flowchart depicts pictorially the sequence in which instructions are
carried out in an algorithm.
 A flowchart is a schematic representation of an algorithm or a
stepwise process, showing the steps of boxes of various kinds and
their order by connecting these with arrows.
 Flowcharts are used not only as aids in developing algorithms but also
to document algorithms.
 Flowcharts are used in designing or documenting a process or
program.

Flowcharts Symbols:
Symbol Name Function

Start and Indicates the starting or ending of


Stop the program, process, or interrupt
program
OR Terminal
Indicates any type of internal
Process
operation inside the Processor or
Memory
Page 7 of 16
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter-6 Introduction to Problem solving

Used for any Input / Output (I/O)


operation. Indicates that the
input/output
computer is to obtain data or
output results

Decision Used to ask a question that can be


answered in a binary format
(Yes/No, True/False)
Connector Allows the flowchart to be drawn
without intersecting lines or
without a reverse flow.
Predefined Used to invoke a subroutine or an
Process interrupt program.
Shows direction of flow. It
Flow Lines indicates the flow of data from
one geometrical signal to another.

Need for the flowchart:


1. Efficient Communication: since its represented in pictorial form a
flowchart seems to be the better way of communicating the logic
to the programmers or any common man.
2. Efficient Analysis: With the help of a flowchart problem can be
analyzed effectively.
3. Proper Documentation: flowchart plays an important role in
understanding the logic of complicated and lengthy problems.
Hence it is very much required to document the flowcharts along
with algorithms.

Page 8 of 16
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter-6 Introduction to Problem solving

4. Easy and efficient coding: by looking at the flowchart writing


programs is a straight forward approach.
5. Program Debugging: flowchart helps in debugging process i.e., to
identify the logical errors in the program.
6. Program Maintenance: Maintaining the software or set of
programs becomes easy.

Drawbacks of flowchart:
(i) Complex logic: For complicated logic the flowchart becomes
complex and clumsy.
(ii) Alterations and modifications: if there is a change in the
logic, the flowchart has to be completely rewritten and
requires lot of time.
(iii) Reproduction: in the case of any problem, reproduction of
flowchart becomes problem since the flowchart symbols
cannot be typed.

Pseudocode
 Pseudocode is a simplified, half-English, half-code outline of a
computer program.
 This code is written for human understanding rather than computer
or machine understanding.
 The aim is to get the idea quickly and also easy to read without
details.
 It is like a young child putting sentences together without any
grammar.

Page 9 of 16
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter-6 Introduction to Problem solving

Criteria of a good pseudo code:

 Easy to user stand, precise and clear


 Gives the correct solution in all cases
 Eventually ends

Following are some of the frequently used keywords while writing


pseudocode:

 INPUT/READ  DECREMENT
 COMPUTE  IF/ELSE
 PRINT  WHILE
 INCREMENT  TRUE/FALSE

Example 1: Write an algorithm to find area of a rectangle


Algorithm Flowchart Pesudocode
Step 1: Start BEGIN
Step 2: get l,b values READ l,b
Step 3: Calculate A=l*b CALCULATE A=l*b
Step 4: Display A PRINT A
Step 5: Stop END

Page 10 of 16
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter-6 Introduction to Problem solving

Example 2: Write an algorithm and draw the flowchart for finding the
average of two numbers
Algorithm flowchart Pesudocode

1. Start START BEGIN


2. get x , y values READ x,y
3. calculate
Input x CALCULATE
sum = x + y
average = sum /2 sum=x+y
4. output average Input y average=sum/2
PRINT sum
Sum = x + y
END

Average = sum/2

Output
Average

END

Page 11 of 16
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter-6 Introduction to Problem solving

Example 3: To check greatest of two numbers

Algorithm Flowchart Pesudocode


Step 1: Start BEGIN
Step 2: get a,b value READ a,b
Step 3: check if(a>b) IF (a>b) THEN
print a is greater
DISPLAY a is greater
Step 4: else b is greater
ELSE
Step 5: Stop
DISPLAY b is greater
END IF
END

Example 4: Write an algorithm (pseudocode) and draw a flowchart to


calculate 24 .
Algorithm flowchart Pesudocode
Step: Start BEGIN

Step 1: get Base (2), Power (4) READ Base (2), Power (4)

Step 2: initialize Product= Base INITIALIZE Product= Base

Step 3: Product = Product * Base CALCULATION

Step 4: Product = Product * Base Product = Product * Base

Step 5: Product = Product * Base Product = Product * Base

Step 6: Display finale Product Product = Product * Base

Step 7: Stop PRINT Product

END

Page 12 of 16
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter-6 Introduction to Problem solving

Example 5: Let us write an algorithm (pseudocode) 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"

The flowchart representation of the algorithm in shown

Page 13 of 16
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter-6 Introduction to Problem solving

Example 6: 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
The flowchart representation is shown

In this example, we do not know how


many numbers a user is going to enter
before entering 0. This is handled by
checking the condition repeatedly till the
condition becomes false.

Page 14 of 16
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter-6 Introduction to Problem solving

Coding:
 In this stage the algorithm is translated into the program called
source code using some programming language.
 Different high level programming languages can be used for
writing a program.
 The output of the program must be the solution of the intended
problem.
Testing and Debugging
 Testing is the process of finding errors in a program and debugging
is of correcting the errors.
 It must respond within the expected time.
 It should generate correct output for all possible inputs.
 In case the output generated is incorrect, then the program should
be checked for logical errors, if any.
 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.

Page 15 of 16
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter-6 Introduction to Problem solving

Documentation
 Documentation referrers to written descriptions specification, design
code and comments, internal and external to program which makes
more readable and understandable
Uses of documentation:

 This becomes a useful interface between a technical personnel and


non technical personnel.
 This is very useful for upkeep and maintenance.
 Documentation makes ease for any technical emergencies.
 Very useful in operating for learners and trainers.
 Enables trouble shooting when the application system breaks down.

Page 16 of 16

You might also like