4.4.1.1–4.4.1.2 - Solving problems and tracing algorithms (C#)
Develop a precise solution, express it in pseudocode, translate it into C# and check the reasoning behind its results. This route uses written C# tasks and is incomplete: runnable support pending; Paper 1 itself is an onscreen practical examination.
Problems and algorithms
A solution starts with a clear statement of the inputs, the required result and the conditions that must hold.
Algorithm
An algorithm is a sequence of steps that can be followed to complete a task and that always terminates.
“Find a good arrangement” is too vague to be an algorithm. “Try each permitted arrangement, reject those that break a rule, and return the remaining arrangements” gives a method, provided the possibilities are finite and each test is precise. An algorithm is an abstract procedure; a program implements that procedure in a language and environment.
Consider three students, Amir, Bea and Chen, assigned distinct presentation slots 1, 2 and 3. Amir must present before Bea; Chen cannot use slot 1. Record the constraints as , , and all three slots distinct. There are candidate arrangements, so checking all of them is feasible.
| Order in slots 1, 2, 3 | Amir before Bea? | Chen not first? | Valid? |
|---|---|---|---|
| Amir, Bea, Chen | Yes | Yes | Yes |
| Amir, Chen, Bea | Yes | Yes | Yes |
| Bea, Amir, Chen | No | Yes | No |
| Bea, Chen, Amir | No | Yes | No |
| Chen, Amir, Bea | Yes | No | No |
| Chen, Bea, Amir | No | No | No |
The table checks both soundness—every reported solution satisfies every constraint—and completeness—every possible valid arrangement has been considered. Finding one plausible arrangement does not establish uniqueness. With many students, enumerating every permutation would be expensive; the size of the search space matters.
A loop that repeatedly tries random arrangements “until one works” might never finish, particularly if the constraints are impossible. A finite systematic search can stop after all candidates have been tested and report “no solution”. Its termination is part of the design.
Pseudocode and tracing
Pseudocode expresses an algorithm without tying its meaning to C# syntax, and a trace follows its state one instruction at a time.
The convention here uses for assignment, = for comparison, zero-based array positions and inclusive FOR bounds. Use the conventions supplied with a task; do not import GCSE or another board's exact tokens as a 7517 requirement.
| Construct | Meaning | Example |
|---|---|---|
| Sequence | Execute instructions in order | Set count, then visit the values |
| Assignment | Replace a variable's stored value | count ← count + 1 |
| Selection | Choose a path using a condition | IF values[i] >= limit THEN |
| Iteration | Repeat a block | FOR i ← 0 TO LENGTH(values) - 1 |
This algorithm counts measurements at or above a threshold. When the array is empty, the FOR body has zero iterations.
count ← 0
FOR i ← 0 TO LENGTH(values) - 1
IF values[i] >= limit THEN
count ← count + 1
ENDIF
ENDFOR
OUTPUT count
For values = [4, 9, 7, 9] and limit = 8, trace after each array value is processed:
| i | values[i] | Condition | count after selection |
|---|---|---|---|
| 0 | 4 | False | 0 |
| 1 | 9 | True | 1 |
| 2 | 7 | False | 1 |
| 3 | 9 | True | 2 |
The output is 2. The initial count = 0 is a separate state before the loop; it is not another processed value. A trace table records the moments requested by the task. Updating a variable on paper before evaluating the condition can silently change the algorithm.
The equivalent C# implementation uses < values.Length because C# for loops test their continuation condition rather than declaring an inclusive final bound.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account