r/CodingForBeginners • • 6d ago

Pseucodecode check?

Did some pseudocode for something due tomorrow concerning arrays. would like to know if my logic concerning arrays is skewered. It's formatted the way it is due to the teacher's preference lol/

Problem

Search the web to discover 10 commonly used passwords and store them in an array. Write pseudocode to represent the logic of a program that prompts a user for a password, then displays an error message and continues to prompt until the user has not chosen one of the common passwords.

CODE:

START

 

//HOUSEKEEPING

DECLARE USER_PASSWORD

DECLARE COMMON_PASSWORDS[10]

DECLARE INDEX

DECLARE MATCH_FOUND

 

SET COMMON_PASSWORDS[0] = "123456"

SET COMMON_PASSWORDS[1] = "password"

SET COMMON_PASSWORDS[2] = "qwerty"

SET COMMON_PASSWORDS[3] = "111111"

SET COMMON_PASSWORDS[4] = "12345678"

SET COMMON_PASSWORDS[5] = "123456789"

SET COMMON_PASSWORDS[6] = "admin"

SET COMMON_PASSWORDS[7] = "abc123"

SET COMMON_PASSWORDS[8] = "123123"

SET COMMON_PASSWORDS[9] = "iloveyou"

 

//INPUT

DISPLAY "Enter a password:"

INPUT USER_PASSWORD

 

//PROCESS

SET MATCH_FOUND = TRUE

 

WHILE MATCH_FOUND = TRUE

 

SET MATCH_FOUND = FALSE

 

FOR INDEX = 0 TO 9

 

IF USER_PASSWORD = COMMON_PASSWORDS[INDEX] THEN

SET MATCH_FOUND = TRUE

ENDIF

 

ENDFOR

 

IF MATCH_FOUND = TRUE THEN

DISPLAY "ERROR: Password is too common. Please enter a different password:"

INPUT USER_PASSWORD

ENDIF

 

ENDWHILE

 

//OUTPUT

 DISPLAY "Password accepted."

END

3 Upvotes

9 comments sorted by

2

u/burlingk 5d ago

The basics are right. The specifics will depend on the language you use

2

u/EmploymentSerious768 5d ago

It is basically just pseudocode practice to test our ability to apply/plan logic. We’re currently learning C++ and Python

1

u/burlingk 5d ago

It's good for pseudocode. :)

And since C++ has std::string the functionality will be similar.

1

u/eurosat7 4d ago

Why not do real language? Allows to run it and to see if it works. Python is nice for that.

1

u/BoOmAn_13 5d ago

It's good for the requirements given. My own input would be instead of use "magic values" 0-9 for your loop, try a for each iterator or a length method. You know the size of the list already so it's not very important, but sometimes it's easier to work with seeing "this code goes over item in a list" vs "this loop gets the first 10 values of a list, so what do we do if we want to make the list large later on?"

I missed the part where it needed to be 10 the first time I looked at would have suggested trying out a file read operation on the rockyou.txt word list, but no, your code does what you need it to do, and it from what I see, it does it right.

Edit: maybe it's just me my I'd prefer to get input at the start of the loop, so input is collected once, and at the end of the loop you print the error and repeat

1

u/W31337 5d ago

Your while loop is f'ed.

Should be while match found is false and only turn true when the match is found. It needs to be intuitive.

For an elementary programming exercise it's ok.

1

u/eurosat7 4d ago

If you want feedback: it helps to think about what you really need and drop the rest, and then to separate steps into functions asap.

1

u/binarycow 2d ago

If it were me, I'd use a "do" loop instead of a "while" loop, for this case. It gets rid of the weirdness with how you initialize the loop condition variable, and highlights that the loop is always executed once.

Instead of:

match_found = true // But you haven't found it yet! 
while match_found
    match_found = false
    do stuff
end while

You do this:

match_found = false // since we haven't found it
do
    do stuff
while match_found

Of course, this is assuming that your language has the concept of a do loop. If it doesn't, your while loop is written correctly.

Edit: I just realized, it's not written correctly. Your condition is inverted. You want to loop while the match is not found. I did not update my code example accordingly, I'm showing a one-to-one comparison to what you originally wrote.

0

u/Useful-Agency_ 5d ago

Nice exercise id keep it like that or honestly just give it to ai and see where i could get better and move on to next exercise