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

Recommended Answers

All 10 Replies

Until you post your code I can only say that we don't do your homework for you. You have the solution algorithm in the pseudo-code. Just apply the language to the solution.

can someone give steps,i know need nested for loop,but how to do comparesment?

Same answer. It is your problem to figure out. Describe first here what you think you should do to solve your conundrum. Describe how you might do it, step by step. Plain language is OK - code can come later.

Each 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. Then go through the loops, and check if closed or open:

if (1) reset value to 0
else reset value to 1

Then just go through all the loops like that. Post your code as you go so we can offer suggestions.

 ///I got this so far ,got general idea,but can't combine all together.

 bool box[150] = {1};

         for(int j=2;j<=150;j++)
              for(int i=j;i<=150;i+=j)

                    bool box = 0;

                     bool box = 1;

                 cout <<?????


     return 0;


I'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 be for(int j=2;j<150;j++)
You defined an array with 150 booleans.
You can access each bolean with an index from 0 to 149.
So the moment j becomes 150, you will get an index value out of range error!
There are other issues, but I leave you to think about this.

  1. What are you trying to test with if(1)?
    An if could test two values (ex.) to see if they are equal (or not)...
    So if(1) won't do much for you (it will always be true).
    But something like if (box[i]) will test your box if it contains true or false. (You could also use if (box[i] == true) or if (!box[i]) // to test for false...).

  2. You also need a bit of math to simplify your program.
    How would you check every other box? (i.e. box 2,4,6,8,10...)
    Or every third box? (box 3,6,9...)
    I would recommend checking out the modulus operator (%) in c++.

  3. I'm not sure what default values are set on each bool array position (would think it is either null or 0). But a way to ensure all array positions are being set to a default value, you could use: std::fill_n(box, 150, false);
    I believe bool box[150] = {0}; only sets the first array position [0] to false;

Hope this enough to get you started?

How about this logic is  it ok?

static int mBox[150]; //It will initialize whole array with value zero
int a=0,b=0,i,j=0,arr[150];
for(int j=0;j<150;j++)

    cout<<"\nNo. of open MailBoxes : "<<b;
    cout<<"\nNo. of closed MailBoxes : "<<a;
    cout<<"\nMailboxes which were closed at the end of the process are ";
    for(i=0;i<a;i++) // Prints the doors which are closed
        cout<<"\nDoor "<<arr[i];


I'll give you another clue:

This is similar to finding prime numbers using the Sieve of Eratosthenes

And another hint:
If you only use a boolean array, you can test if it's open or closed with:

if (mBox[i]) {
    // This mailbox is (true) opened, close it?
} else {
    // This mailbox is (false) closed, open it?

Cheers, and good luck!

1) 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 elements with idexes numbered from 0-149.

If you are forced to use a 150 element array then you need to take into account an offset, since you want to door numbers to be 1,2,3,... but the elements of an array are numbered (indexed) 0,1,2....

If you can use a 151 element array and ignore element with index 0, then you don't need to use an offset and the door number will be the index.

The outer loop controls where the inner loop starts (door number minus 1) and the size of increments used in the inner loop (door number).

The inner loop controls which element to look at.

In the inner loop:
if the current element is 1, then change current element to 0; otherwise, change the current element to 1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.