Unit 1: Problem Solving and Programming Fundamentals

Table of Contents

Introduction to Problem Solving

Problem-solving is the process of identifying a problem, breaking it down into smaller, manageable parts, and developing a logical, step-by-step solution to solve it.

In computer programming, this means translating a real-world problem into a set of instructions (an algorithm) that a computer can understand and execute to produce a desired output.

Algorithms

An algorithm is a finite, well-defined, step-by-step set of instructions or rules designed to solve a specific problem.

Algorithms are written in pseudo-code, which is a plain English, informal description of the steps, rather than in a specific programming language.

Characteristics of a Good Algorithm

Flowcharts

A flowchart is a graphical or visual representation of an algorithm. It uses standard symbols to show the sequence of operations and the flow of logic.

Common Flowchart Symbols

Symbol Name Function
Oval Terminator Represents the Start or End of the algorithm.
Rectangle Process Represents a calculation, assignment, or any data manipulation. (e.g., `Set count = 0`)
Parallelogram Input / Output Represents reading data (e.g., `Read N`) or printing data (e.g., `Print Sum`).
Diamond Decision Used for conditional statements. Has one entry point and two exit points (e.g., Yes/No).
Arrows Flow Lines Connect the symbols and show the direction of logic flow.

Control Structures (Decision Making)

These structures allow the algorithm to make choices and execute different paths based on certain conditions.

1. if-then

Executes a block of code only if a condition is true.

IF (condition is true) THEN
    Execute this statement
ENDIF

2. if-then-else

Executes one block of code if the condition is true, and a different block if it is false.

IF (condition is true) THEN
    Execute statement_block_A
ELSE
    Execute statement_block_B
ENDIF

3. nested if-then-else

A multi-way decision structure, often used to check several related conditions. (Also known as an `if-elseif-else` ladder).

IF (condition_1 is true) THEN
    Execute statement_block_A
ELSE IF (condition_2 is true) THEN
    Execute statement_block_B
ELSE
    Execute statement_block_C
ENDIF

Control Structures (Loops)

Loops (or iterations) allow a block of code to be executed repeatedly.

1. for loop

A **counter-controlled** loop. Use this when you know exactly how many times you want the loop to run (e.g., 10 times, or N times).

FOR counter = start_value TO end_value
    Execute this block of statements
ENDFOR

2. while loop

A **pre-test** loop. The condition is checked *before* the loop body is executed. The body will run as long as the condition is true. It might not run at all if the condition is false from the start.

WHILE (condition is true) DO
    Execute this block of statements
    (Ensure a statement here updates the condition,
     e.g., increment a counter)
ENDWHILE

3. do-while loop

A **post-test** loop. The loop body is executed first, and *then* the condition is checked. This guarantees the loop body runs at least once.

DO
    Execute this block of statements
WHILE (condition is true)
Exam Tip: Be able to state the key difference between a while loop (pre-test, 0 or more executions) and a do-while loop (post-test, 1 or more executions).

Algorithms and Examples

Algorithm: Swapping Two Numbers (a, b)

START
    Read a
    Read b
    
    // Use a temporary variable
    Set temp = a
    Set a = b
    Set b = temp
    
    Print "a is now: ", a
    Print "b is now: ", b
END

Algorithm: Factorial of a Number (N)

START
    Read N
    
    Set fact = 1
    Set i = 1
    
    WHILE (i <= N) DO
        fact = fact * i
        i = i + 1
    ENDWHILE
    
    Print "Factorial of ", N, " is ", fact
END

Algorithm: Fibonacci Series (Print first N terms)

START
    Read N
    
    Set a = 0
    Set b = 1
    
    Print a
    Print b
    
    FOR i = 3 TO N
        Set next = a + b
        Print next
        Set a = b
        Set b = next
    ENDFOR
END

Algorithm: Roots of a Quadratic Equation (ax² + bx + c = 0)

START
    Read a, b, c
    
    Set d = b*b - 4*a*c
    
    IF (d > 0) THEN
        // Real and distinct roots
        Set root1 = (-b + sqrt(d)) / (2*a)
        Set root2 = (-b - sqrt(d)) / (2*a)
        Print "Real and distinct roots: ", root1, root2
    ELSE IF (d == 0) THEN
        // Real and equal roots
        Set root1 = -b / (2*a)
        Print "Real and equal roots: ", root1
    ELSE
        // Complex roots
        Set realPart = -b / (2*a)
        Set imagPart = sqrt(-d) / (2*a)
        Print "Complex roots: ", realPart, "+i", imagPart,
              " and ", realPart, "-i", imagPart
    ENDIF
END

Introduction to Arrays

An array is a data structure that stores a collection of elements of the same data type in contiguous (one after another) memory locations.

Each element is identified by at least one index (or key). A one-dimensional (1D) array is like a list, e.g., `A = [5, 10, 3, 8]`. Here, `A[0]` is 5, `A[1]` is 10, etc. (using 0-based indexing).

Common Exercises Involving Arrays