0% found this document useful (0 votes)
2 views21 pages

Ch4 Problem Solving Notes

Chapter 4 of the I PUC Computer Science curriculum introduces problem-solving in computer science, outlining the steps involved: analyzing the problem, developing algorithms, coding, and testing. It emphasizes the importance of algorithms, their characteristics, and methods of representation such as flowcharts and pseudocode. The chapter also covers flow control mechanisms, including sequence, selection, and repetition, along with techniques for verifying algorithms through dry runs.

Uploaded by

Soumik Mukherjee
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)
2 views21 pages

Ch4 Problem Solving Notes

Chapter 4 of the I PUC Computer Science curriculum introduces problem-solving in computer science, outlining the steps involved: analyzing the problem, developing algorithms, coding, and testing. It emphasizes the importance of algorithms, their characteristics, and methods of representation such as flowcharts and pseudocode. The chapter also covers flow control mechanisms, including sequence, selection, and repetition, along with techniques for verifying algorithms through dry runs.

Uploaded by

Soumik Mukherjee
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

I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

I PUC COMPUTER SCIENCE


Chapter 4

Introduction to Problem Solving


Detailed Teaching Notes with Flowcharts & Examples

Karnataka DPUE Board | The Paradise PU College, Basapura, Bengaluru

TOPICS COVERED IN THIS CHAPTER

● 4.1 Introduction to Problem Solving

● 4.2 Steps for Problem Solving

● 4.3 Algorithm – Definition, Need, Characteristics

● 4.4 Representation of Algorithms – Flowchart & Pseudocode

● 4.5 Flow of Control – Sequence, Selection, Repetition

● 4.6 Verifying Algorithms (Dry Run)

● 4.7 Comparison of Algorithms (Time & Space Complexity)

● 4.8 Coding

● 4.9 Decomposition

The Paradise PU College, Basapura, Bengaluru | Page 1


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

4.1 Introduction
Computers are powerful tools that help us solve complex problems quickly and accurately. However, a
computer cannot think on its own. It needs precise, step-by-step instructions from us to solve any
problem. The quality of the solution depends entirely on how well we define the problem, design a solution,
and implement it.
Problem Solving in Computer Science is the process of:
● Identifying and understanding a problem clearly
● Designing an algorithm (step-by-step solution)
● Implementing the algorithm as a computer program
● Testing and verifying the program for correctness

Key Quote

"Computer Science is a science of abstraction — creating the right model for a problem and
devising the appropriate mechanisable techniques to solve it."

— A. Aho and J. Ullman

Real-Life Example – Railway Reservation System

Online train ticket booking involves: train schedules, berth availability, simultaneous multi-user
booking, payment processing, and cancellations. This complex task is broken into sub-problems
and solved using computers — a perfect example of computerisation.

4.2 Steps for Problem Solving


Solving a complex problem requires a methodical approach. The four key steps are:

Carefully read and understand the problem statement. Identify: inputs (what
1. Analyse the data is given), outputs (what result is expected), and constraints (any special
Problem conditions). Without clear analysis, we may build a program that does not solve
the actual problem.

2. Develop an Design a step-by-step solution in natural language. Refine it until it covers all
Algorithm cases. More than one algorithm may exist — choose the most suitable one.

Convert the algorithm into a high-level programming language (e.g. Python).


3. Coding
Follow the syntax rules. Document the code for future reference.

Test the program with various inputs — valid, invalid, and boundary values. Fix
4. Testing and syntax errors and logical errors (debugging). Repeat until all errors are
Debugging removed. Software testing types: unit testing, integration testing, system testing,
acceptance testing.

The Paradise PU College, Basapura, Bengaluru | Page 2


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

4.3 Algorithm
Definition

An algorithm is a finite, ordered sequence of well-defined steps that, when followed correctly,
solves a given problem or accomplishes a required task. It has a definite beginning, a definite end,
and consists of a finite number of steps.

Origin of the word "Algorithm"


