Part A (Savitch, Q. 8, p.786) Modify or rewrite the Queue class (Display 13.21 t
ID: 3600914 • Letter: P
Question
Part A (Savitch, Q. 8, p.786) Modify or rewrite the Queue class (Display 13.21 through 13.23) to simulate customer arrivals at the Department of Motor Vehicles (DMV) counter. As customers arrive, they are given a ticket number starting a 1 and incrementing with each new customer. When a customer service agent is free, the customer with the next ticket number is called. This system results in a FIFO queue of customers ordered by ticket number. Write a program that implements the queue and simulates customers entering and leaving the queue. Input into the queue should be the ticket number and a timestamp when the ticket was entered into the queue. A ticket and its corresponding timestamp is removed when a customer service agent handles the next customer. Your program should save the length of time the last three customers spent waiting in the queue. Every time a ticket is removed from the queue, update these times and output the average of the last three customers as an estimate of how long it will take until the next customer is handled. If nobody is in the queue output that the line is empty 0 Code to compute a timestamp based on the computer's clock is given below. The time(NULL) function returns the number of seconds since January 1, 1970, on most implementations of C++ #include int main() long seconds; seconds = static-cast(time (NULL)); coutExplanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
#define MAX_SIZE 100
class Queue{
private:
int data[MAX_SIZE];
long timestamp[MAX_SIZE];
long time_a, time_b, time_c;
int removed;
int front;
int rear;
public:
Queue(){
front = -1;
rear = -1;
time_a=0;time_b=0;time_c=0;
removed=0;
}
void Enqueue(int element, long time){
if ( Size() == MAX_SIZE - 1 ){
cout << "Queue is full" << endl;
return;
}
data[rear] = element;
timestamp[rear] = time;
rear = ++rear % MAX_SIZE;
cout << "Customer " << element << " entered the queue at time " << time << "." << endl;
}
void Dequeue(){
long now = static_cast<long>(time(NULL));
if ( isEmpty() ){
cout << "Queue is empty" << endl;
return;
}
long ret = timestamp[front];
long wait_time = now - ret;
cout << "Customer "<<data[front]<<" is being helped at time "<<ret<<". Wait time = "<<wait_time<<" seconds." << endl;
time_c = time_b;
time_b = time_a;
time_a = wait_time;
front = ++front % MAX_SIZE;
removed++;
if(front==rear){
cout << "Queue is empty" << endl;
}else{
long avg_wait_time = (time_a+time_b+time_c)/3.0;
if(removed==1){
avg_wait_time = time_a;
}else if(removed==2){
avg_wait_time = (time_a+time_b)/2.0;
}
cout << "The estimated wait time for customer "<<data[front]<<" is "<<avg_wait_time<<" seconds." << endl;
}
}
int Front(){
if ( isEmpty() ){
cout << "Queue is empty" << endl;
return -1;
}
return data[front];
}
int Size(){
return abs(rear - front);
}
bool isEmpty(){
return ( front == rear ) ? true : false;
}
};
int main(){
int command, counter=1;
long time_stamp;
Queue q;
while(true){
cout << "Enter '1' to simulate a customer's arrival, '2' to help the next customer, or '3' to quit." << endl;
cin >> command;
if(command==3){break;}
if(command==1){
time_stamp = static_cast<long>(time(NULL));
q.Enqueue(counter++, time_stamp);
}else{
q.Dequeue();
}
}
}