Hi everybody!!
I'm learning programming and trying to solve the next exercise:
There are 11 lockers in a school hallway numbered from 0 to 10, all of them closed. Follow the instructions in the following Pseudocode algorithm, then answer the question below.
Algorithm:
1. The number of lockers to move ("Step Size") is equal to 1.
2. Move to the first locker.
3. If you are at or past the end of the lockers, add 1 to step size, then go to step 2.
If step size is greater than 10, go to step 7.
4. Open or close the locker. For example: If the locker you're standing in front of is
closed, open it. If the locker is already open, close it.
5. Move "Step Size" lockers down (i.e, if step size is 1, move to the next locker. If it
is 2, move 2 lockers down, skipping 1, etc.).
6. Go to step 3.
7. End.
Which locker numbers are open?
The right answers are 1,4,9 ; but my answers are 1,4,9,10 . Any can explain me , what's my mistake?
Thanks a lot.
What I have tried:
subprocess fillinguparray(name,size)
for i<--0 to size with step 1 do
Write "CLOSE"
Read name[i]
endfor
EndSubProcess
subprocess showscreen(name,size)
For i<--0 to size with step 1 do
Print name[i]
endfor
EndSubProcess
Algorithm no_title
stepsize=1
Dimension lockers[11]
fillinguparray(lockers,10);
While stepsize<10 do
For i<--0 to 10 with step stepsize do
if lockers[i] = "CLOSE" then
lockers[i] = "OPEN" ;
if not
if lockers[i] = "OPEN" then
lockers[i] = "CLOSE" ;
endif
Endif
EndFor
stepsize=stepsize+1;
EndWhile
showscreen(lockers,10)
EndAlgorithm