Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Peter the postman became bored one night and, to break the monotony of the night

ID: 3627371 • Letter: P

Question

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

Display the results as follows:

Display open mailboxes as '1'
Display closed mailboxes as '0'
Display a new line after each closed mailbox.


Try to write the code without using an if or conditional statement. Hint this can be done with the exclusive or.

Explanation / Answer

please rate - thanks

#include <iostream>
using namespace std;
bool exor(bool,bool);
int main()
{int boxes=150;
bool box[150];
int i,j,k;             

for (i=0;i<boxes;i++)   //odd boxes start closed
        box[i]=false;
for (i=1;i<boxes;i+=2)   //even boxes start open
        box[i]=true;
     
for(i=2;i<boxes;i++)           
   {
    for(j=i;j<boxes;j+=i+1)  
         box[j]=exor(box[j],true);
  
   }
cout<<"Final mailbox configuration (0=closed, 1=open) ";
for(k=0;k<boxes;k++)
        {cout<<box[k]<<" ";
        if(box[k]==0)
              cout<<endl;
         }
cout<<endl;        
system("pause");

}
bool exor(bool a,bool b)
{return (a||b)&&!(a&&b);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote