﻿ Hmwk 9

## Homework 9

In this assignment, you will write a program that simulates a waiting line. There are three basic components to the simulation; clients, the waiting line, and servers. Clients represent entities that require some sort of action. The time required to perform the action may vary from client to client and clients arrive at random times.

As clients arrive, they move to the end of the waiting line (no cuts allowed). In some cases, there may be more than one waiting line in which case clients move to the end of the shortest waiting line. However, in our simulation, we will have only one waiting line.

Servers perform the action required by the clients. When they are done servicing a client, the first client in the waiting line will be the next client served. A single waiting line may be served by more than one server. If there are multiple waiting lines, each line is usually served by a single server. In our case, we will have a single server serving our single waiting line.

A time-driven simulation simulates such a system by determining the state of the system at discrete moments of time. The time interval depends on the type of system being simulated. For example, if you are simulating processes waiting for service by a CPU, the time interval might be a millisecond or even a microsecond. If you are simulating a waiting line in a busy post office, the time interval might be a minute. If you are simulating complex jobs like bricking the exterior of a house, the time interval might be a hour.

In any case, the simulation generally consists of a for loop that, like a clock, counts time intervals beginning at 0 up to the desired duration of the simulation. For each tick of the "clock", the simulation determines whether a new client arrives and if so sends it to a waiting line. It then removes clients from servers that have just finished serving the client. Finally, if any servers are free (including those that have just become free) the program moves a client from a waiting line to the server (if there are any clients in line, of course).

When the stop time is reached there may still be clients waiting in line. We could just ignore them or, more realistically, we continue running the clock until all clients have been served. However, no new clients are added to the line after the stop time.

### Output Specifications

Your program will employ a single waiting line and a single server. At the end of the time specified by the user, the server will continue to serve the clients that were already in line but no new clients will be added to the line. Your program should display the specified duration of the simulation, the actual duration, the arrival probability, the range of service times, the number of clients served, the average wait time and the maximum wait time.

### Input Specifications

The user will enter, interactively, the following:

• the time unit (such as second, minute, hour, etc.) as a string.
• the length of time the simulation is to run as an integer. You may assume that this is expressed in the time units previously entered by the user.
• the probability that a client will arrive at any given unit of time. This is a double value in the closed interval [0,1].
• The shortest possible service time and the longest possible service time; both as integers. Both are assumed to be expressed in the time units previously entered by the user.

The sample run shown below indicates how the input and output will be formatted on the computer screen.

### Process Specifications

The simulation will be coded within a counting loop that counts from 0 to the length of time the simulation is to run. Each time through the loop, you will perform the following tasks:

• Determine whether a new client arrives in this unit of time.
• If so, set the arrival time of the client, determine the service time for the client, and place the client in the waiting line.
• Determine if the server has just finished serving a client. If so, remove the client from the server.
• If the server is free and there is a client waiting in line, send the client to the server and set the exit time of the client (i.e., the time it left the waiting line).

At some point you will need to update the variables that allow you to calculate the average wait time and the maximum wait time. You can do this when the client leaves the waiting line or, if you prefer, do it when the client leaves the server.

Use a uniform probability distribution to determine the length of time required to serve a client. For example, suppose the shortest service time entered by the user is 2 and the longest is 5. In that case, there are four possible service times (2, 3, 4, or 5 units of time) each with a probability of 1/4.

The waiting line itself is modeled by a queue object. A client is added to the rear of the queue and served when it reaches the front of the queue and the server is free.

### File Submission

I've done some of the work for you in Hmwk09.cpp. Add your name as co-author and send your final version to me as an email attachment.