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
ioda
3 years ago
7

It is a well-researched fact that men in a restroom generally prefer to maximizetheir distance from already occupied stalls, by

occupying the middle of the longestsequence of unoccupied places.For example, consider the situation where ten stalls are empty._ _ _ _ _ _ _ _ _ _The first visitor will occupy a middle position:_ _ _ _ _ X _ _ _ _The next visitor will be in the middle of the empty area at the left._ _ X _ _ X _ _ _ _Write a program that reads the number of stalls and then prints out diagrams in theformat given above when the stalls become filled, one at a time. Hint: Use an array ofboolean values to indicate whether a stall is occupied.
Computers and Technology
1 answer:
Artyom0805 [142]3 years ago
7 0

Answer:

The code below addresses the problem statement with appropriate comments for explanation.

Explanation:

public class Stall

{

public static void main(String[] args)

{

boolean[] arr = new boolean[10];

while(hasFreeSpots(arr))   // as long as there is a free slot

{

arr[position(arr)] = true;  

// position(arr) gets the position of slot a person would most likely enter at(view problem statement)

// setting that position true(position occupied) by arr[position(arr)]

System.out.println(stallView(arr));   // printing current state of the stall using 'X' and '_'

}

}

/**

* Find the empty position that is in the middle of the sub-array of largest group of empty spots

* @param arr an array of boolean values

* @return returns an an integer value. Optimal position to be occupied.

*/

public static int position(boolean[] arr)

{

int first = 0;   // first stores the starting position of "longest sequence of unoccupied places"

int last = 0;   // last stores the ending position of "longest sequence of unoccupied places"

int temp1 = 0;   // temp1 stores the starting position of "current sequence of unoccupied places"

int temp2 = 0; // temp2 stores the ending position of "current sequence of unoccupied places"

 

for(int i = 0; i < arr.length; i++)   // for each slot in the Stall

{    

if(!arr[i]) // if slot i is not occupied

{    

temp1 = i;   // store starting position of "current sequence of unoccupied places" in temp1

while(i < arr.length && !arr[i] )   // if i is a valid index and as long as this slot is not occupied increment i

{

i++;

}

// we break out of the loop either if i is invalid index(reached end of the array)

// or if the slot is occupied by a person already

temp2 = i;   // store ending position of "current sequence of unoccupied places" in temp2

if(Math.abs(first - last) < Math.abs(temp1 - temp2))

  // if current sequence is longer than longest sequence, update it

{

first = temp1;

last = temp2;    

}

}

}

return (first + last) / 2;   // return middle index of first and last

}

/**

* Checks if there are empty spots in an array, basically checks if there is a false boolean value

* thats representing an empty spot in an array

* @param arr the input array, of boolean values

* @return returns a boolean value, true if there are false elements inside the array

*/

public static boolean hasFreeSpots(boolean[] arr)

{

boolean rtn = true;   // initializing rtn to 'true'

for(int i = 0; i < arr.length; i++)   // for every slot in the Stall

{

if(!arr[i]){rtn = false;}   // sets rtn to false if a free spot found

}

return !rtn;   // returns true if a free spot found and returns false if no free spot found

}

/**

* X for true, _ for false. Representing occupied(X) and free stalls(_)

* @param arr a boolean array

* @return returns a string representation if the input

*/

public static String stallView(boolean[] arr)

{

String str = "";       // initializing str to be an empty String

for(int i = 0; i < arr.length; i++)   // for every slot in Stall

{

if(arr[i]){str += " X";}   // Add X to str if slot is occupied

else {str += " _";}           // prints _ to str if slot not occupied

}

return str;       // return str(pictorial representation of Stall with 'X', '_')

}

}

You might be interested in
5. Many vehicles have indicator lights telling you when your
I am Lyosha [343]

Answer:

A.

Explanation:

The rest is nearly impossible to detect or not worth the time.

8 0
3 years ago
Read 2 more answers
Using a WHILE loop write a program that asks the user to guess your favorite number. The program should end when the user guesse
eduard

Answer:

fav_number=26

guess=0

while 0==0:

   try:

       guess=int(input('Enter your guess: '))

   except:

       print ('You have entered an invalid value.')

   else:

       if guess == fav_number:

           break

       elif guess>fav_number:

            print ('Try a smaller number.')

       elif guess<fav_number:

           print ('Try a larger number')

Explanation:

define a variable fav_number and set it to number. write an infinite while loop to read input from user. In try block get input from user, if user enters invalid input code in except block will be executed. For valid input

  1. check if its greater than favorite number, if yes print try smaller number.
  2. check if its smaller than favorite number, if yes print try larger number.
  3. check if its equal to favorite number, break loop and exit

4 0
4 years ago
Which of these is an application? Microsoft Windows Linux Microsoft Word Apple ios​
STALIN [3.7K]

Answer:

I think it's Microsoft windows

8 0
4 years ago
Read 2 more answers
A programmer needs to understand sequencing to determine whether the order of steps will affects the outcome of the program.
professor190 [17]

Answer:

true

Explanation:

Based on the information provided within the question it can be said that this statement is completely true. Programming code is built as a series of steps that are followed in a linear way. So much so that one action needs to be completed before another action can begin. There are also conditions such as, if step 1 is true then execute step 2, these are all sequences that must be understood by the individual writing the code.

5 0
3 years ago
Which of the following best explains how algorithms that run on a computer can be used to solve problems?
timama [110]

The statement which best explains how algorithms running on a computer can be used to solve problems is; D. Some problems cannot be solved by an algorithm.

<h3>What is an algorithm?</h3>

An algorithm is simply a standard formula or procedures which is made up of a set of finite steps and instructions that must be executed on a computer, so as to proffer solutions to a problem or solve a problem under appropriate conditions.

However, it should be noted that it is not all problems that can be solved by an algorithm, especially because the required parameters and appropriate conditions are not feasible or met.

Read more on algorithm here: brainly.com/question/24793921

7 0
2 years ago
Other questions:
  • What is the primary limitation of free versions of antivirus applications
    14·1 answer
  • I am doing Microsoft Excel and I have do formulas, Can some please explain to me how do them?
    14·1 answer
  • On a network, a(n) ________ helps prevent data packets from colliding with each other.
    9·1 answer
  • 1. The number of major solutions for preventing workplace hazards is
    14·1 answer
  • Explain why you should research a potential employer before actively seeking employment
    12·1 answer
  • What is the output of the AWK program?
    11·1 answer
  • What is the difference between cybercrime and cyber security?
    12·1 answer
  • A computer takes a lot of time to do complex calculation​
    9·1 answer
  • 6. By default,how the table headings are placed. (2 Points)
    6·1 answer
  • Original documents or objects used during Internet research are known as
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!