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). 









int _tmain(int argc, _TCHAR* argv[])
{
    bool postArray[150] ={false};
     int step =2;
    while(!false)
    {
        for(int i=step;i<=150;i+=step)
        {
            for(int b =3;b <= 150 ;b ++)
            {
                postArray[i] = postArray[b];


            step ++;
            cout << postArray[i] << " " << postArray[b];
        }




    }
    return 0;
}

Recommended Answers

All 2 Replies

The nested loop idea is fine, but many of the details are wrong.

First the while loop is not doing anything, so why have it.

Second, you are missing a closing brace for one of the loops.

Third, can you see boolean values known as true or false? I can't. So you have to come up with some way to visualize the outcome. Either check the value of an element and if true print out something, else print out something else OR change the type of the array to char or int and use t/f or 0/1 or some other dicotomy to represent the door being open or closed.

Fourth, you need some way to change the value from true to false or open to closed or t to f or 0 to 1, etc. The same mechanism can be used with any of the options I listed.

Fifth, your loop doesn't take into account starting at door 2. The outer loop controls what door you start at and what door you look at next. The inner loop controls which door you are actually looking at.

Sixth, when starting the any given cycle are you supposed to leave that door the way it is, or flip it?

Seventh, when possible it helps to create a test system that is smaller and easier to manipulate than the one you eventually want to use. Often trying to work out the test system using pencil and paper will more than reward you for the time it took when you do start writing your code.

There may be other details I haven't seen, too.

commented: Can give step wise guide,please +1

Can you guide ,with solution stepwise?

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.