So this is my first week in CS265 and between refreshing my memory from a previous semester and grasping this new professor and concepts, I can't seem to figure this out. My prompt is this:
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.
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.
I wrote the code from beginning to end in about 2-3 hours, but have yet to get anything to display through my cout loop, besides my test line. Any help is much appreciated. Basically, I am unsure of how to accurately forge the necessary loops to open/close the correct mailboxes.
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
|
#include <iostream>
using namespace std;
enum mailbox{open, close};
int box[149];
void closeAll(); // this is working. don't change.
void boredPeter();
void coutClosed(); // this is working. don't change.
int main()
{
closeAll();
boredPeter();
coutClosed();
return 0;
}
void closeAll() // this is working. don't change.
{
for (int i = 0; i < 150; i++)
{
box[i] = close;
}
}
void boredPeter()
{
}
void coutClosed() // this is working. don't change.
{
for (int x = 0; x < 150; x++) //use this to test
{
if (box[x] = 1)
{
cout << "Mailbox #" << x+1 << " is closed" << endl;
}
else
{
}
}
}
|