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

In C++! Write a program that simulates a checkout line at a supermarket. The lin

ID: 3801319 • Letter: I

Question

In C++! Write a program that simulates a checkout line at a supermarket. The line is a queue object. Customers (i.e. customer objects) arrive in random integer intervals of 1-4 minutes, also, each customer is served in random integers intervals of 1-4 minutes. Obviously, the rates need to be balanced. If the average arrival rate is larger than the average service rate, the queue will grow infinitely. Even with balanced rates, randomness can still cause long lines. Run the supermarket simulation for a 2-hour period (120 minutes) using the following algorithm:

I wouls also appreciate it if you could use queue as an array and not stacks.

1). Choose a random integer from 1 to 4 to determine the minute at which the first customer arrives

2). At the first customer’s arrival time:

a) Determine customer’s service time

b) Begin servicing the customer;

c) Schedule arrival time of next customers

3). For each minute of the day

a) If the next customer arrives, Say so, enqueue the customer, and schedule the arrival time of the next customer.

b) If the services was completed for the last customer, Say so, dequeue next customer to be serviced and determined customer’s service completion time (random integer 1 – 4 added to the current time).

Now run your simulation for 120 minutes, and answer each of the following:

a. What is the maximum number of customers in the queue at any time?

b. What is the longest wait any one customer experiences?

c. What happens if the arrival interval is changed from 1-4 minutes to 1-3 minutes?

Please upload the following:

The class .cpp file

The main program

The class .h file

Output File

Answers to the Questions Above

Explanation / Answer

#include <iostream>

#include <ctime>

#include<stdlib.h>

//#include<conio.h>

using namespace std;

struct queueNode {   /* self-referential structure */

    int data;

    struct queueNode *nextPtr;

};

typedef struct queueNode QueueNode;

typedef QueueNode *QueueNodePtr;

/* function prototypes */

void CustQueue( QueueNodePtr );

int isEmpty( QueueNodePtr );

char Out_queue( QueueNodePtr *, QueueNodePtr * );

void Into_queue( QueueNodePtr *, QueueNodePtr *, int );

int main() {

   

   

   

    int Total_time= 0;

    int Arrival_t= 0;             // arrival time

    int Departure_t = 0;    // departure and hence time needed to serve last customer

    int Cust_num = 0;

    QueueNodePtr startptr = NULL,endptr = NULL;

    srand((unsigned int)time(NULL));

    while(Total_time<= 120) {

        if(Total_time== Arrival_t) { //customer arrives

            Cust_num++;

            Into_queue(&startptr,&endptr,Cust_num); //customer joins the Q

            Arrival_t+= rand()%4 + 1;

            cout<<" time" << Total_time<< "customer" <<Cust_num<< "joins Q, next customer scheduled for " << Arrival_t<< endl;

            CustQueue(startptr);

    //        getchar();

        }

        if(Total_time== Departure_t) {

            if(!isEmpty(startptr)) {

   

                Out_queue(&startptr,&endptr); //customer gets serviced               

                Departure_t += rand()%4 + 1;                    //time customer leaves service

                cout<<"time = " << t_time<< " ,customer " <<Cust_num << "ON, next service session at "<<Departure_t<<"sec"<<endl ;

            }

            else {                                                                         //if Q empty schedule service at a later time

                Departure_t += rand()%4 + 1;

                cout<<"time = "<<t_time<< ",next service session at "<<Departure_t<<"sec"<<endl ;

            }                                 

                    CustQueue(startptr);

      //      getchar();

        }

        t_time++;

    }

    CustQueue(startptr);

    return 0;

}

void Into_queue( QueueNodePtr *startptr, QueueNodePtr *endptr,int value )

{

    QueueNodePtr newPtr;

    newPtr = ( struct queueNode * ) malloc( sizeof( QueueNode ) );

    if ( newPtr != NULL ) {

       newPtr->data = value;

        newPtr->nextPtr = NULL;

        if ( isEmpty( *startptr ) )

            *startptr = newPtr;

        else

            ( *endptr )->nextPtr = newPtr;

        *endptr = newPtr;

    }

    else

        cout<< value<<" not inserted. No memory available. " <<endl ;

}

char Out_queue( QueueNodePtr *startptr, QueueNodePtr *endptr )

{

    char value;

    QueueNodePtr tempPtr;

    value = ( *startptr )->data;

    tempPtr = *startptr;

    *startptr = ( *startptr )->nextPtr;

    if ( *startptr == NULL )

        *endptr = NULL;

    free( tempPtr );

    return value;

}

int isEmpty( QueueNodePtr startptr )

{

    return startptr == NULL;

}

void CustQueue( QueueNodePtr currentPtr )

{

    if ( currentPtr == NULL )

        cout<<"Queue is empty" << endl;

    else {

        cout<<"The queue is: " <<endl ;

        while ( currentPtr != NULL ) {

            cout<< currentPtr->data<<" <-- " ;

            currentPtr = currentPtr->nextPtr;

        }

        cout<<"NULL " <<endl ;

    }

}