Problem Solving with Algorithms: Qualities and Structure
Before a civil engineer pours concrete to build a bridge across a river, what do they do? They draw architectural blueprints, calculate weight loads, and map out each phase of construction.
In software development, before a programmer types a single line of Python, C++, or Java code, what do they do? They write an Algorithm!
An algorithm is the logical foundation of all computer programming. A computer has zero human common sense: it will execute exactly what you tell it to do, even if your instructions are completely foolish! In this lesson, we will learn how to break complex real-world problems into clear, structured, and foolproof algorithmic steps.
Learning Outcomes
By the end of this lesson, you will be able to:
- Define an Algorithm and explain why planning is mandatory before coding.
- State the five golden qualities of an effective algorithm (Finiteness, Definiteness, Input, Output, Effectiveness).
- Identify the three fundamental building blocks: Sequence, Selection (Conditionals), and Iteration (Loops).
- Write structured pseudocode for real-world problem solving.
Key Vocabulary
| Term | Technical Meaning |
|---|---|
| Algorithm | A finite, ordered set of unambiguous instructions that performs a task and terminates. |
| Finiteness | The guarantee that an algorithm will end after a countable number of steps. |
| Definiteness | Every instruction is precisely stated with no double meanings or vagueness. |
| Pseudocode | A structured, human-readable description of an algorithm using plain English keywords. |
| Selection (Branching) | Making a decision between two paths based on a condition (IF ... THEN ... ELSE). |
| Iteration (Looping) | Repeating a block of instructions until a condition is fulfilled (WHILE, FOR). |
The Five Pillars of a Valid Algorithm
Donald Knuth, one of the world's greatest computer scientists, established that every valid algorithm must possess five essential properties:
=========================================================================
PROPERTY WHAT IT MEANS
=========================================================================
1. INPUT Accepts zero or more well-defined starting values.
2. OUTPUT Produces at least one meaningful, useful result.
3. DEFINITENESS Every step is crystal clear and completely unambiguous.
4. FINITENESS Must stop and terminate after a finite number of steps.
5. EFFECTIVENESS Every step is realistic, practical, and executable.
=========================================================================
Study Notes: The Three Fundamental Control Structures
Every program written in every language across history is assembled from just three control patterns:
1. Sequence (Step-by-Step Order)
Instructions execute strictly one after the other from top to bottom:
Step 1: Open the water tap.
Step 2: Wet your hands with clean water.
Step 3: Apply soap and rub hands for 20 seconds.
Step 4: Rinse thoroughly and turn off the tap.
2. Selection (Making Decisions: IF-THEN-ELSE)
The algorithm chooses which path to take based on whether a condition evaluates to True or False:
IF temperature is greater than 35°C THEN
Turn ON the ceiling fan
ELSE
Turn OFF the ceiling fan
ENDIF
3. Iteration (Repeating Actions: LOOPS)
Repeating instructions without having to rewrite them twenty times:
Worked Example: An ATM Cash Withdrawal Algorithm
Let us write a clear, robust pseudocode algorithm for an ATM withdrawal in Nigeria:
ALGORITHM: ATM_Cash_Withdrawal
INPUT: Card, PIN, Requested_Amount
OUTPUT: Cash Dispensed or Error Message
BEGIN
1. Prompt user: "Please insert your debit card"
2. Read Card_Data
3. Prompt user: "Enter 4-digit PIN"
4. Input Entered_PIN
5. IF Entered_PIN != Stored_PIN THEN
Display "Incorrect PIN. Transaction cancelled."
Eject Card
HALT
ENDIF
6. Prompt user: "Enter withdrawal amount"
7. Input Requested_Amount
8. IF Requested_Amount > Account_Balance THEN
Display "Insufficient funds in your account."
ELSE
Account_Balance = Account_Balance - Requested_Amount
Dispense Requested_Amount in cash
Display "Please take your cash and receipt"
ENDIF
9. Eject Card
10. Display "Thank you for banking with us!"
END
Notice: Every step is definite. The algorithm checks both conditions (PIN validity and account balance) and terminates safely!
Practical Class Activity: Designing an Algorithm
Write a step-by-step pseudocode algorithm in your notebook for the following real-life problem:
Problem: A school library has a rule: students can borrow a book for 14 days. If the student returns the book late, they must pay a fine of ₦50 for every day it was delayed.
- Input: Days_Kept
- Decision: Is Days_Kept > 14?
- Calculation: If late, Days_Late = Days_Kept - 14, and Fine = Days_Late * 50.
- Output: Display "No fine" or display "Your late fee is ₦[Fine]".
Common Mistakes & Corrections
| Common Mistake | Why It Is Incorrect | Proper Correction |
|---|---|---|
| "Instructions like: 'Add a little bit of salt until it tastes nice'." | "A little bit" and "tastes nice" are vague and ambiguous; computers cannot taste! | Make steps definite and exact: "Add 5 grams of salt". |
| "Creating an algorithm with an endless infinite loop that never halts." | It violates the principle of Finiteness, causing the computer to freeze. | Always ensure every loop has a clear exit condition so the algorithm terminates. |
Notebook Summary
Copy Into Your Computing Notebook
- An Algorithm is a step-by-step sequence of unambiguous instructions to solve a problem.
- The 5 golden qualities: Input, Output, Definiteness, Finiteness, and Effectiveness.
- The 3 structural building blocks are Sequence, Selection (
IF-ELSE), and Iteration (Loops). - Planning clear algorithms in pseudocode prevents programming errors before writing real code!