±«Óătv

Decomposing the problem – example two

The following example has been written by ±«Óătv Bitesize consultants as a suggestion of the type of problem that may appear in an exam paper.

The problem

A program that validates a password is required. The password must be at least five characters long and include at least one uppercase (U/C) character, one lowercase (L/C) character and a number.

Decompose the problem

The first step is to break down (decompose) the overall problem into several smaller, more easily solved problems:

  1. Input the password.
  2. Check that the password is at least five characters in length.
  3. If the password is not at least five characters, an error has occurred.
  4. If the password is at least five characters, then inspect each character in the password:
    • If the character is an uppercase character, add one to the uppercase character total.
    • If the character is a lowercase character, add one to the lowercase character total.
    • If the character is a number, add one to the number total.
  5. If there is not at least one uppercase character, one lowercase character and one number, an error has occurred.
  6. If an error has occurred, loop back to the beginning.
  7. Otherwise, accept the password.

Variables and constants

From the above, the solution will require the following variables. These values will change as the program is run.

VariableData type
errorBoolean
lowercaseInteger
uppercaseInteger
numberInteger
passwordString
positionInteger
Variableerror
Data typeBoolean
Variablelowercase
Data typeInteger
Variableuppercase
Data typeInteger
Variablenumber
Data typeInteger
Variablepassword
Data typeString
Variableposition
Data typeInteger

No constants are required.