Computer science · 02 / 06

Algorithms: inputs, steps, output

High school foundations · Worked example and 4 exercises with solutions, at your pace.

The principle, simply

An algorithm specifies operations for a family of problems. Separate inputs, processing and output. A condition chooses a branch; a loop repeats operations. Trace variables on a small example and then test a boundary case. One successful example does not establish correctness everywhere.

THE PICTURE TO REMEMBERAlgorithms: inputs, steps, output
  1. 01Inputs
  2. 02Initialisation
  3. 03Steps and conditions
  4. 04Output and tests

WORKED EXAMPLE

From principle to reasoning

To sum values, start at total = 0 and add each item. A trace table records each value and the updated total.

Solve a problem in this subject

  1. Specify the contract

    Define inputs, types, outputs and forbidden cases. State whether a search algorithm requires sorted data.

  2. Trace a small example

    Tabulate variables, tested conditions and actions using a small hand-checkable example.

  3. Test boundaries

    Consider empty inputs, one item, duplicates, missing values and numeric limits where relevant. Define the output or explain rejection.

  4. Justify

    Explain termination and correctness. Distinguish operation counts, measured time and memory use.

YOUR TURN

01 · Try before checking

Why not initialise total to the first value and then loop over the entire list?

One hint

Check whether the first item is counted twice.

Reveal and understand the solution

It is counted once in initialisation and again in the loop. Match initialisation with loop bounds.

Compare method, conditions and answer; identify the exact part to revisit.

IMPROVE THROUGH PRACTICE

Three additional challenges with solutions

Start with the first challenge. Write your method before opening a hint; use the solution to find the first step that needs work.

Reflect after practising
02 · Recognise and understandLink to this exercise

Trace s = 0; for x in [2,5,1], s = s + x. Find final s.

A hint to get started

Record the accumulator after each iteration.

Step-by-step solution
  1. Successive values are 2, 7 and 8.
  2. Final s is 8.
  3. Zero is the additive identity.

Which step did you miss? Retry without help, then explain why the method works.

03 · Apply the methodLink to this exercise

Why does initialising a maximum to 0 fail for [−5,−2,−8]?

A hint to get started

No element exceeds zero.

Step-by-step solution
  1. The algorithm would retain 0, which is not in the list.
  2. The actual maximum is −2.
  3. Start with the first element of a nonempty list and handle an empty list separately.

Which step did you miss? Retry without help, then explain why the method works.

04 · Go furtherLink to this exercise

How should a mean algorithm be tested?

A hint to get started

Include boundary cases and hand-computable results.

Step-by-step solution
  1. Test a short list, one value and negative values.
  2. Define behaviour for an empty list, whose mean is undefined.
  3. Compare with independent arithmetic, not merely absence of errors.

Which step did you miss? Retry without help, then explain why the method works.

The trap to avoid

Test empty lists, boundary values and termination.

Without notes, can you…

  • Explain the principle in your own words?
  • Rebuild the diagram and its connections?
  • Solve the exercise and justify the method?

Choosing a state saves it in this browser. This is your assessment, not a grade.