MedVision ad

SDD Questions [Coping with the HSC] (1 Viewer)

huynguyencab

Member
Joined
Nov 11, 2011
Messages
37
Gender
Male
HSC
2012
Hi,

I've been studying SDD to fight with the HSC and I have lots of questions that I want to ask. So for those who are doing HSC, if you have any questions please post here, AND for those who did SDD before, please help us :)

MY question:

A valid password consists of a letter followed by four digits. F1837 is an example of a valid password.

The following algorithm was designed for checking the validity of a password. The algorithm has logic errors.


Code:
1 BEGIN password_check
 
2 INPUT password
 
3 IF the length of password = 5 THEN
 
4  valid = true
 
5 ENDIF
 
6 IF character 1 of password is a letter THEN
 
7  valid = true
 
8  FOR n = 2 TO 5
 
9  IF character n of password is a digit THEN
 
10  valid = true
 
11  ELSE
 
12  valid = false
 
13  ENDIF
 
14 NEXT
 
15 ENDIF
 
16 PRINT ‘PASSWORD IS VALID?’, valid. 
 
17 END
Use the password H43k21 to desk check the algorithm

I tried to desk check the algorithm but when it comes to n = 4 and n = 5 the value of valid should be false then true. However, the answer given in BOS is different. Please tell me what did I do wrong :)
 

Gigacube

Active Member
Joined
Apr 19, 2010
Messages
1,333
Location
Australia
Gender
Female
HSC
2012
The password, H43k21 is longer than 5 characters so you know straight away that it is an invalid password and should return false regardless of whether the letter and digits are in the right order. To overcome this error, you should insert a line of code which checks whether valid is true and if it is it lets the algorithm perform lines 6-15. If it is false it will print that the password is invalid and skip unnecessary processing.

Another reason why the algorithm is invalid is because during lines 9-15, if the password contains a digit then a letter then a digit (e.g. 5a3) it will print that the password is valid. This is because 5 is True, a is False, 3 is True. As you can see valid will end up returning True even though there is an invalid character.

What year is this paper from?
 

huynguyencab

Member
Joined
Nov 11, 2011
Messages
37
Gender
Male
HSC
2012
The password, H43k21 is longer than 5 characters so you know straight away that it is an invalid password and should return false regardless of whether the letter and digits are in the right order. To overcome this error, you should insert a line of code which checks whether valid is true and if it is it lets the algorithm perform lines 6-15. If it is false it will print that the password is invalid and skip unnecessary processing.

Another reason why the algorithm is invalid is because during lines 9-15, if the password contains a digit then a letter then a digit (e.g. 5a3) it will print that the password is valid. This is because 5 is True, a is False, 3 is True. As you can see valid will end up returning True even though there is an invalid character.

What year is this paper from?
Thanks GG, it comes from 2011 past paper (Question 26), I still don't get why when n = 4, the character is k which is False (k is not a digit); and when n = 4, the character is 2 which is True but the answer is opposite. I think I miss interpreting the algorithm :(
 

Gigacube

Active Member
Joined
Apr 19, 2010
Messages
1,333
Location
Australia
Gender
Female
HSC
2012
Thanks GG, it comes from 2011 past paper (Question 26), I still don't get why when n = 4, the character is k which is False (k is not a digit); and when n = 4, the character is 2 which is True but the answer is opposite. I think I miss interpreting the algorithm :(
Sorry for the late reply huynguyencab, I don't quite understand what you're trying to say. I think it might make it clearer if you upload your desk checking to help us identify where you went wrong.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top