1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
miss Akunina [59]
3 years ago
15

Locker doors There are n lockers in a hallway, numbered sequentially from 1 to n. Initially, all the locker doors are closed. Yo

u make n passes by the lockers, each time starting with locker #1. On the ith pass, i = 1, 2,...,n, you toggle the door of every ith locker: if the door is closed, you open it; if it is open, you close it. After the last pass, which locker doors are open and which are closed? How many of them are open?
Computers and Technology
1 answer:
kow [346]3 years ago
5 0

Answer:

// here is code in C++

#include <bits/stdc++.h>

using namespace std;

// main function

int main()

{

   // variables

   int n,no_open=0;

   cout<<"enter the number of lockers:";

   // read the number of lockers

   cin>>n;

   // initialize all lockers with 0, 0 for locked and 1 for open

   int lock[n]={};

   // toggle the locks

   // in each pass toggle every ith lock

   // if open close it and vice versa

   for(int i=1;i<=n;i++)

   {

       for(int a=0;a<n;a++)

       {

           if((a+1)%i==0)

           {

               if(lock[a]==0)

               lock[a]=1;

               else if(lock[a]==1)

               lock[a]=0;

           }

       }

   }

   cout<<"After last pass status of all locks:"<<endl;

   // print the status of all locks

   for(int x=0;x<n;x++)

   {

       if(lock[x]==0)

       {

           cout<<"lock "<<x+1<<" is close."<<endl;

       }

       else if(lock[x]==1)

       {

           cout<<"lock "<<x+1<<" is open."<<endl;

           // count the open locks

           no_open++;

       }

   }

   // print the open locks

   cout<<"total open locks are :"<<no_open<<endl;

return 0;

}

Explanation:

First read the number of lockers from user.Create an array of size n, and make all the locks closed.Then run a for loop to toggle locks.In pass i, toggle every ith lock.If lock is open then close it and vice versa.After the last pass print the status of each lock and print count of open locks.

Output:

enter the number of lockers:9

After last pass status of all locks:

lock 1 is open.

lock 2 is close.

lock 3 is close.

lock 4 is open.

lock 5 is close.

lock 6 is close.

lock 7 is close.

lock 8 is close.

lock 9 is open.

total open locks are :3

You might be interested in
The process of engineering design typically starts with what ?
klemol [59]

The process of engineering typically starts with brainstorming.
6 0
2 years ago
Write a machine-language program to add the three numbers 2, –3, and 6 and output the sum on the output device. Write it in a fo
olya-2409 [2.1K]

Answer:

Below is the code ...hope it meet the requirements

lda 0, i

adda 6, i

suba 3, i

adda 2, i

STA 0x0010 ,d

CHARO 0x0010 ,d

.end

Explanation:

8 0
3 years ago
For your biology class, you have taken a number of measurements for a plant growth experiment. You wish to create a chart that s
Mamont248 [21]
Calc or excel
Hope this helps
4 0
3 years ago
Eight houses represented as cells are arranged in a straight line java int state []
monitta

Answer:

int state[] = new int[8];

8 0
3 years ago
The primary stream fed by tributaries within a dendritic drainage network is termed a ________ stream
a_sh-v [17]
<span>TRUNK. In a dendritic system, there are many smaller rivers or streams (the "twigs" of the tree), which are then merged into the tributaries of the main river (the branches and the trunk of the tree, respectively). They tend to develop in V-shaped valleys</span>
6 0
3 years ago
Other questions:
  • In C, developers may access arrays via bracketed syntax like Java or by using * dereferencing notation. The following assignment
    5·1 answer
  • 4. How can you select non-adjacent cells (i.e. cells that are not all together in one block)?
    5·2 answers
  • In which of the security mechanism does the file containing data of the users/user groups have inbuilt security?
    6·1 answer
  • Using the Vigenere cipher, does the length of the key matter?
    8·1 answer
  • This exercise shows why each pivot (in eli1nination by pivoting) must be in a different row. (a) In Example 7, make the third pi
    15·1 answer
  • Write a program to generate a square wave with 80% duty cycle on bit P2.7 Microprocessor.​
    7·1 answer
  • 8.11 LAB: Filter and sort a list
    8·1 answer
  • Is spin to win paying or is a scam app​
    6·2 answers
  • Which option should you choose to change the background of your current slide?
    10·2 answers
  • Retype the below code. Fix the indentation as necessary to make the program work.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!