6.1.1 – Decompose and abstract a problem

6.1.1 – Decompose and abstract a problem

Turn a requirement into relevant data, clear subproblems and a workable plan. Connect the jobs through explicit inputs and results, then check that the solution covers the requirement.

Choose the information the program needs

Decomposition breaks a programming problem into purposeful subproblems that can be solved and combined. Abstraction keeps the information needed for the required result and leaves out irrelevant detail. These decisions depend on the actual requirement.

A quiz organiser records scores, player names, favourite colours and recording times. The required program must display the mean of a nonempty list of whole-number scores and how many scores are at least a supplied target.

The score values are needed for both results, and the target decides which values count. Names, colours and times cannot change either required output, so they do not need to enter this calculation. If the requirement changes to name everyone who reached the target, names become relevant. Abstraction is not simply deleting as much data as possible.

State assumptions before designing: the score list is nonempty, scores and target are valid integers, and equality counts. There is no requirement to validate keyboard input or sort the scores. Adding those jobs would not solve a missing requirement.

Define jobs and the data between them

For the quiz requirement, a useful plan is:

  1. obtain the score list and target;
  2. total every score and count those meeting the target;
  3. divide the completed total by the number of scores;
  4. display the mean and count.

A subproblem should have a clear result. “Do the maths” is too vague; “total every score once” can be implemented and checked. Jobs need not be equal in size, and each need not become a separate function. One processing function can total and count in the same loop.

JobInputsResult needed by the next job
Analyse scoresnonempty score list, targetmean and target count
Display summarymean, target counttwo labelled output lines

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account