Hi,i used this example for practicing,supposed to be easy one,but,abit confusing.
Can you help to solve and point to mistakes i've done.
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).
Can someone guide with the step/by step,please
aluhnev 0 Junior Poster in Training
Recommended Answers
Jump to PostEach mailbox is either closed or open, so you could represent the mailboxes by an array of ints, which are assigned the value 0 or 1, to represent closed or open. Or an array of booleans. Start by assigning all of them to one value (say, 1) to represent closed. …
Jump to PostI'm sure we all appreciate your effort. At least I do.
You made a mistake, I made very often in the beginning!for(int j=2;j<=150;j++)
should befor(int j=2;j<150;j++)
Why?
You defined an array with 150 booleans.
You can access each bolean with an index from 0 to 149.
So the moment …
Jump to Post1) No reason to use the key word static, that I can see anyway.
2) Some compiler implementations might default elements in arrays of type int to a value of zero, but I wouldn't rely on it if you are writing code in C++.
3) int arr[150] will contain 150 …
All 10 Replies
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster
aluhnev 0 Junior Poster in Training
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster
DavidB 44 Junior Poster
aluhnev 0 Junior Poster in Training
ddanbe 2,724 Professional Procrastinator Featured Poster
TylerD75 0 DaniWeb Newbie
aluhnev 0 Junior Poster in Training
TylerD75 0 DaniWeb Newbie
Lerner 582 Nearly a Posting Maven
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.