Chapter 3: Introduction to Problem-Solving
Before diving into writing code, it is essential to understand the systematic approach to solving problems using computers. Programming is not just about typing syntax; it’s about logic and structured thinking.
3.1 Steps for Problem-Solving
Solving a problem using a computer involves a life cycle consisting of several distinct stages:
- Analyzing the Problem: Understanding the core issue, identifying what the inputs are, what output is expected, and what constraints exist.
- Developing an Algorithm: Creating a step-by-step logical sequence of instructions to solve the problem.
- Coding: Translating the algorithm into a specific programming language (like Python) that the computer can execute.
- Testing: Running the program with various sets of test data (including edge cases) to ensure it produces the correct output.
- Debugging: The process of finding and fixing errors (bugs) in the code that are discovered during testing.
3.2 Representation of Algorithms
Algorithms can be represented visually or textually to make them easier to understand and communicate.
Pseudocode
Pseudocode is an informal, plain English description of the steps of an algorithm. It does not use strict programming syntax. Example Pseudocode to find the sum of two numbers:
Step 1: Input first number as A
Step 2: Input second number as B
Step 3: Calculate SUM = A + B
Step 4: Print SUM
Flowcharts
A flowchart is a graphical representation of an algorithm using standard geometric shapes connected by arrows.
- Oval: Start/Stop
- Parallelogram: Input/Output
- Rectangle: Processing/Calculation
- Diamond: Decision/Condition
- Arrows: Flow of control
3.3 Decomposition
Decomposition (also known as factoring) is the process of breaking down a complex problem or system into smaller, more manageable parts. By solving these smaller sub-problems individually and then combining their solutions, we can solve the original complex problem much more easily.
Example: Building a calculator application. Instead of trying to write the entire application at once, decompose it into:
- Building the User Interface.
- Writing the logic for Addition.
- Writing the logic for Subtraction.
- Writing the logic for Multiplication/Division.
Competency Based Questions
Q1. Case-Based Scenario Meera is tasked with creating a program that calculates the compound interest for a bank. She immediately opened her laptop and started typing Python code, but she soon got confused and her program gave incorrect results.
- Which crucial problem-solving step did Meera skip?
- What should she have done before starting to code?
Q2. Find the Error Consider the following pseudocode meant to check if a person is eligible to vote:
Step 1: Input Age
Step 2: If Age < 18, Print "Eligible to vote"
Step 3: Else, Print "Not eligible to vote"
Identify the logical error in the pseudocode and provide the corrected version.
Q3. Application-Oriented Draw a flowchart (or describe the shapes and flow) to input a number and check whether it is even or odd.
Q4. Assertion-Reasoning
- Assertion (A): Debugging is done before the coding phase in the problem-solving cycle.
- Reason (R): Debugging is the process of finding and fixing errors in the written code. Choose the correct option: a) Both A and R are true and R is the correct explanation of A. b) Both A and R are true but R is NOT the correct explanation of A. c) A is true but R is false. d) A is false but R is true.
Answers to Competency Based Questions
A1.
- Meera skipped the Analyzing the Problem and Developing an Algorithm steps.
- She should have first understood the formula for compound interest, determined her inputs, and written an algorithm or flowchart to structure her logic before writing any code.
A2.
Error: The condition Age < 18 is mapped to “Eligible to vote”, which is logically backward.
Corrected Pseudocode:
Step 1: Input Age
Step 2: If Age >= 18, Print "Eligible to vote"
Step 3: Else, Print "Not eligible to vote"
A3. Flowchart Description:
- Oval: START
- Parallelogram: INPUT Number (N)
- Rectangle: Calculate Remainder R = N % 2
- Diamond: Is R == 0?
- If YES (True arrow) -> Parallelogram: PRINT “Even”
- If NO (False arrow) -> Parallelogram: PRINT “Odd”
- Oval: STOP (Both True and False paths merge here).
A4. d) A is false but R is true. Debugging is done after or during the coding and testing phases, not before it, because you cannot debug code that hasn’t been written yet.