Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 1-May-20 0:57am
v4
Comments
CHill60 30-Apr-20 11:48am    
You've posted the algorithm but not the question. We also need your working in English please
Patrice T 30-Apr-20 16:59pm    
From the question: "The right answers are 1,4,9 ; but my answers are 1,4,9,10 . Any can explain me , what's my mistake?"
CHill60 1-May-20 4:24am    
That is the OP's question - the point of the exercise is not clear. Are those the lockers that should be open, closed, not touched - what?

Quote:
The right answers are 1,4,9 ; but my answers are 1,4,9,10 . Any can explain me , what's my mistake?

While stepsize<10 do

Why last stepsize is 9, try to include 10!
While stepsize<=10 do

or
While stepsize<11 do
 
Share this answer
 
Comments
CHill60 1-May-20 4:30am    
OP is trying to contact you via a solution
Patrice T 1-May-20 6:50am    
Thank you for info, but solution removed.
CHill60 1-May-20 6:54am    
:sigh: Hopefully they saw my reply to their solution and will come back with the comments
Member 14818346 1-May-20 8:12am    
Thank you guys. Sorry, It was my first post; I made a lot of mistakes and I had troubles trying edit the message, I don't know it was any problem in the website or my internet connection.
But, yes the problem was solved, Thanks Patrice and everybody who answered.
Thanks a lot!
Patrice T 1-May-20 8:30am    
if problem is solved, you can accept useful answers.
secondary effect, it will close the question.
The basics of the algorithm are quite simple. You need two loops:
// The outer loop counts from 1 to the highest step value:
for (int stepvalue = 1; stepvalue < maximum; stepvalue++)
{
    // the inner loop counts from zero (the first locker location)
    // to the index value of the last locker.
    // But this loop increments itself by the stepvalue, so on the first outer loop
    // it will examine every locker, on the next outer loop every other, etc.
    for (int offset = 0; offset < maxoffset; offset += stepvalue)
    {
        // here swap the open/closed status of locker[offset]
        locker[offset] = !locker[offset]; // 0 becomes 1, and 1 becomes 0
    }
}
 
Share this answer
 
Comments
Patrice T 1-May-20 7:02am    
It is basically what OP have done.
Richard MacCutchan 1-May-20 7:11am    
But he says he gets the wrong answer. When i tried it it worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900