Computational Thinking, Algorithm Design and Tracing

IB Computer Science · Computational Thinking, Problem-Solving and Programming

The four habits of computational thinking. Abstraction is deciding what to leave out: a route-planning program models a road network as nodes and weighted edges and ignores the colour of the tarmac, because those details do not affect the answer. Decomposition breaks a problem into sub-problems that can be solved and tested separately. Pattern recognition notices that two sub-problems have the same shape so that one solution serves both. Algorithmic thinking expresses the solution as an unambiguous finite sequence of steps. Examiners award marks for showing these in use, not for defining them.

Expressing an algorithm. A flowchart shows control flow graphically and is easy for non-programmers to read but becomes unwieldy for long algorithms. Pseudocode is language-independent, compact and close to real code, which is why it is used in examinations. Whichever is used, an algorithm must be unambiguous, must terminate, and must have defined inputs and outputs.

The building blocks. Every algorithm is built from four constructs. Sequence executes steps in order. Selection chooses between paths using a condition. Iteration repeats: a count-controlled loop repeats a known number of times, while a condition-controlled loop repeats until something becomes true, which is what you must use when the number of repetitions is not known in advance. Sub-programs package a block of code under a name and take parameters.

Why sub-programs matter more than they appear to. They avoid duplicating code, so a correction is made once rather than in many places. They allow independent testing, since a sub-program with defined inputs and outputs can be tested on its own. They let a large program be divided among several developers. And they support abstraction: the caller needs to know what the sub-program does, not how it does it, so the implementation can be replaced without touching the callers.

Tracing. A trace table records the value of every variable after each step. It is the standard way of determining what an unfamiliar algorithm actually does and of locating a fault. The discipline matters: one column per variable, one column per condition, one row per iteration, and an initialisation row before the loop begins. Attempting to trace mentally rather than on paper is the most common cause of lost marks in this topic.

TRACE TABLE — FINDING THE MAXIMUM VALUE AND ITS POSITION A = [7, 3, 9, 4, 9, 2] MAX = A[0] POS = 0 K = 1 loop while K ≤ 5 if A[K] > MAX then MAX = A[K] ; POS = K end if ; K = K + 1 end loop K A[K] A[K] > MAX MAX POS - - initialize 7 0 1 3 sai 7 0 2 9 TRUE 9 2 3 4 sai 9 2 4 9 false — 9 is NOT greater than 9 9 2 5 2 sai 9 2 6 exit loop 9 2 THE ROW K = 4 IS THE DECIDING ROW: the condition uses a strict GREATER THAN, so the second 9 does NOT update Result: the algorithm returns the position of the FIRST occurrence. Change to greater than or equal and it returns the LAST Table rule: one column for EACH variable, one row for EACH iteration, and an initialization row before entering the loop Also write the last row when the condition is FALSE to prove the loop exits at the right time
Trace questions appear in almost every paper, and they give almost free marks if the table is built correctly — but zero if done in your head. Three rules for building the table. One column for EACH changing variable, plus one column for the condition of each branching statement and of the loop. One row for EACH iteration, never merging several iterations into one row. And always have an initialization row recording the values of the variables before entering the loop — missing this row is the most common cause of errors, because the initial value of MAX is what every later comparison depends on. The row K equals 4 is the whole point of this example. The array has the number 9 twice, at position 2 and position 4. The condition is written with a strict greater than, so when the second 9 is met, the comparison gives false and the variable POS is not updated. So the algorithm returns the position of the FIRST occurrence. If the condition is changed to greater than or equal to, the value of MAX is still exactly 9 but POS becomes 4, the LAST occurrence. This is a high-scoring question type: the paper gives an algorithm and asks "if the comparison sign is changed, how does the result change". A trace table gives a certain answer in thirty seconds, while reasoning in your head almost always goes wrong in exactly the case with duplicate values. Do not forget the final row, when K equals 6 and the loop condition becomes false. Writing that row shows you understand where the loop exits, and it is also where an off-by-one error shows up if the condition was written wrongly.

Worked example: Tracing an unknown algorithm to find out what it does

Given the following algorithm, where N is a positive integer:

R = 0 ; T = N ; loop while T greater than 0 ; R = R times 10 plus T mod 10 ; T = T div 10 ; end loop ; output R

Trace with N equal to 4,073 and say what the algorithm does. Then point out what problem it has with the input 1,200.

Solution

Trace table with N equal to 4,073. Three columns: T, T mod 10, R. Initialization row: T equals 4,073 and R equals 0.

