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 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:
- Input the password.
- Check that the password is at least five characters in length.
- If the password is not at least five characters, an error has occurred.
- 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.
- If there is not at least one uppercase character, one lowercase character and one number, an error has occurred.
- If an error has occurred, loop back to the beginning.
- 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.
Variable | Data type |
error | Boolean |
lowercase | Integer |
uppercase | Integer |
number | Integer |
password | String |
position | Integer |
Variable | error |
---|---|
Data type | Boolean |
Variable | lowercase |
---|---|
Data type | Integer |
Variable | uppercase |
---|---|
Data type | Integer |
Variable | number |
---|---|
Data type | Integer |
Variable | password |
---|---|
Data type | String |
Variable | position |
---|---|
Data type | Integer |
No constants are required.