4.4.1.1–4.4.1.2 - Solving problems and tracing algorithms (C#)

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 A<BA<B, C≠1C\ne1, and all three slots distinct. There are 3!=63!=6 candidate arrangements, so checking all of them is feasible.

Order in slots 1, 2, 3Amir before Bea?Chen not first?Valid?
Amir, Bea, ChenYesYesYes
Amir, Chen, BeaYesYesYes
Bea, Amir, ChenNoYesNo
Bea, Chen, AmirNoYesNo
Chen, Amir, BeaYesNoNo
Chen, Bea, AmirNoNoNo

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 ←\leftarrow 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.

ConstructMeaningExample
SequenceExecute instructions in orderSet count, then visit the values
AssignmentReplace a variable's stored valuecount ← count + 1
SelectionChoose a path using a conditionIF values[i] >= limit THEN
IterationRepeat a blockFOR 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.

Pseudocode
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:

ivalues[i]Conditioncount after selection
04False0
19True1
27False1
39True2

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