Scenario-Based Problem Solving · Lesson 2 of 3
Scenario Algorithms
A scenario algorithm should be complete: initialise values, obtain data, validate where required, process all cases, output the requested information and terminate. Use subprograms to separate coherent tasks and arrays when related values must be retained, and add comments to explain the code, as the scenario question asks.
By the end, you can
- Write precise pseudocode.
- Use variables, selection and iteration appropriately.
- Use arrays where appropriate.
- Use procedures and functions where appropriate.
- Trace and test the proposed solution.
Pseudocode solutions
Writing the solution as pseudocode means being precise enough that someone else could implement it without asking a question.
Follow the conventions consistently: keywords in capitals, Pascal-case identifiers, the arrow for assignment, every block closed with ENDIF, ENDWHILE, NEXT or UNTIL, and indentation showing what belongs to what. Define any procedures and functions at the start, declare the variables and arrays with their types, name them for what they hold, and add // comments that say what each part does.
Structure the solution in the order the work happens: declarations, then initialisation, then input with validation, then processing, then output. Initialising totals, counters and flags before the loop that uses them is the step most often forgotten, and it is the one that makes the answer wrong on the second run.
Use an array wherever the same operation applies to many values, and use procedures and functions once the solution is more than about twenty lines — one for validation, one for the calculation, one for the report. A function that returns a value can be tested on its own, and the main program then reads as a list of the steps.
Trace the finished pseudocode before presenting it. Use a small dataset where you know the answer, and include an edge case: an empty list, a single item, a value exactly on the boundary. A trace table over three or four items catches almost every error that a reading of the code would not.
Worth remembering
- Declarations, initialisation, validated input, processing, output.
- Arrays for repeated values; procedures and functions once it grows.
- Trace it with a small dataset including an edge case before presenting it.
Worked example
A mark-processing algorithm can separate validation, statistics and report formatting into distinct steps or subprograms.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
If marks must be revisited, searched, sorted or reported later, the individual values need to be retained.
Check your method, units and explanation against it.
Marked practice
Set 1 of 4 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.