A minimal reproducible example is the smallest code and data that still produce the wrong result, with the expected and actual output written beside it. You use it to find a fault yourself, to explain it in writing or to ask a teacher for help.
This lesson completes the problem-solving explanations module. It uses describing a correction and connecting a trace with output once the example is small enough to trace by hand.
How do you shrink a failing program?
- Write the symptom. Note the input, the expected output and the actual output.
- Replace input with fixed values. Remove
INPUTand assign known data instead. - Delete one unrelated part at a time. Remove the other calculations, messages and menus.
- Run again after each deletion. If the wrong output remains, the deletion was safe. If it changes or vanishes, restore the line.
- Reduce the data. Use the fewest values that still fail, with numbers that are easy to calculate.
- State expected and actual output with the one line you suspect.
Worked example
A longer program reads scores into an array, then finds the total, the average and the highest score. The total is wrong. The loop is:
Total ← 0
FOR Index ← 2 TO 5
Total ← Total + Scores[Index]
NEXT Index
Step 1, symptom. For five scores, the total is smaller than expected.
Step 2, fixed data. Remove the input and use three scores: Scores[1] ← 10, Scores[2] ← 20, Scores[3] ← 30.
Step 3, delete unrelated parts. The average and highest score do not touch Total, so remove them. Change the loop limit to 3 to match the data.
Minimal example:
Scores[1] ← 10
Scores[2] ← 20
Scores[3] ← 30
Total ← 0
FOR Index ← 2 TO 3
Total ← Total + Scores[Index]
NEXT Index
OUTPUT Total
Expected output: 60 (10 + 20 + 30). Actual output: 50 (20 + 30).
Why it points to the fault. The loop starts at 2, so Scores[1] is never added. The correction is to start the loop at 1: FOR Index ← 1 TO 3.
Check. The example still fails after every deletion, and it is short enough to trace in four steps.
The mistake to watch for
A frequent slip is to shrink too far or to change the problem:
Scores[1] ← 10 and Total ← 0 with no loop. Output: 0.
The loop that holds the fault has been deleted, so the example no longer shows the problem. Another slip is to submit the example without the expected output, so nobody can tell what “wrong” means.
The correction: after every deletion, rerun the example and check that the actual output is still wrong. Always write expected and actual output together.
Check yourself
1. For the Total fault above, which parts must stay in a minimal example: (a) the input of names, (b) the loop, (c) the highest-score calculation, (d) the array values?
Show answer
Keep (b) the loop and (d) the array values. The loop holds the fault and the values give a result that can be checked. The input of names and the highest-score calculation do not affect Total, so they can be removed.
2. Reduce the example to two scores, 10 and 20, with the same faulty loop FOR Index ← 2 TO 2. Does it still show the fault? State expected and actual.
Show answer
Yes. Expected total is 30 (10 + 20). The loop runs once, for Index = 2, so only Scores[2] = 20 is added. Actual total is 20. The fault remains, and the example is even easier to trace.
3. A student says a program that counts values above 10 gives 2 for the data 5, 10, 15 but should give 1. Write the minimal example’s expected and actual output, and name the likely fault.
Show answer
Expected: 1, because only 15 is above 10. Actual: 2, because the code counts 10 and 15. The likely fault is a comparison >= 10 where > 10 is needed. The data 5, 10, 15 is a good minimal set because 10 sits exactly on the boundary.
Where this leads next
Now bring the five skills together in the problem-solving explanations practice set. To keep track of repeated errors, the mistake log and retest queue helps you note the fault type and set a retest.
If you want someone to shrink a stubborn program with you and talk through your reasoning, our teachers can do that in online one-to-one Computer Science tuition.