4.4.1.1–4.4.1.2 - Solving problems and tracing algorithms (VB.NET)
Develop a solution from explicit rules, express it as pseudocode, trace its state and convert it into VB.NET. Check why it works and what evidence could reveal a defect. VB.NET activities are written-only; this route is incomplete: runnable support pending.
Problems and algorithms
A precise problem states the available inputs, the required result and the rules that a result must satisfy. Solving it means finding a result or method that satisfies all those rules, rather than making a plausible guess.
Algorithm
A sequence of steps that can be followed to complete a task and that always terminates.
Instructions must be unambiguous enough to follow. “Keep trying until it works” is not sufficient if success is undefined or the process can continue forever. An algorithm can be expressed in words, pseudocode or a diagram; a program implements it in a particular language. A computer program that waits indefinitely is not a terminating solution to a task that requires an answer.
Consider placing red, blue and green deliveries in slots 1, 2 and 3, once each. Red must be earlier than blue; green cannot be first; blue cannot be last. There are only six possible orders, so exhaustive checking is feasible. Blue cannot be first because red must precede it, and cannot be last, so blue is second. Red must therefore be first and green third. Checking the result against every original rule confirms it, rather than checking only the rule used most recently.
For a larger problem, systematically eliminate impossible choices or decompose the work. Keep the original constraints visible: a solution that satisfies two of three rules is still incorrect. Exhaustive search terminates when the finite candidate set is exhausted, although it may become impractical as that set grows.
Pseudocode and state
Pseudocode describes the logic without requiring all the declarations and punctuation of a programming language. State its conventions so that the algorithm has one clear meaning. Here arrays use zero-based indices, the assignment arrow stores the right-hand value in the left-hand variable, and FOR i ← 0 TO n − 1 visits the endpoints inclusively when there are elements; it has no iterations when n is zero.
Sequence performs statements in order. Assignment changes stored state. Selection chooses statements according to a condition. Iteration repeats them. This algorithm returns the sum of array values strictly above a limit:
total ← 0
FOR i ← 0 TO LENGTH(values) − 1
IF values[i] > limit THEN
total ← total + values[i]
ENDIF
ENDFOR
RETURN total
With values = [3, 8, 5, 10] and limit = 5, a hand trace records each actual update. The value 5 is excluded because the comparison is strict.
| i | values[i] | values[i] > 5 | total after this iteration |
|---|---|---|---|
| 0 | 3 | false | 0 |
| 1 | 8 | true | 8 |
| 2 | 5 | false | 8 |
| 3 | 10 | true | 18 |
The returned value is 18. Do not reset total inside the loop, add a value on the false branch, or invent an extra visit to index 4. A trace follows the written algorithm even if it contains a bug; silently tracing the intended solution conceals the defect.
Converting to VB.NET
Converting an algorithm preserves its input, state changes, decisions and result while expressing them in the language's syntax. VB.NET functions declare a return type, arrays use parentheses for indexing, If uses Then and End If, and a For Each loop can visit each element without managing indices.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account