Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A door lock asks for a password until the right one, Open, is typed. It then outputs how many tries were needed.
pass, OPEN and Open. How many tries are output?Flowchart symbols: terminator (START, STOP), process (rectangle), input/output (parallelogram), decision (diamond, two exits Yes and No), subroutine (rectangle with a line inside each end), flow line (arrow). A flow line back to an earlier shape makes a loop. Conditions are written precisely, such as Count > 10.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Step 1. The flowchart.
Tries ← 0.INPUT Password.Tries ← Tries + 1.Password = "Open"? No: flow line back to step 3. Yes: go on.OUTPUT Tries.Step 2. The inputs. pass is try , not equal. OPEN is try , not equal, because the capital letters differ. Open is try , equal. The output is .
Step 3. Pseudocode. The test comes after the input, so a REPEAT loop fits.
Tries ← 0
REPEAT
INPUT Password
Tries ← Tries + 1
UNTIL Password = "Open"
OUTPUT "Tries: ", TriesAnswer: (b) tries.
Check. The loop body always runs at least once, which is right: the user must type at least one password.
Log in to join the discussion.
Loading…