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:
- 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 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
- If there is not at least one upperc ase character, one lower case 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 variableA quantity that can change or that may take on different values. Variable also refers to a letter or symbol representing such a quantity.. These values will change as the program is run:
Variable | Data type |
error | Boolean |
lower case | Integer |
upper case | Integer |
number | Integer |
password | String |
position | Integer |
Variable | error |
---|---|
Data type | Boolean |
Variable | lower case |
---|---|
Data type | Integer |
Variable | upper case |
---|---|
Data type | Integer |
Variable | number |
---|---|
Data type | Integer |
Variable | password |
---|---|
Data type | String |
Variable | position |
---|---|
Data type | Integer |
No constants are required.