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

Please, I need that in C++ language Using C++: Write a simulation program of the

ID: 675250 • Letter: P

Question

Please, I need that in C++ language

Using C++: Write a simulation program of the lines at a grocery store. The program will be similar to the car wash simulation, except that there are multiple queues instead of one. You might use an array (or vector) of queues to simulate the lines. Assume that there are five cashier lines at the grocery store. Customers enter randomly to check out, and then enter the shortest line. If the lines are equal, then the first available line is chosen. Each transaction takes a random amount of time to complete.

For additional work, expand the grocery line program to allow shoppers to

-avoid a line if all lines are a certain length

-leave a line if they have waited beyond a certain time

-check if another line is shorter at specified time intervals

-switch lines if another line is shorter

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct queueNode
{
int cust_n;
int serv_t;
int arr_t;
struct queueNode *nextPtr;
};
typedef struct queueNode customer;
typedef customer *customerPtr;
void printQueue( customerPtr );
int isEmpty( customerPtr );
void dequeue( customerPtr *, customerPtr * );
void enqueue( customerPtr *, customerPtr *, int ,int ,int );
int main()
{
int i; int t_time = 0;
int t_arrival = 0;
int t_depart = -1;
int customer_n = 0;
int serv_time = 0;
int MAX_SERV_TIME = serv_time;
int tot_wait_t = 0;
customerPtr headptr = NULL,tailptr = NULL;
srand((unsigned int)time(NULL));
while(t_time != 10)
{
printf("TIME = %d,MAX_SERV_TIME = %d,TOT WAIT TIME = %d ",t_time,MAX_SERV_TIME,tot_wait_t);
if(t_time == t_depart && !isEmpty(headptr))
{
printf("customer %d ON ",headptr->cust_n);
serv_time = headptr->serv_t;
service time tot_wait_t += (t_time - headptr->arr_t);
dequeue(&headptr,&tailptr);
if(!isEmpty(headptr)) t_depart = serv_time + t_time;
}
if(t_time == t_arrival)
{   
serv_time = rand()%4 + 1;
MAX_SERV_TIME = (serv_time>MAX_SERV_TIME?serv_time:MAX_SERV_TIME);
if(isEmpty(headptr)) t_depart = t_time + serv_time;
enqueue(&headptr,&tailptr,customer_n,serv_time,t_arrival);
t_arrival += rand()%4 + 1;
printf("customer %d joins at %d,serv_time %d,new_cust %d ",tailptr->cust_n,tailptr->arr_t,tailptr->serv_t,t_arrival);
}
printQueue(headptr);
getchar();
t_time++;
}
customer_n = (tailptr->cust_n - headptr->cust_n)+ 1;
printf("average wait time = %d,max service time = %d,tot custs unserved %d ",tot_wait_t/customer_n,MAX_SERV_TIME,customer_n);
for(i = 0;i<customer_n;i++)
{
printQueue(headptr);
getchar();
dequeue(&headptr,&tailptr); } //printQueue(headptr); return 0;
}
void enqueue( customerPtr *headPtr, customerPtr *tailPtr,int cust_n,int serv_t,int arr_t)
{
customerPtr newPtr; newPtr = malloc( sizeof( customer ) );
if ( newPtr != NULL )
{
newPtr->cust_n = cust_n;
newPtr->serv_t = serv_t;
newPtr->arr_t = arr_t;
newPtr->nextPtr = NULL;
if ( isEmpty( *headPtr ) ) *headPtr = newPtr;
else ( *tailPtr )->nextPtr = newPtr;
*tailPtr = newPtr;
}
else
printf("No memory available. ");
}
void dequeue( customerPtr *headPtr, customerPtr *tailPtr )
{
customerPtr tempPtr;
tempPtr = *headPtr;
*headPtr = ( *headPtr )->nextPtr;
if ( *headPtr == NULL ) *tailPtr = NULL;
free( tempPtr );
}
int isEmpty( customerPtr headPtr )
{
return headPtr == NULL;
}
void printQueue( customerPtr currentPtr )
{
if ( currentPtr == NULL )
printf( "Queue is empty. " );
else
{
printf( "The queue is: " );
while ( currentPtr != NULL )
{
printf( "%d <-- ", currentPtr->cust_n );
currentPtr = currentPtr->nextPtr;
}
printf( "NULL " );
}
}