Decomposition and algorithm practice questions - EduqasDesigning an algorithm - example two

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

Part ofComputer ScienceStudy skills

Designing an algorithm - example two

Algorithm design option one - flow diagram

Flow diagram showing the process required for a password to be accepted

Algorithm design option two - pseudocode

{set error flag to True to enable loop}
Set error = True {iterate until a valid password is entered}
while error == True {set all variables to default values } set error = False set lower case = 0 set upper case = 0 set number = 0 {input the password} Output "Type in a password of at least 5 characters including 1 l/c letters, 1 u/c, 1 number: " input password {check password length - error if not at least 5 characters} if len(password)< 5 then error = True end if {if the password is the right length then} {inspect each character in the password} {add 1 to the number of upper case for each upper case character} {add 1 to the number of lower case for each lower case character} {add 1 to the number of numbers for each number} if error = False then for position (0 to len(password)) {loop between 0 and length of password} if password[position] >="A" AND password[position] < ="Z" then upper case = upper case + 1 else if password[position] >="a" AND password[position]< ="z" then lower case = lower case 1 else if password[position] >="0" AND password[position]< ="9" then number = number + 1 next position end if {if character requirements are not met, an error has occured} if upper case < 1 OR lower case < 1 OR number < 1 then error = True output "Password not accepted"
Else
{output acceptance}
output "Password accepted: "< password end if
END

Output table

With the inputs stated below the outputs would be:

Example:

Type in a password of at least 5 characters including 1 l/c letters, 1 u/c, 1 number: Pass1234

Password accepted: Pass1234

Testing table

Tests will be required to test that the final program runs correctly.

Test no.DescriptionTest dataTest typeExpected outcome
1Enter a valid passwordAbcd36ValidPassword should be accepted
2Enter a ten-character passwordABcd457AnzValidPassword should be accepted
3Enter a five-character passwordAbc67ExtremePassword should be accepted
4Password has at least one lower case characterABCd57ExtremePassword should be accepted
5Password has at least one numberABcd36ExtremePassword should be accepted
6Password has at least one upper case characterAbcd45ExtremePassword should be accepted
7Password is too shortAbc5InvalidPassword must be re-entered
8Password does not contain at least one upper case characterabcd5InvalidPassword must be re-entered
9Password does not contain at least one lower case characterABCD8InvalidPassword must be re-entered
10Password does not contain at least one numberABcdeInvalidPassword must be re-entered
Test no.1
DescriptionEnter a valid password
Test dataAbcd36
Test typeValid
Expected outcomePassword should be accepted
Test no.2
DescriptionEnter a ten-character password
Test dataABcd457Anz
Test typeValid
Expected outcomePassword should be accepted
Test no.3
DescriptionEnter a five-character password
Test dataAbc67
Test typeExtreme
Expected outcomePassword should be accepted
Test no.4
DescriptionPassword has at least one lower case character
Test dataABCd57
Test typeExtreme
Expected outcomePassword should be accepted
Test no.5
DescriptionPassword has at least one number
Test dataABcd36
Test typeExtreme
Expected outcomePassword should be accepted
Test no.6
DescriptionPassword has at least one upper case character
Test dataAbcd45
Test typeExtreme
Expected outcomePassword should be accepted
Test no.7
DescriptionPassword is too short
Test dataAbc5
Test typeInvalid
Expected outcomePassword must be re-entered
Test no.8
DescriptionPassword does not contain at least one upper case character
Test dataabcd5
Test typeInvalid
Expected outcomePassword must be re-entered
Test no.9
DescriptionPassword does not contain at least one lower case character
Test dataABCD8
Test typeInvalid
Expected outcomePassword must be re-entered
Test no.10
DescriptionPassword does not contain at least one number
Test dataABcde
Test typeInvalid
Expected outcomePassword must be re-entered

The problem has now been fully decomposed and an has been designed.