Suppose that a row of closed mailboxes are numbered 1 through 150 and that beginning with mailbox 2, we open the doors of all the even-numbered mailboxes. Next, beginning with mailbox 3, we go to every third mailbox, opening its door if it is closed and closing it if it is open. We repeat this procedure with every fourth mailbox, then every fifth mailbox, and so on. Write a program to determine which mailboxes will be closed when this procedure is completed.
i have the whole program done but when i create the loop for the output it lists all of the mailboxes. i need to define the closed mailboxes but im not sure how. here is my program and what ive come up with so far.
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{\
// ignore 0 mailbox
bool mailbox[151];
for (int i=1; i<=150; i++)
mailbox[i] = false; // closed
for (int j = 2; j <= 151; j+=2) {
for (int i = j; i <= 151; i += j){
if (mailbox[i] == false)
mailbox[i] = true; // open mailbox
else
mailbox[i] = false; // mailbox closed
}
}
for (int i=1; i<=150; i++){
if (mailbox[i] == false) // if closed output mailbox number
cout << i << endl;
}
return 0;
}


Sign In
Create Account

Back to top









