Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify or rewrite the Queue class (Display 13.21 through 13.23) to simulate cust

ID: 3598882 • Letter: M

Question

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 at 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.

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 <ctime>

...

int main()

{

long seconds;

seconds = static_cast<long>(time(NULL));

cout << "Seconds since 1/1/1970: " << seconds << endl;

return 0;

}

Sample execution is shown here:

The line is empty.

Enter '1' to simulate a customer's arrival, '2' to help the

next customer, or '3' to quit.

1

Customer 1 entered the queue at time 100000044.

Enter '1' to simulate a customer's arrival, '2' to help the

next customer, or '3' to quit.

1

Customer 2 entered the queue at time 100000049.

Enter '1' to simulate a customer's arrival, '2' to help the

next customer, or '3' to quit.

1

Customer 3 entered the queue at time 100000055.

Enter '1' to simulate a customer's arrival, '2' to help the

next customer, or '3' to quit.

2

Customer 1 is being helped at time 100000069. Wait time = 25

seconds.

The estimated wait time for customer 2 is 25 seconds.

Enter '1' to simulate a customer's arrival, '2' to help the

next customer, or '3' to quit.

2

Customer 2 is being helped at time 100000076. Wait time = 27

seconds.

The estimated wait time for customer 3 is 26 seconds.

Enter '1' to simulate a customer's arrival, '2' to help the

next customer, or '3' to quit.

1

Customer 4 entered the queue at time 100000080.

Enter '1' to simulate a customer's arrival, '2' to help the

next customer, or '3' to quit.

2

Customer 3 is being helped at time 100000099. Wait time = 44

seconds.

The estimated wait time for customer 4 is 32 seconds.

Explanation / 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();

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote