Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey i can't figure out how to do this program, would any of ye could give a hand?

"Postman Pat became bored one night at the postal sorting office and to break the monotony of the nightshift, he carried out the following experiment with a row of mailboxes (all initially closed) in the post office. These mailboxes are numbered 1 through to 150, and beginning with mailbox 2, he opened the doors of all the even-numbered mailboxes. Next, beginning with mailbox 3, he went to every third mailbox, opening its door if it was closed and closing it if it was open. Then he repeated this procedure with every fourth door, then every fifth door, and so on. When he finished, he was surprised at the distribution of closed mailboxes. Write a program to determine and display which mailboxes these were (i.e. which doors were closed at the end of the process)."
Posted
Comments
Kornfeld Eliyahu Peter 18-Oct-15 9:18am    
It's about math - the amount of numbers divide the numbers in the list (2-150)...
2 is only 2
3 is only 3
4 is 2 and 4
5 is only 5
6 is 2 and 3 and 6
and so on...
What you need (without any pre-thinking) is a loop inside a loop...
Maciej Los 18-Oct-15 9:24am    
Not a question at all! This is a code request!
PIEBALDconsult 18-Oct-15 11:03am    
Similar to a Sieve of Eratosthenes.
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Except it finds square numbers rather than prime numbers.
A good exercise; you should enjoy it.
[no name] 18-Oct-15 13:01pm    
A 5 even it does not count.

Start by thinking how you would do it manually: and then emulate the process.
Create an array to hold the mailboxes, and assign each a value that says "closed".
Then loop through toggling the mailboxes from open to closed appropriately.
As you have been told, all you need is two nested loops...

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
Patrice T 18-Oct-15 10:54am    
+5
PIEBALDconsult 18-Oct-15 12:32pm    
It's more efficient to use three loops. :D
OriginalGriff 18-Oct-15 12:45pm    
It is - but I prefer generic solutions! :laugh:
PIEBALDconsult 18-Oct-15 12:48pm    
I used straight C -- no generics.
We do not do your homework: it is set to make you think about what you have learned at school.

Hint:
Take a sheet of paper and do by hand what is said with only 20 mailboxes (it is only for testing).
Your program will have to do the same.
When the even boxes are done, display the boxes status to see if it match your paper sheet. When the third boxes are done, display the boxes status to see if it match your paper sheet. and so on.

If you have problems in your program, use the debugger to see your code execute step by step, inspect variables until you understand where it goes wrong and why.

If you are stuck, Tell your teacher it will help him to spot your weakness and correct it.
 
Share this answer
 

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