Courses / Computer Science XI (HSSC-I) – FBISE Complete SLO-Based Course
2.1 Problem Solving: Algorithms, Pseudocode, Flowcharts and Tracing
Unit 2 – Computational Thinking and Algorithms · 80 min read · 12 MCQs to check yourself
Introduction to flowcharts and pseudocode for students
1. Problem-solving steps
- Understand the problem – what are the inputs, the required outputs and the constraints?
- Plan the solution – break it into steps (an algorithm) and write it as pseudocode or a flowchart.
- Implement – translate the algorithm into a programming language (Python).
- Test and refine – run with test data, trace by hand, fix errors and improve.
An algorithm is a finite, ordered set of unambiguous steps that solves a problem. A good algorithm has: clear input and output, finiteness (it stops), definiteness (each step has one meaning), effectiveness (each step can be done) and correctness.
2. Pseudocode
Pseudocode is a structured, English-like description of an algorithm that is independent of any programming language. It is not executed by a computer, but it should be precise enough to translate directly into code.
Conventions (FBISE expects these):
| Convention | Example |
|---|---|
| Keywords in CAPITALS | INPUT, OUTPUT, IF … THEN … ELSE … ENDIF, WHILE … DO … ENDWHILE, FOR … TO … NEXT |
| One statement per line, numbered lines | 1 SET total ← 0 |
| Indentation shows blocks inside IF/loops | statements inside a loop are indented 4 spaces |
| Assignment with ← (or =) | count ← count + 1 |
| Declarations with data types | DECLARE marks : INTEGER, DECLARE name : STRING |
| Comments explain intent | // add only even numbers |
| Operators | + - * / MOD DIV, = <> < > <= >=, AND OR NOT |
| Readable font, consistent case for names | monospace font, camelCase or snake_case used consistently |
Example – sum of even numbers 2 … N (model paper):
1 DECLARE N, sum, i : INTEGER
2 INPUT N
3 SET sum ← 0
4 SET i ← 2
5 WHILE i <= N DO
6 sum ← sum + i // add the even number
7 i ← i + 2 // next even number
8 ENDWHILE
9 OUTPUT sum
Modified to print the product of odd numbers from 1 to 30:
1 DECLARE product, i : INTEGER
2 SET product ← 1 // product starts at 1, not 0
3 SET i ← 1
4 WHILE i <= 30 DO
5 product ← product * i
6 i ← i + 2
7 ENDWHILE
8 OUTPUT product
Changes: start the product at 1 (multiplying by 0 would always give 0), start i at 1 (first odd number), multiply instead of add, fixed limit 30.
3. Flowcharts
A flowchart is a diagram of an algorithm using standard symbols joined by arrows (flow lines).
| Symbol | Shape | Used for |
|---|---|---|
| Terminal | Rounded rectangle (oval) | Start / Stop |
| Input/Output | Parallelogram | INPUT, OUTPUT/print |
| Process | Rectangle | Calculations and assignments |
| Decision | Diamond | A condition with Yes/No exits |
| Flow line | Arrow | Order of steps |
| Connector | Small circle | Joins parts of a long flowchart |
| Predefined process | Rectangle with double sides | Calling a sub-program/function |
4. Tracing an algorithm (dry run)
A trace table records the value of every variable after each step. It shows how an algorithm works and helps find logic errors.
Trace the sum-of-evens algorithm for N = 7:
| Step | i | i <= N ? | sum | Output |
|---|---|---|---|---|
| start | 2 | – | 0 | |
| 1st loop | 4 | 2 <= 7 Yes | 2 | |
| 2nd loop | 6 | 4 <= 7 Yes | 6 | |
| 3rd loop | 8 | 6 <= 7 Yes | 12 | |
| check | 8 | 8 <= 7 No | 12 | 12 |
| The output 2 + 4 + 6 = 12 is correct, so the algorithm is correct for this test. |
The same algorithm in Python:
N = 7
total = 0
i = 2
while i <= N:
total = total + i
i = i + 2
print(total)
Output:
12
5. Systematic testing
| Test data type | Purpose | Example for "marks 0–100" |
|---|---|---|
| Normal (valid) | Typical values that should be accepted | 45, 78 |
| Boundary (extreme) | Values at the edges of the range | 0, 100 (and just outside: −1, 101) |
| Erroneous (invalid) | Wrong values that must be rejected | −20, 150, "abc" |
Refine the artifact after each test: fix the step, re-trace and re-test.
6. Evaluating algorithms
| Criterion | Question | Example |
|---|---|---|
| Correctness | Does it give the right output for all valid inputs and stop? | Check with boundary cases such as N = 0, N = 1, N = 2 |
| Efficiency | How many steps/how much memory as input size grows? | Adding 2, 4, 6 … takes about N/2 steps; the formula k(k+1) with k = ⌊ N/2 ⌋ gives the answer in one step |
| Clarity | Is it easy to read, understand and maintain? | Meaningful names (total, not t), comments, indentation |
Efficiency example: the sum 1 + 2 + … + n can be found with a loop (n additions) or with the formula S = (n(n+1))/2 (one calculation). Both are correct; the formula is more efficient.
n = 100
loop_sum = 0
for k in range(1, n + 1):
loop_sum += k
print(loop_sum, n * (n + 1) // 2)
Output:
5050 5050
Practical (PBA) tasks
- Write pseudocode and draw a flowchart to find the largest of three numbers; trace it for (4, 9, 2).
- Write pseudocode to print the multiplication table of a number; convert it to Python and test with normal and boundary data.
- Draw a flowchart for grading: A (≥ 80), B (≥ 70), C (≥ 60), F otherwise; test with 80, 79, 60, 59, 101.
Problem solving with pseudocode and flowcharts
Short and long questions
1. Explain how an algorithm is systematically tested and refined. Trace the "sum of even numbers 2 to N" algorithm for N = 7 and N = 1 and comment on its correctness.
Model answer
Systematic testing: (1) choose test data – normal, boundary and erroneous; (2) work out the expected output by hand; (3) dry-run the algorithm with a trace table (or run the program); (4) compare actual with expected output; (5) if different, locate the faulty step, fix it (refine) and re-test all cases. Trace N = 7: i = 2, sum = 0 → sum 2, i 4 → sum 6, i 6 → sum 12, i 8 → 8 <= 7 false → output 12 (2 + 4 + 6 = 12 ✔). Trace N = 1: i = 2, 2 <= 1 false → loop never runs → output 0 ✔ (there are no even numbers up to 1). The algorithm is correct for both normal and boundary cases. It is reasonably efficient (about N/2 iterations) and clear (meaningful names, one loop). It could be made more efficient with the formula k(k + 1) where k = N DIV 2.
2. Given the following pseudocode, modify it to print the product of odd numbers from 1 to 30. Step 1: Initialize sum with 0. Step 2: Initialize i with 2. Step 3: While i <= N. Step 4: Add i to sum. Step 5: Increment i by 2. Step 6: Print sum.
Model answer
1 SET product ← 1
2 SET i ← 1
3 WHILE i <= 30 DO
4 product ← product * i
5 i ← i + 2
6 ENDWHILE
7 OUTPUT product
Changes: product starts at 1 (not 0), i starts at 1 (first odd number), multiplication replaces addition, and the limit is 30.
3. What is an algorithm? Write any four characteristics of a good algorithm.
Model answer
An algorithm is a finite, ordered sequence of unambiguous steps that solves a problem. Characteristics: (1) well-defined input and output; (2) finiteness – it terminates; (3) definiteness – every step is clear and has one meaning; (4) effectiveness – each step can be carried out; (5) correctness – gives the right result for all valid inputs.
4. Draw flowchart symbols for terminal, process, input/output and decision, and state their use.
Model answer
Terminal – rounded rectangle/oval: Start/Stop. Process – rectangle: calculations/assignments (sum = sum + i). Input/Output – parallelogram: INPUT N / PRINT sum. Decision – diamond: condition with Yes/No branches (i <= N ?). Arrows (flow lines) connect symbols and show the order.
5. Differentiate between pseudocode and flowchart.
Model answer
| Pseudocode | Flowchart |
|---|---|
| Written in structured English-like text | Drawn with standard graphical symbols |
| Quick to write and edit, easy to convert to code | Easier to see the flow at a glance |
| Hard to show complex branching visually | Becomes large and hard to modify for big programs |
| Uses keywords (IF, WHILE, OUTPUT) and indentation | Uses shapes (diamond, parallelogram) and arrows |
6. Trace the following algorithm for N = 5 and give the output: sum ← 0, i ← 1; WHILE i <= N: sum ← sum + ii, i ← i + 2; OUTPUT sum.
Model answer
| i | i <= 5? | sum |
|---|---|---|
| 1 | Yes | 0 + 1 = 1 |
| 3 | Yes | 1 + 9 = 10 |
| 5 | Yes | 10 + 25 = 35 |
| 7 | No | 35 |
| Output: 35 (sum of squares of odd numbers up to 5). |
7. What is meant by evaluating an algorithm for efficiency, correctness and clarity?
Model answer
Efficiency: how much time (number of steps) and memory it uses as input grows – fewer steps is better. Correctness: it produces the correct output for all valid inputs and terminates – checked by tracing and testing. Clarity: it is easy to read and maintain – meaningful names, comments, indentation and simple logic.
8. Name three types of test data with an example for an age field that accepts 15 to 25.
Model answer
Normal: 18, 22 (accepted). Boundary: 15 and 25 (limits, accepted) and 14/26 (just outside, rejected). Erroneous: −3, 200, "twenty" (rejected).
9. Write pseudocode and draw a flowchart that inputs marks of a student and prints the grade: A for 80 and above, B for 70–79, C for 60–69 and F below 60. Also give suitable test data to test your algorithm.
Model answer
Pseudocode
1 DECLARE marks : INTEGER
2 INPUT marks
3 IF marks < 0 OR marks > 100 THEN
4 OUTPUT "Invalid marks"
5 ELSE IF marks >= 80 THEN
6 OUTPUT "A"
7 ELSE IF marks >= 70 THEN
8 OUTPUT "B"
9 ELSE IF marks >= 60 THEN
10 OUTPUT "C"
11 ELSE
12 OUTPUT "F"
13 ENDIF
Flowchart: Start → Input marks (parallelogram) → decision "marks < 0 or > 100?" Yes → print Invalid → Stop; No → decision "marks >= 80?" Yes → print A; No → "marks >= 70?" Yes → print B; No → "marks >= 60?" Yes → print C; No → print F; all paths join → Stop. Test data: normal 85 → A, 72 → B, 65 → C, 40 → F; boundary 80 → A, 79 → B, 60 → C, 59 → F, 0 → F, 100 → A; erroneous −5, 101 → Invalid.
This lesson ends with a quiz of 12 MCQs. Open the lesson on PrepOasis to take it and save your progress.
Previous: Unit 1 Review, SLO Checklist and Unit Test · Next: 2.2 Computational Thinking and Searching Algorithms
All lessons in Computer Science XI (HSSC-I) – FBISE Complete SLO-Based Course