The term comes from the name of Persian mathematician Abu Abdullah Muhammad ibn Musa
Al-Khwarizmi (c. 850 AD). The Latin translation of his name was "Algorithmi".

(A) Characteristics of a Good Algorithm


Property Meaning

Precision Every step is clearly and exactly stated — no ambiguity.

Each step produces a unique result depending only on the input and previous
Uniqueness steps.

Finiteness The algorithm always terminates after a finite number of steps.

Input It accepts zero or more inputs from the user.

Output It produces at least one output (the result).

(B) What to identify before writing an algorithm


● The input to be taken from the user
● The process / computation to be performed
● The output expected

Day-to-Day Example – Riding a Bicycle


Even simple activities follow an algorithm. Example — steps to ride a bicycle:
Step 1: Remove the bicycle from the stand
Step 2: Sit on the seat of the bicycle
Step 3: Start peddling
Step 4: Use brakes whenever needed
Step 5: Stop on reaching the destination

Mathematical Example – GCD of 45 and 54


GCD (Greatest Common Divisor) is the largest number that divides both numbers.
Divisors of 45: 1, 3, 5, 9, 15, 45 | Divisors of 54: 1, 2, 3, 6, 9, 18, 27, 54
Common divisors: 1, 3, 9 → GCD = 9

The Paradise PU College, Basapura, Bengaluru | Page 3


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

4.4 Representation of Algorithms


Algorithms are represented using two popular methods: Flowchart (visual) and Pseudocode (text). Both
show the logic without implementation details and reveal the flow of control.

4.4.1 Flowchart – Visual Representation


A flowchart is a diagram made of standard shapes connected by arrows. Each shape represents a step;
arrows show the order of execution.

Symbol Name Shape Description

Start / End (Terminator) Rounded rectangle / OvalIndicates where the flow starts and ends

Process (Action) Rectangle Represents a computation or processing step

Decision Diamond A yes/no question; splits flow into two branches

Input / Output (Data) Parallelogram Used to input data or display output

Arrow (Flow Line) → Shows the direction / order of flow between steps

Example 4.1 – Find the Square of a Number


Input: A number (num) Process: square = num × num Output: square
Algorithm:
Step 1: Input a number → store in num
Step 2: Compute square = num × num
Step 3: Print square

The Paradise PU College, Basapura, Bengaluru | Page 4


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

Start

Input num

square = num * num

Print square

Stop

Fig 1: Flowchart – Square of a Number

Example 4.2 – Sum of Two Numbers (Pseudocode & Flowchart)


Pseudocode:
INPUT num1
INPUT num2
COMPUTE Result = num1 + num2
PRINT Result

The Paradise PU College, Basapura, Bengaluru | Page 5


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

Start

Read num1, num2

Result = num1 + num2

Print Result

Stop

Fig 2: Flowchart – Sum of Two Numbers

Example 4.3 – Area and Perimeter of a Rectangle


Pseudocode:
INPUT length
INPUT breadth
COMPUTE Area = length * breadth
PRINT Area
COMPUTE Perim = 2 * (length + breadth)
PRINT Perim

The Paradise PU College, Basapura, Bengaluru | Page 6


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

Start

Input length, breadth

Area = length * breadth

Print Area

Perim = 2*(length+breadth)

Print Perim

End

Fig 6: Flowchart – Area & Perimeter of Rectangle

4.4.2 Pseudocode
Pseudocode (pronounced Soo-doh-kohd) is a human-readable, informal description of an algorithm. It
uses English-like statements and is not directly executable by a computer. "Pseudo" means "not real" —
so pseudocode means "not real code".

Commonly used keywords in pseudocode:

INPUT COMPUTE PRINT

SET INCREMENT DECREMENT

IF / ELSE WHILE TRUE / FALSE

Benefits of Pseudocode

● Helps the programmer plan before writing actual code

The Paradise PU College, Basapura, Bengaluru | Page 7


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

● Easy to understand even for non-programmers

● Ensures no important step is missed

● Can be converted to any programming language

The Paradise PU College, Basapura, Bengaluru | Page 8


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

4.5 Flow of Control


