I\'m not getting right output. when calls to next customer, It gives negative nu
ID: 3281739 • Letter: I
Question
I'm not getting right output. when calls to next customer, It gives negative numbers and I can't fix the time
Write a C++ program to simulate customer arrival at the Department of Motor Vehicles counter.
As customers arrive, they are given a ticket number starting at 1 and increment with each new customer. when a customer service agent is free, the customer with the next ticket is called. This system results in FIFO queue of customers ordered by ticket number. Your program will implement the queue and simulate customers entering and leaving the queue. Input into the queue should be the ticket number and time stamp when the ticket was entered into the queue. A ticket and its time stamp are removed from the queue when a customer service agent starts handling the next customer in the queue. The program should keep track and print waiting time for that customer. The program will also simulate the varying times of handling a customer by a customer service agent. If no body is in the queue, output that the line is empty.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include
#include
#include
using namespace std;
#define MAX_SIZE 100
class DMV {
private:
int data[MAX_SIZE];
long time1, time2, time3;
int exit;
int front;
int back;
long printTime[MAX_SIZE];
public:
DMV()
{
front = -1;
back = -1;
time1 = 0;
time2 = 0;
time3 = 0;
exit = 0;
}
void Enqueue(int element, long time)
{
if (Size() == MAX_SIZE - 1)
{
cout << "DMV is full" << endl;
return;
}
data[back] = element;
printTime[back] = time;
back = ++back%MAX_SIZE;
cout << "customer number: " << element << "Entered time: " << time << "." << endl;
}
void dequeue()
{
long now = static_cast(time(NULL));
if (isEmpty())
{
cout << "DMV is empty" << endl;
return;
}
long helped = printTime[front];
long wait_time = now - helped;
cout << "Customer" << data[front] << "id being helped at time" << helped << ". Wait time= " << wait_time << "seconds" << endl;
time3 = time2;
time2 = time1;
time1 = wait_time;
front = ++front%MAX_SIZE;
exit++;
if (front == back)
{
cout << "The queue is empty............" << endl;
}
else
{
long avg_wait_time = (time1 + time2 + time3) / 3;
if (exit == 1)
{
avg_wait_time = time1;
}
else if (exit == 2)
{
avg_wait_time = (time1 + time2) / 2;
}
cout << "The estimated wait time for customere" << data[front] << " is " << avg_wait_time << "seconds" << endl;
}
}
int Front()
{
if (isEmpty())
{
cout << "The queue is empty............" << endl;
return -1;
}
return data[front];
}
int Size()
{
return abs(back - front);
}
bool isEmpty()
{
return(front == back) ? true : false;
}
};
int main()
{
int choice, data = 1;
long time_stamp;
DMV q;
while (true)
{
cout << "Enter 1 to new customer's arrival" << endl;
cout << "Enter 2 to for call next customer." << endl;
cout << "Enter 3 to quit" << endl;
cin >> choice;
if (choice == 3)
{
return 0;
}
if (choice == 1)
{
time_stamp = (long)time(NULL);
q.Enqueue(data++, time_stamp);
}
else
{
q.dequeue();
}
}
}
output
Enter 1 to new customer's arrival
Enter 2 to for call next customer.
Enter 3 to quit
1
customer number: 1Entered time: 1521429982.
Enter 1 to new customer's arrival
Enter 2 to for call next customer.
Enter 3 to quit
1
customer number: 2Entered time: 1521429984.
Enter 1 to new customer's arrival
Enter 2 to for call next customer.
Enter 3 to quit
1
customer number: 3Entered time: 1521429985.
Enter 1 to new customer's arrival
Enter 2 to for call next customer.
Enter 3 to quit
1
customer number: 4Entered time: 1521429988.
Enter 1 to new customer's arrival
Enter 2 to for call next customer.
Enter 3 to quit
2
Customer1id being helped at time86. Wait time= 1521429904seconds
The estimated wait time for customere-858993460 is 1521429904seconds
Enter 1 to new customer's arrival
Enter 2 to for call next customer.
Enter 3 to quit
2
Customer-858993460id being helped at time-858993460. Wait time= -1914543842seconds
The estimated wait time for customere-858993460 is -196556969seconds
Enter 1 to new customer's arrival
Enter 2 to for call next customer.
Enter 3 to quit
Explanation / Answer
Solution:
CPP Code:
#include <iostream>
#include <queue>
#include <ctime>
using namespace std;
/*stores the waittime of last 3 customer*/
long last_waittime[3];
/*store the number of customer came*/
int customer_no = 1;
/*customer class*/
class customer
{
public:
int number;
long sec;
customer()
{
}
customer(int no)
{
number = no;
sec = static_cast<long>(time(NULL));
}
};
int main()
{
int option;
long wait_time, Avg;
queue<customer *> myqueue;
customer *cust;
cout << "The line is empty.";
while(1)
{
cout << "Enter '1' to simulate a customer's arrival, '2' to help the next customer, or '3' to quit. ";
cin >> option;
switch(option){
case 1: /*new customer*/
/*creating and pushing new customer into queue*/
cust = new customer(customer_no++);
myqueue.push(cust);
cout << "Customer " << cust->number <<" entered the queue at time " << cust->sec << endl;
break;
case 2:/*service time*/
if (myqueue.empty()) /*queue is empty*/
{
cout << "The line is empty. ";
}
else
{
/*getting the first customer from the queue*/
cust = myqueue.front();
myqueue.pop();
/*calulating average waiting time*/
wait_time = static_cast<long>(time(NULL)) - cust->sec;
last_waittime[cust->number % 3] = wait_time;
Avg = (last_waittime[0] + last_waittime[1] + last_waittime[2]) / 3;
cout << "Customer " << cust->number << " is being helped at time " << static_cast<long>(time(NULL)) <<". Wait time = "<< wait_time <<" sec.";
cout <<" The estimated wait time for customer "<< cust->number +1 <<" is "<< Avg <<"seconds. ";
}
break;
case 3:
cout << "Bye Bye !!";
return 0;
break;
default:
cout << "You have entered the wrong optiom ";
break;
}
}
return 0;
}
output screenshot:
./a.out
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 1517686373
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 1517686374
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 1517686375
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 1517686379
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 1517686380.
Wait time = 7 sec. The estimated wait time for customer 2 is 2seconds.
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 1517686382.
Wait time = 8 sec. The estimated wait time for customer 3 is 5seconds.
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 1517686382.
Wait time = 7 sec. The estimated wait time for customer 4 is 7seconds.
Enter '1' to simulate a customer's arrival, '2' to help the next customer, or '3' to quit.
2
Customer 4 is being helped at time 1517686384.
Wait time = 5 sec. The estimated wait time for customer 5 is 6seconds.
Enter '1' to simulate a customer's arrival, '2' to help the next customer, or '3' to quit.
2
The line is empty.
Enter '1' to simulate a customer's arrival, '2' to help the next customer, or '3' to quit.
3
Bye Bye !!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.