Sample Notes: Algorithm Design And Problem-Solving
Program Development Life Cycle
Analysis
- First step of solving any problem using algorithms or programming.
- Includes:
- Identifying the problem to be solved.
- Performing abstraction: ignoring unnecessary details.
- Performing decomposition: breaking a large problem into smaller sub-problems.
- Identifying:
- Inputs (what data is needed),
- Processes (what needs to be done to the data),
- Outputs (what results are expected),
- Storage (if data needs to be saved or retrieved).
Design
- Logical structure of the solution is formed.
- Main methods of design:
- Pseudocode: Plain English-like code that outlines logic.
- Flowcharts: Graphical symbols representing process flows.
- Structure diagrams: Hierarchical module breakdown.
- Good design ensures modularity, reusability, and clarity.
Coding
- Translating the designed solution into an actual programming language.
- Follows best practices:
- Meaningful variable names.
- Code indentation.
- Modular functions.
- Includes iterative testing during development to catch bugs early.
Testing
- Verifies whether the program functions correctly and meets requirements.
- Testing types:
- Normal data: Expected/typical input.
- Abnormal data: Invalid input to test robustness.
- Boundary data: Edge of acceptable range.
- Extreme data: Max/min values within acceptable range.
- Use test tables to record:
- Inputs
- Expected outputs
- Actual outputs
- Pass/fail result
System Decomposition
- Any large system can be divided into sub-systems, and each sub-system further into modules.
- Helps manage complexity, aids parallel development, and improves code maintenance.
- Example:
- Main system: E-commerce Website
- Sub-systems: User Login, Payment Gateway, Product Catalog
- Each sub-system can be decomposed further into functions and methods.
- Main system: E-commerce Website
Designing Solutions
Methods:
- Pseudocode:
- E.g.
total = 0 FOR i = 1 TO 10 total = total + i NEXT i
- E.g.
- Flowcharts: Use symbols:
- Oval = Start/End
- Rectangle = Process
- Diamond = Decision
- Parallelogram = Input/Output
- Structure Diagrams:
- Tree-like diagrams showing how system is broken down hierarchically.
Understanding Algorithms
- Must be able to explain:
- What the algorithm does.
- What the inputs and outputs are.
- How data is processed.
- Example:
total = 0 FOR each number in list total = total + number NEXT
- This calculates the sum of a list.
Standard Methods of Solution
Linear Search
- Go through each item in a list one by one.
- Stop when the item is found or list ends.
- Inefficient for large datasets.
Bubble Sort
- Compare adjacent items and swap if out of order.
- Repeat until entire list is sorted.
- Easy to implement but not efficient.
Totalling
- Add up values using a loop and a total variable.
Counting
- Keep a counter variable and increment when condition matches.
Finding Max/Min
- Start with first value as reference.
- Compare and update if higher/lower value found.
Averaging
- Total of values divided by number of values.
Validation Checks
Type | Purpose |
---|---|
Range Check | Ensures input is within defined minimum and maximum values. |
Length Check | Ensures correct number of characters. |
Type Check | Ensures correct data type (e.g. number, string). |
Presence Check | Ensures field is not empty. |
Format Check | Input follows a specific pattern (e.g. email format). |
Check Digit | Used in numeric codes to validate accuracy via algorithms. |
Verification Checks
Type | Purpose |
---|---|
Visual Check | Manually checking data entered. |
Double Entry | Entering the data twice and comparing both inputs. |
Test Data Types
Type | Example |
---|---|
Normal | Regular input the system expects. |
Abnormal | Completely invalid input (e.g. text in age field). |
Extreme | Largest/smallest valid values. |
Boundary | Just at or outside acceptable limits. |
Dry Run and Trace Tables
- Dry run: Manually execute algorithm line by line.
- Trace table: Document variables and outputs at each step.
- Useful for:
- Debugging logic errors
- Understanding algorithm behavior
Error Identification and Correction
- Must be able to:
- Read and understand a flawed algorithm.
- Locate logic/syntax/structure errors.
- Suggest appropriate corrections.
Writing and Amending Algorithms
- Express algorithms in:
- Pseudocode
- Flowcharts
- Actual program code
- Be precise:
- Write
x > y
, not “x is greater than y”. - Use indentation and clear control structures.
- Write
- Example:
IF score >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF
Writing Pseudocode – Key Concepts and Conventions
What is Pseudocode?
- Pseudocode is a way to express algorithms in a structured, human-readable form without using syntax of a specific programming language.
- Acts as a bridge between algorithm design and actual coding.
- Must be clear, logically ordered, and easy to convert into any programming language.
Standard Pseudocode Syntax and Items
1. Input and Output
- INPUT – for receiving user data
Example:
INPUT name
- READ – for reading from a file or database
Example:
READ score FROM results.txt
- OUTPUT – for displaying a result
Example:
OUTPUT "Enter your name:"
OUTPUT total
2. Variables
- Variables are declared and used directly. No specific data types in basic pseudocode.
- Example:
total ← 0
name ← ""
3. Assignment
- Use
←
to assign values to variables.
Example:
sum ← num1 + num2
4. Arithmetic Operations
- Standard operations used in math:
+
,-
,*
,/
,%
(modulus)
- Example:
average ← total / count
remainder ← number % 2
5. Selection / Decision Making (IF Statements)
- General structure:
IF condition THEN statements ELSE alternative statements ENDIF
- Example:
IF age ≥ 18 THEN OUTPUT "Adult" ELSE OUTPUT "Minor" ENDIF
6. Nested IF Statements
- When decisions are layered within decisions:
IF score ≥ 90 THEN grade ← "A" ELSE IF score ≥ 80 THEN grade ← "B" ELSE grade ← "F" ENDIF ENDIF
7. Loops
WHILE Loop (Condition-Checked Before Entering)
WHILE condition DO
statements
ENDWHILE
Example:
count ← 1
WHILE count ≤ 5 DO
OUTPUT count
count ← count + 1
ENDWHILE
FOR Loop (Fixed number of repetitions)
FOR i ← 1 TO 10 DO
OUTPUT i
NEXT i
REPEAT…UNTIL Loop (Condition-Checked After Loop Runs At Least Once)
REPEAT
INPUT guess
UNTIL guess = secret
8. Procedures and Functions
Procedure
PROCEDURE ShowWelcome()
OUTPUT "Welcome to the system."
ENDPROCEDURE
Function
FUNCTION Square(num)
RETURN num * num
ENDFUNCTION
9. Comments
- Used to explain logic (not always required in exams, but useful):
// This calculates the total sales
10. String Handling
- Concatenation:
fullName ← firstName + " " + lastName
- Length:
length ← LEN(text)
- Substring:
part ← SUBSTRING(text, 1, 5)
11. Common Operations
Operation | Pseudocode Example |
---|---|
Totalling | total ← total + number |
Counting | count ← count + 1 |
Finding max | IF num > max THEN max ← num |
Finding min | IF num < min THEN min ← num |
Averaging | average ← total / count |
Best Practices for Writing Pseudocode
- Use indentation to show structure (especially in nested loops/conditions).
- Keep the logic language-agnostic (avoid specific syntax like
print()
orint
). - Use meaningful variable names.
- Clearly indicate start and end of control structures (
IF...ENDIF
,WHILE...ENDWHILE
, etc.). - Ensure logical flow is easy to follow.