The flow of control describes the order in which steps of an algorithm are executed. There are three
types:
● Sequence – steps executed one after another in order
● Selection – a step is chosen based on a condition (if-else)
● Repetition – steps are repeated until a condition is met (loop)

4.5.1 Sequence
In a sequential algorithm, every step is executed exactly once, one after the other, top to bottom.
Examples 4.1 – 4.3 above are sequential algorithms.

4.5.2 Selection (Decision Making)


Sometimes we need to choose between two or more actions based on a condition. This is called
selection. The condition evaluates to either True or False.

Pseudocode structure:
IF THEN
steps when condition is True
ELSE
steps when condition is False
END IF

Example 4.4 – Check Whether a Number is Even or Odd


Input: any number Output: "Even" or "Odd"

Pseudocode:
PRINT "Enter the Number"
INPUT number
IF number MOD 2 == 0 THEN
PRINT "Number is Even"
ELSE
PRINT "Number is Odd"

The Paradise PU College, Basapura, Bengaluru | Page 9


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

Start

Input number

num MOD 2
== 0?

Yes No

Print "Even" Print "Odd"

Stop

Fig 3: Flowchart – Even or Odd

Example 4.5 – Classify a Person as Child, Teenager, or Adult


Rules: Age < 13 → Child | 13 ≤ Age < 20 → Teenager | Age ≥ 20 → Adult

Pseudocode:
INPUT Age
IF Age < 13 THEN
PRINT "Child"
ELSE IF Age < 20 THEN
PRINT "Teenager"
ELSE
PRINT "Adult"

The Paradise PU College, Basapura, Bengaluru | Page 10


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

Start

Enter Age

Age < 13?


No

Yes
Age < 20?

Print "Child"

Yes No

Print "Teenager" Print "Adult"

Stop

Fig 4: Flowchart – Child / Teenager / Adult

Dry-Run Table for Example 4.5


Input Age Age < 13? Age < 20? Output

8 Yes (True) — Child

16 No (False) Yes (True) Teenager

25 No (False) No (False) Adult

13 No (False) Yes (True) Teenager

The Paradise PU College, Basapura, Bengaluru | Page 11


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

4.5.3 Repetition (Iteration / Loop)


A loop repeats a set of steps until a specified condition becomes false. This avoids writing the same steps
multiple times.

Types of Repetition

● Count-controlled loop (FOR / WHILE with counter) – repeat a known number of times

● Condition-controlled loop (WHILE) – repeat until a condition is false; number of repetitions


unknown

Example 4.6 – Find the Average of 5 Numbers


Pseudocode:
SET count = 0, sum = 0
WHILE count < 5, REPEAT steps below:
INPUT num
sum = sum + num
count = count + 1
COMPUTE average = sum / 5
PRINT average

The Paradise PU College, Basapura, Bengaluru | Page 12


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

Start

count=0, sum=0

count < 5? No average = sum / 5

Yes

Print average

Input num

Stop

sum = sum + num

count = count + 1

Fig 5: Flowchart – Average of 5 Numbers

Dry-Run for Example 4.6 (inputs: 10, 20, 30, 40, 50)
Iteration count num sum count<5?

Before loop 0 — 0 True

1st 0→1 10 0+10=10 True

2nd 1→2 20 10+20=30 True

3rd 2→3 30 30+30=60 True

4th 3→4 40 60+40=100 True

5th 4→5 50 100+50=150 False

After loop 5 — 150 average=150/5=<b>30</b>

Extra Example 4.7 – Factorial of a Number


Factorial of n (written n!) = 1 × 2 × 3 × … × n e.g. 5! = 120

The Paradise PU College, Basapura, Bengaluru | Page 13


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

Pseudocode:
INPUT n
SET fact = 1, i = 1
WHILE i <= n:
fact = fact * i
i = i + 1
PRINT fact

Start

Input n

fact = 1, i = 1

i <= n? No Print fact

Yes

Stop
fact = fact * i

i=i+1

Fig 8: Flowchart – Factorial of a Number

