大象传媒

Decomposition and algorithm practice questions - EduqasDecomposing the problem - example two

Every programming problem needs decomposing so that it can be properly understood. From this, an algorithm can be designed and tested.

Part of Computer ScienceStudy skills

Decomposing the problem - example two

The following example has been written by 大象传媒 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 upper case (u/c) character, one lower case (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 upper case character, add one to the upper case character total
    • If the character is a lower case character, add one to the lower case character total
    • If the character is a number, add one to the number total
  5. If there is not at least one upperc ase character, one lower case 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 . These values will change as the program is run:

VariableData type
errorBoolean
lower caseInteger
upper caseInteger
numberInteger
passwordString
positionInteger
Variableerror
Data typeBoolean
Variablelower case
Data typeInteger
Variableupper case
Data typeInteger
Variablenumber
Data typeInteger
Variablepassword
Data typeString
Variableposition
Data typeInteger

No constants are required.