So I have a program scenario for a postman who carries out an experiment:
1. He has mailboxes numbered 1-150 all of which are 'closed'
2. Starting with mailbox 2, he 'opens' all even-numbered mailboxes, leaving all others 'closed'
3. Next, beginning with mailbox 3,goes on to 'open' every third 'if' its 'closed' and 'if' its 'open' he 'closes' it
4. He repeats step (3) with the 4th,5th...nth mailbox, opening or closing every 4th, 5th...nth mailbox accordingly.
5. display open and closed mailboxes

I've wrote the program all the way up to the third mailbox, but I just can figure out how to increment my 'interval' and my 'int i' in my 'for' loop to go onto to the 4th box and open or close every 4th and so on...if anyone has any ideas, they would be greatly appreciated!

#include<iostream>
#include<string>
#define CAPACITY 150
using namespace std;

int everyOther(int []);
int everyNthBox(int []);

int main()
{
    int mailbox[CAPACITY] = {0};    //0 closed, 1 open
    
    everyOther(mailbox);
    everyNthBox(mailbox);
    
    //test display
    cout << "0 is closed, 1 is open" << endl;
    for(int i = 0; i < CAPACITY; i++)
        cout << mailbox[i];  
    
    system("pause");
    return 0;
}
int everyOther(int mb[])
{
    for(int nthBox = 1; nthBox < CAPACITY; nthBox++)
    {
        mb[nthBox] = 1;
        nthBox++;
    }
}
int everyNthBox(int mb[])
{  
    int interval = 3;
    for(int i = 2; i < CAPACITY; i+=interval)
    {  
        if(mb[i])
            mb[i] = 0;
        else
            mb[i] = 1;
    }
}

When you can, always try to figure out what to do and what happens using pencil and paper before you write the program. In this case I would create a table:

For mailboxes with indexes 0-9 (10 mailboxes)all starting with value 0:

index        0123456789
interval = 2 0101010101 
interval = 3 0111000111
interval = 4 0110000011
interval = 5 0110100010

From the table you can see a pattern that looks like:

for intervals starting at 2, increment interval by one each time through array 
  for indexes starting at index of interval minus one, increment index by current interval 
    flip value of element in array at current index
commented: great help, I couldn't have done my program without it +1

Great, thanks, your steps 1 and 2 helped me understand the nested loop a lot better. Can you give me a little more information on how to flip the value of the element in array at current index though?

I took my everyNthBox() function out and am just using my everyOther() function below

int everyOther(int mb[])
{
    
    for(int interval = 2; interval < 74; interval++)
    {
        for(int i = interval-1; i < CAPACITY; i+=interval)
        {
            if(mb[i])
                mb[i] = 0;
            else
                mb[i] = 1;
        }
    }
}

When you can, always try to figure out what to do and what happens using pencil and paper before you write the program. In this case I would create a table:

For mailboxes with indexes 0-9 (10 mailboxes)all starting with value 0:

index        0123456789
interval = 2 0101010101 
interval = 3 0111000111
interval = 4 0110000011
interval = 5 0110100010

From the table you can see a pattern that looks like:

for intervals starting at 2, increment interval by one each time through array 
  for indexes starting at index of interval minus one, increment index by current interval 
    flip value of element in array at current index

Most systems will recognize zero as being false and anything (in this case 1) as being true. So if(mb) will often (always?) evaluate to true if the value is 1. If you want to be absolutely sure about things, the use if(mb == 1). Someplace, there has to be that if/else action that you have written. You might put it in a function and call the function (useful if you want to reuse the toggle/flip action in several places in your program) or you might use the ternary conditional operator ?, whatever, but someplace you have to do the equivalent of what you've already done.

Put the body of everyother() from your last post in main() and do not call any function such as everyother() or everyThird() or everyFouth() since the action of all the everyXs() is incorporated into the nested loop.

Oh I gotcha. Your help is much much much appreciated. The code is working as it should now! TY!

Most systems will recognize zero as being false and anything (in this case 1) as being true. So if(mb) will often (always?) evaluate to true if the value is 1. If you want to be absolutely sure about things, the use if(mb == 1). Someplace, there has to be that if/else action that you have written. You might put it in a function and call the function (useful if you want to reuse the toggle/flip action in several places in your program) or you might use the ternary conditional operator ?, whatever, but someplace you have to do the equivalent of what you've already done.

Put the body of everyother() from your last post in main() and do not call any function such as everyother() or everyThird() or everyFouth() since the action of all the everyXs() is incorporated into the nested loop.

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.