Trace, diagnose and repair algorithms
Trace in execution order
Record values after the statement or iteration named in the question. Carry unchanged values forward. Do not jump directly to the final answer: a trace table records the route taken.
Use a counterexample to find the fault
A greatest-value algorithm initialised with zero appears to work for positive data. The smallest counterexample is a single negative value. Known: the answer must be a member of the data. Therefore initialise from the first item, then compare remaining items.
DECLARE Best, Value : INTEGER
Best ← -8
Value ← -2
IF Value > Best THEN
Best ← Value
ENDIF
OUTPUT Best
Distinguish error types
A syntax error breaks the language's structure; a runtime error occurs during execution, such as an out-of-bounds index; a logic error produces the wrong result despite running. After repairing a fault, test the counterexample and a normal case. Explain the cause, not just the changed line.
Best starts at 8. Process values 5, 7, 2, replacing Best only when a value is smaller. Give Best after each value.
The starter finds the largest of N values but wrongly outputs zero for all-negative data. Correct it. N is 1 to 5.
출력을 보려면 '실행'을 클릭하세요.
The program executes without crashing but skips the last array item because its loop ends at N - 1. Is this a syntax, runtime or logic error? Enter one word.
Explain your choice of data structure and loop. Identify one boundary case and predict its result. For a subroutine, explain what is returned or changed in the caller.
Saved for this sitting on this browser. Review this yourself or with your teacher; these explanations are not automatically marked.
Review your explanation against these criteria
- My types and data structure match the data and the question.
- I justified the loop and checked its first and last iterations.
- Initial values and rejected operations preserve the intended state.
- My test includes input, purpose and expected result.
- I distinguished returning a value from printing and changing caller state.