Iteration 1: T mod 10 equals 3, so R equals 0 times 10 plus 3 equals 3; T equals 4,073 div 10 equals 407.
Iteration 2: T mod 10 equals 7, so R equals 3 times 10 plus 7 equals 37; T equals 40.
Iteration 3: T mod 10 equals 0, so R equals 37 times 10 plus 0 equals 370; T equals 4.
Iteration 4: T mod 10 equals 4, so R equals 370 times 10 plus 4 equals 3,704; T equals 0.
Condition check: T equals 0 so the loop stops. The output is 3,704.

What the algorithm does. The input 4,073 gives the output 3,704, that is, the digits are reversed in order. The mechanism is two operations working together: the mod 10 operation extracts the last digit, and the div 10 operation removes the last digit. Multiplying R by 10 before adding the new digit pushes the existing digits one place to the left, so the digit extracted last ends up in the highest position. The mod 10 and div 10 pair is the standard tool for every problem that processes digits one at a time, and recognizing it is the fastest way to answer the question "what does this algorithm do".

The problem with the input 1,200. A quick trace: R is successively 0, then 0, then 2, then 21. The output is 21, not 0021. The cause is that the result is held as a number, and a number has no concept of leading zero digits. Mathematically the result is not wrong, but if the requirement is to reverse a string of digits, then it is wrong.

The fix and the general lesson. If leading zeros must be kept, the input must be handled as a string of characters rather than as a number, and that string reversed. The lesson to state in the exam: choosing a data type is a design decision, not a technical detail — the same sequence of digits, represented as a number or as a string, gives two different behaviors, and the cases that expose the difference are boundary cases such as numbers ending in 0.

Worked example: Why splitting into subprograms changes even how you test

A 400-line grade management program is written in one continuous block in the main body. State the problems this causes and describe how decomposition into subprograms changes the situation.

Solution

Problem 1 — it cannot be tested part by part. With continuous code, the only way to test is to run the whole program and look at the final result. If the result is wrong, the error could be anywhere in the 400 lines. When it is split into subprograms such as read grades, compute average, assign grade and output report, each subprogram has a defined input and output so it can be tested separately: call the grading function with the boundary values 39, 40, 69, 70 and confirm that it returns correctly, completely independently of the rest.

Problem 2 — repeated code means repeated errors. In continuous code, the same block that validates grades is often copied in many places. When an error is found in that block, the fixer must remember to find every copy; missing one leaves the bug. With a single subprogram, there is only one place to fix.

Problem 3 — work cannot be divided. Four people cannot edit one 400-line continuous file without stepping on each other. When the program is decomposed and the subprograms communicate through parameters and return values, each person takes a part and only needs to agree on the interface between them.

Problem 4 — the implementation cannot be replaced. This is the deepest point and also the one that earns marks. When the grading algorithm is scattered through the main body, changing the grade scale forces rereading the whole program to find every relevant place. When it sits neatly in one subprogram, the caller only needs to know that it takes a score and returns a grade; the inside can be rewritten completely — change the scale, change the rounding, look up from a configuration file — without a single line at the call site needing to change. This is exactly abstraction applied to program structure.

Two concepts worth naming to earn marks. Good decomposition aims for high cohesion within each subprogram — it does exactly one clear thing — and low coupling between subprograms, meaning they communicate through parameters rather than through shared global variables. Coupling through global variables is the reason a change in one place causes errors in a place that seems unrelated.

Common mistake

Trap 1 — tracing in your head. Build the table with all columns and all rows, including the initialization row before the loop and the last row when the condition becomes false. This question type gives almost free marks if done with discipline.
Trap 2 — using a counted loop when the number of iterations is not known in advance. If the number of iterations depends on the data or on the user, you must use a condition-controlled loop. Choosing the wrong kind of loop is a design error, not a syntax error.
Trap 3 — only DEFINING abstraction and decomposition instead of POINTING OUT where they appear in the example. The question always asks how these concepts are applied to the specific situation in the paper, so the answer must stick to that situation.

Key takeaway

Lesson 1 wrap-up. Four things to take into the exam room. One, the four computational-thinking habits must be illustrated with the paper's own situation rather than defined in the abstract. Two, the four control structures, and the rule for choosing a loop type by whether the number of iterations is known in advance. Three, the discipline of building trace tables, and remember that a strict or non-strict comparison sign decides whether the algorithm returns the first or the last occurrence. Four, the four benefits of subprograms — avoiding repeated code, independent testing, dividing work, and allowing the implementation to be replaced — together with the two concepts of high cohesion and low coupling.

← Virtual Private Networks, Cloud Computing and Network Design · All IB Computer Science lessons · Practice IB Computer Science questions (free, instant scoring) · Standard Algorithms, Data Structures and Efficiency →