### Array help, outputs the wrong thing

So, I thought the code looked good, but it doesn't output what it should. The code is supposed to be like:

Mailbox #1, #4, #9, #16 is closed etc.

Mailbox #1, #2, #3, #4, #5 etc...#150 is close.. you get the point, so it's like it's only going to by the OutClosed Function.

Here's the actual assignment.
Problem Statement

Peter the postman became bored one night and, to break the monotony of the night shift, he carried out the following experiment with a row of mailboxes in the post office. These mailboxes were numbered 1 through 150, and beginning with mailbox 2, he opened the doors of all the even-numbered mailboxes, leaving the others closed. Next, beginning with mailbox 3, he went to every third mail box, opening its door if it were closed, and closing it if it were open. Then he repeated this procedure with every fourth mailbox, then every fifth mailbox, and so on. When he finished, he was surprised at the distribution of closed mailboxes. Write a program to determine which mailboxes these were.

Assignment

In your program, first declare an enumeration type for mailbox door open or close in global.

Next write a function to initialize all mailbox doors to closed.

Next write a second function that will conduct what Peter did as described above.

Next write another function that will output all of the mailboxes whose doors are closed.

Next write the main function. In your main function, call all of the above three functions to find out the distribution of closed mailboxes for Peter.

BTW It is supposed to be a call by parameters also.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172`` `````` #include #include enum mailbox { close, open }; mailbox box[150]; using namespace std; void AllClosed(mailbox box[]); void Peter(mailbox box[]); void OutClosed(mailbox box[]); int main() { AllClosed(box); Peter(box); OutClosed(box); system("Pause"); return 0; } void AllClosed(mailbox box[]) { for (int i = 0; i < 150; i++) { box[i] = close; } } void Peter(mailbox box[]) { for (int x = 1; x < 150; x = x + 2) { if (box[x] == close) { box[x] == open; } else { box[x] == close; } } for (int y = 2; y < 150; y++) { for (int z = y; z < 150; z = z + 1) { if (box[z] == close) { box[z] == open; } else { box[z] == close; } } } } void OutClosed(mailbox box[]) { for (int index = 0; index < 150; index++) { if (box[index] == close) { cout << "Mailbox #" << index + 1 << " is closed" << endl; } } }``````

You want something like this for peter... its not exactly right but this is the idea

while (start != 150)
{
for(x = start; x < 150; x +=start) //the key to what I am showing you..
//if start is 1, its 1-149, if start is 2, its 2,4,6,8..., then its 3,6,9, then 4,8,12, etc
toggle mailboxes[x];
start ++;
}

I believe this is directly solvable, that is, I think there is a formula that just gives the answers without the brute force.
Last edited on
Topic archived. No new replies allowed.