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;
}
}