Dry-Run for Factorial (n = 4)


i i <= 4? fact = fact * i Updated fact

1 True 1*1 1

2 True 1*2 2

3 True 2*3 6

The Paradise PU College, Basapura, Bengaluru | Page 14


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

4 True 6*4 24

5 False — Output: 24

The Paradise PU College, Basapura, Bengaluru | Page 15


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

4.6 Verifying Algorithms


After writing an algorithm, we must verify it — i.e., check that it produces the correct output for all possible
inputs.

Dry Run

A dry run is the process of manually tracing through an algorithm with sample inputs, step by step,
to check for errors without running a computer. It helps to:

● Identify incorrect or missing steps

● Find boundary/edge-case bugs

● Confirm the algorithm works for all types of input

Classic Verification Example – Adding Time (Hours & Minutes)


Algorithm: Add T1 (hh1:mm1) + T2 (hh2:mm2) → T_total (hh_total:mm_total)

Pseudocode (First Attempt — with bug):


INPUT hh1, mm1
INPUT hh2, mm2
hh_total = hh1 + hh2
mm_total = mm1 + mm2
PRINT hh_total, mm_total

Algorithm
Test Case T1 T2 Output Expected Correct?

Test 1 5h 20m 7h 30m 12h 50m 12h 50m ✓ Yes

Test 2 4h 50m 2h 20m 6h 70m 7h 10m ✗ No

Bug Fix

When mm_total ≥ 60: add 1 to hh_total and subtract 60 from mm_total.

IF mm_total >= 60 THEN

hh_total = hh_total + 1

mm_total = mm_total - 60

4.7 Comparison of Algorithms


For a given problem, multiple algorithms may exist. We choose the best one based on Time Complexity
(processing time) and Space Complexity (memory used).

Example – Prime Number Check (4 Approaches)


Method How Efficiency

The Paradise PU College, Basapura, Bengaluru | Page 16


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

(i) Full divisor check Test all divisors from 2 to n-1 ■ Slowest – many calculations

(ii) Half divisor check Test divisors from 2 to n/2 ✓ Better – halves the work

(iii) Square root check Test divisors from 2 to √n ✓✓ Even faster

(iv) Pre-stored primes Divide only by known primes < n ✓✓ Fastest calc, but uses extra memory

Start

Input n

Stop

i = 2, flag = True

Print result
(flag)
No

i <= n//2?

Yes

No
n % i == 0? i=i+1

Yes

flag = False

Fig 9: Flowchart – Prime Number Check

Time Complexity vs Space Complexity


Term What it measures Goal

Time Complexity Amount of processing time / number of steps


Minimise (faster is better)

Space Complexity Amount of memory / storage used Minimise (less memory is better)

The Paradise PU College, Basapura, Bengaluru | Page 17


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

4.8 Coding
Once an algorithm is finalised, it is written in a high-level programming language (e.g. Python, C, C++,
Java). The code follows the syntax (grammar rules) of the language.

Language Type Examples Advantages Disadvantages

Low-level (Machine) 0s and 1s Directly understood by CPU Very hard for humans to write/read

Assembly Language MOV, ADD Slightly readable Machine-specific; hard to maintain

High-level Language Python, C, Java Easy to read/write; portable Needs compiler/interpreter

Source Code → Machine Code

A program written in a high-level language is called source code. It must be translated to machine
language using a Compiler or Interpreter before the computer can execute it.

● Compiler: translates the entire program at once (e.g. C, C++)

● Interpreter: translates and executes line by line (e.g. Python)

4.9 Decomposition
Decomposition is the technique of breaking a complex problem into smaller, manageable sub-problems.
Each sub-problem is solved independently, then the solutions are integrated to solve the original problem.

Spirit of Decomposition

"Decompose a complex problem into simpler problems, get one's thinking straight in these
simpler problems, put these analyses together with logical glue."

— Howard Raffa, mathematician

Example – Railway Reservation System (Decomposed)


