Problem description

For this program, you will model the flow of customers through a grocery store check- out queue.

To create this simulation, you must model both the passage of time and the flow of customers through the checkout process. You can model time using a loop in which each iteration corresponds to a set time interval - one (1) minute, for example. You can model the flow of customers using a queue in which each data item corresponds to a customer in the line.

To complete the simulation, you need to know the rate at which customers join the line, as well as the rate at which they are served and leave the line. Suppose the grocery store check-out has the following properties:

  • One customer is checked out every minute (assuming there is at least one customer waiting to be served during that minute).
  • Between zero and two customers join the line every minute, where there is a 50 percent chance that no customers arrive, a 25 percent chance that one customer arrives, and a 25 percent chance that two customers arrive.
  • Customers who are still in the queue at the end of the simulation should be checked out, but no new customers should be allowed to join the queue during this time. The wait times of these leftover customers should be counted along with all the other customers.

Background information

You can simulate the flow of customers through the checkout during a specific time period t minutes long using the following algorithm (this basic algorithm works with many simulation scenarios):

1. Initialize the queue to empty.

2. While the simulation is not done:

  • Increment simulated time by one minute
  • If the queue is not empty, then remove the customer leaving the checkout.
  • Compute a random number k between 0 and 3.
    • If k is 1, then add one customer to the line.
    • If k is 2, then add two customers to the line.
    • Otherwise (if k is 0 or 3), do not add any customers to the line.
  • Update queue statistics

3. Check out the all customers remaining in queue.

4. Display summary information showing total customers served, average wait time, longest wait time, and the number of customers remaining in queue at the end of the simulation. To maintain good customer relations, any customers in queue will be served, extending the simulation time to accommodate them.

Program requirements

Extend the std::list< T > class to implement a template Queue class with the following interface:

/// A container adapter that gives the programmer the functionality of a
/// queue -- specifically, a FIFO (first-in, first-out) data structure.
/// The class template acts as a wrapper to the underlying container --
/// only a specific set of functions is provided. The Queue pushes the
/// elements on the back of the underlying container and pops them from
/// the front.

template < class T >
class Queue : protected std::list< T > {
public:
/// Returns reference to the first element in the Queue. This element
/// will be the first element to be removed on a call to pop().
T& front();
/// Returns reference to the last element in the Queue. This is the most
/// recently pushed element.
T& back();
/// Checks if the underlying container has no elements.
bool empty() const;
/// Pushes the given element 'value' to the end of the Queue.
void push(const T& value);
/// Removes an element from the front of the queue.
void pop();
};

Use your derived Queue class to facilitate your simulation.

Read the number of minutes to run the simulation from the command line. (The command line argument must be converted from a c-string to an integer value.) If no command line argument is provided, default to 1440 minutes (24 hours). If a command line argument is provided, then check its integer value. If less than 30, display a usage message and terminate. Else, run the simulation for the number of minutes passed via the command line.

Create a program to model the grocery store checkout described above. Your program should update the following information during each simulated minute.

  • The total number of customers served.
  • The combined length of time these customers spent waiting in line.
  • The maximum length of time any of these customers spent waiting in line.

The data that are stored in the queue should contain everything that is necessary to 1) represent the customer and 2) compute the previous statistics. To compute how long a customer waited to be served, you need the difference in time from when the customer arrived to when the customer completed checkout. There is no additional information needed in the statistics, nor is there any customer-specific information (e.g., items purchased) that is used for our simple simulation. Therefore, we can represent the customer in the queue simply by using the simulated time the customer entered the queue.

Use your program to simulate the flow of customers through the checkout process and complete the table shown in the Sample Output below. Note that the average wait is the combined waiting time divided by the total number of customers served.

Input specification

Input is read from the command line and converted to an integer value. If no command line argument is provided, the simulation duration is set to 1440 minutes (24 hours). If the command line argument's value is less than 30 minutes, print the error message shown and exit. (Note: the name of the program should be taken from the command line argument and not hard-coded into your program. A user may choose to rename the program.)

Sample output

Simulated data is shown. Your results will differ. Average time should be displayed with one decimal digit of precision.

$ ./pa18b 30
Simulation duration: 30 minutes
Customers Served: 23
Average Wait: 2.5 minutes
Longest Wait: 6 minutes
Customers in queue: 0

$ ./pa18b hello!
Invalid duration.
Usage: ./pa18b minutes (min 30)

$ ./pa18b 10
Invalid duration.
Usage: ./pa18b minutes (min 30)

$ ./pa18b
Simulation duration: 1443 minutes
Customers Served: 907
Average Wait: 6.1 minutes
Longest Wait: 11 minutes
Customers in queue: 3
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.