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

Simulation of DMV queue Use the dynamic queue class to simulate customers arrivi

ID: 3557565 • Letter: S

Question

Simulation of DMV queue Use the dynamic queue class to simulate customers arriving at the DMV and being serviced.

Customers arrive at random intervals. Each will be given a ticket with their name, a number and a time stamp. The customer will enter the queue to wait to be served. When a customer is served, they are taken out of the queue.

The DMV service agent will process this person for a certain amount of time and then go on to the next person.

In order to simulate the time of arrival of customers and the time of processing, you will need to use the localtime function, a wait function, and a random number generator. The random number should be between 1 and 5. Add customers to the queue by using a random number generator to choose a random number between 1 and 5.

Use this number as a parameter to the wait function. When the wait function ends, add them to the queue. When there are customers waiting, the DMV service agent function should dequeue a customer, generate a random number between 1 and 10 and use this number as a parameter to the wait function simulating the processing time for that person.

The process function will then need to find the time the person waited by calling the time function and finding the difference between the start and end timestamps. Save this time to an accumulator so that in the end you can find the average wait time for all customers.

Add 10 people to the line and process them. Each time you process someone, print out their ticket information. After all 10 have been serviced, print the average wait time. Below is an example of the time calls you will need and a wait function.

The time code below should help me with this process but I'm still very confused:

#include <ctime>

#include <iostream>
using namespace std;
void wait ( int seconds );
int main()
{
   
    wait(5);
   
    time_t rawtime;
    struct tm * timeinfo;

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    cout << timeinfo->tm_hour<< ":" << timeinfo->tm_sec;
   wait(5);
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    cout << timeinfo->tm_hour<< ":" << timeinfo->tm_sec;

    return 0;
}
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}

Explanation / Answer

/*here is another version which doesnt have threads

once the queue is full that is 10 customers arrives then it starts processing them

*/

#include<ctime>
#include<iostream>
#include<pthread.h>
#include<string>
#include<cstdlib>
#include<queue>


using namespace std;

struct Ticket{
string name;
int num;
struct timeOfArrival{
short int hr;
short int min;
short int sec;
}toa;
};
void wait ( int seconds );
queue<struct Ticket> Queue;
void addCustomers()
{

for(int i=0;i<10;i++){
Ticket tk;
tk.name="P";
tk.num=rand()%100007;
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
tk.toa.hr= timeinfo->tm_hour;
tk.toa.min= timeinfo->tm_min;
tk.toa.sec=timeinfo->tm_sec;
cout<<"Customer "<<i+1<<" Arrives at time "<<tk.toa.hr<<":"<<tk.toa.min<<":"<<tk.toa.sec<<endl;
Queue.push(tk);


wait(rand()%5);
}

}

int main()
{

time_t rawtime;
struct tm *timeinfo;
pthread_t th;
long tid=0;
int sum=0;
int processingTime[10];
addCustomers();
//wait if no customer are there in queue;
while(Queue.empty()){
}
//now first customer arrives;
struct Ticket tk;
int passengerNo=0;
while(!Queue.empty()){
while(Queue.empty()&& passengerNo<10){}//wait if no customer in queue
tk=Queue.front();
wait(rand()%10);
time ( &rawtime );
timeinfo = localtime ( &rawtime );
cout<<" Processing passenger no "<<passengerNo+1<<" at time "<<timeinfo->tm_hour<<":"<<timeinfo->tm_min<<":"<<timeinfo->tm_sec<<endl;
cout<<"Ticket Number "<<tk.num<<endl;
cout<<"Arrival Time "<<tk.toa.hr<<":"<<tk.toa.min<<":"<<tk.toa.sec<<endl;
/******calculating processing time*******/
int sec=0;
int time1=tk.toa.hr*3600+tk.toa.min*60+tk.toa.sec;
int time2=timeinfo->tm_hour*3600+timeinfo->tm_min*60+timeinfo->tm_sec;
sec=time2-time1;

/***************************/
processingTime[passengerNo]=sec;

passengerNo+=1;
Queue.pop();
}

cout<<"Average processing time for all customers = ";
for(int i=0;i<10;i++)
sum+=processingTime[i];

cout<<fixed<<sum/10.0<<" seconds";


return 0;
}
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}