● Trains information (days, timings, stations, classes, berths)
● Reservation information (booking, cancellation, waiting list)
● Information about staff, security, railway infrastructure
● Billing service
● Food service
● Other details about railways

Benefits of Decomposition
● Makes complex problems manageable
● Each sub-problem can be solved by different teams / experts
● Sub-problems can be developed and tested independently
● Promotes code reusability

The Paradise PU College, Basapura, Bengaluru | Page 18


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

Additional Practice Examples


Practice 1 – Greatest of Two Numbers
Input: A, B Output: Larger number

Pseudocode:
INPUT A, B
IF A > B THEN
PRINT "A is the Greatest"
ELSE
PRINT "B is the Greatest"

Start

Input A, B

A > B?

Yes No

Print A is largest Print B is largest

Stop

Fig 7: Flowchart – Greatest of Two Numbers

Practice 2 – Calculate Percentage of Marks (3 Subjects)


Input: Marks in CS, Maths, Physics (out of 100 each)
INPUT cs, maths, physics
COMPUTE total = cs + maths + physics
COMPUTE percentage = (total / 300) * 100
PRINT total, percentage

Practice 3 – Water Bill Calculation

The Paradise PU College, Basapura, Bengaluru | Page 19


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

Slabs: 0–100 units @ ■5 | 101–250 units @ ■10 | >250 units @ ■20 | Meter charge: ■75
INPUT units
IF units <= 100 THEN
bill = units * 5
ELSE IF units <= 250 THEN
bill = 100 * 5 + (units - 100) * 10
ELSE
bill = 100 * 5 + 150 * 10 + (units - 250) * 20
total_bill = bill + 75
PRINT total_bill

Chapter Summary
Concept Description

Process of identifying a problem, designing an algorithm, and implementing


Problem Solving it as a program.

Steps Analyse Problem → Develop Algorithm → Coding → Testing & Debugging

Algorithm Finite, ordered sequence of well-defined steps that solves a problem.

Good Algorithm Must be precise, unique, finite; must have input and output.

Flowchart Visual (pictorial) representation of an algorithm using standard shapes.

Pseudocode Informal, English-like text representation of an algorithm.

Sequence Steps executed one after another in order.

Selection Choosing between alternatives based on a condition (if-else).

Repetition Repeating steps until a condition is false (loop / iteration).

Dry Run Manually tracing an algorithm with sample inputs to verify correctness.

Time Complexity Amount of computation / time an algorithm needs.

Space Complexity Amount of memory an algorithm uses.

Decomposition Breaking a complex problem into simpler sub-problems.

Important Questions for Exam


1 Mark Questions
1. Define Algorithm.
2. What is a Flowchart?

The Paradise PU College, Basapura, Bengaluru | Page 20


I PUC Computer Science | Chapter 4: Introduction to Problem Solving Karnataka DPUE Board

3. What is Pseudocode?
4. What is Decomposition?
5. Name the four steps of problem solving.
6. What is a Dry Run?
7. What is meant by GIGO?
8. List any two characteristics of a good algorithm.

2 Mark Questions
1. What is the difference between a compiler and an interpreter?
2. Distinguish between Flowchart and Pseudocode.
3. Explain Time Complexity and Space Complexity.
4. What are the three types of flow of control?
5. Write the pseudocode to find the largest of two numbers.

3 Mark Questions
1. Explain the steps for problem solving with an example.
2. Write the algorithm and draw a flowchart to find whether a number is even or odd.
3. Write pseudocode to find the factorial of a number.
4. Explain selection with an example and flowchart.
5. Explain repetition with a suitable example and flowchart.

5 Mark Questions
1. Explain the characteristics of a good algorithm. Write an algorithm to check whether a number is prime.
2. Write pseudocode and draw flowchart to classify a person as Child, Teenager or Adult based on age.
3. Explain decomposition with an example. How does it help in solving complex problems?
4. Write an algorithm to calculate the average of N numbers entered by the user.
5. Explain the four methods to check whether a number is prime and compare their efficiencies.

The Paradise PU College, Basapura, Bengaluru | Page 21

You might also like