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.

but instead, the output is:

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  #include <iostream>
#include <iomanip>
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.