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

i need help fixing a simulation program of the lines at a grocery store. the pro

ID: 3676736 • Letter: I

Question

i need help fixing a simulation program of the lines at a grocery store.

the program has to do the following instractions but i keep getting errors and i will be thankfull if someone can help get it running fully.

Assume that there are 5 lines at the grocery. 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.

Additional features:  
•   Avoid a line if all lines are certain length.
•   Leave a line if they have waited beyond a certain time.
•   Switch lines if another line is shorter.

code i have so far with errors below.

______________________________________________________________________________________________________________________________

// supermarket simulation
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
struct queueNode {
/* self-referential structure */
int cust_n;
int serv_t;
int arr_t;
struct queueNode* nextPtr;
};
typedef struct queueNode customer;
typedef customer* customerPtr; /* function prototypes */

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; // track global time in secs
int t_arrival = 0; // track arrival time
int t_depart = -1; // track departure and hence time needed to serve last customer
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) {
cout << "TIME =" << t_time << " ,MAX_SERV_TIME = " << MAX_SERV_TIME << "TOT WAIT TIME = " << tot_wait_t;
if(t_time == t_depart && !isEmpty(headptr)) {
// its time to depart and list isn't empty
cout << "customer " << headptr->cust_n << endl << endl;
serv_time = headptr->serv_t; // present customer's service time
tot_wait_t += (t_time - headptr->arr_t);
dequeue(&headptr, &tailptr); // customer 'n' gets serviced
if(!isEmpty(headptr))
t_depart = serv_time + t_time; // cutomer 'n+1' will get serviced at
}
if(t_time == t_arrival) {
// customer arrives
customer_n++;
serv_time = rand() % 4 + 1; // get service time
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); // customer joins the Q
t_arrival += rand() % 4 + 1;
cout << "customer " << tailptr->cust_n << " joins at " << tailptr->arr_t << " serv_time " << tailptr->serv_t
<< " new_cust " << t_arrival;
}
printQueue(headptr);
getchar();
t_time++;
}
customer_n = (tailptr->cust_n - headptr->cust_n) + 1; // total no. of customers unserved
cout << "average wait time = " << tot_wait_t / customer_n << " max service time = " << MAX_SERV_TIME
<< " tot custs unserved " << customer_n;
for(i = 0; i < customer_n; i++) {
// puts("TEST");
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
cout << "No memory available." << endl;
}

void dequeue(customerPtr* headPtr, customerPtr* tailPtr)
{
// char value;
customerPtr tempPtr;
// value = ( *headPtr )->data;
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)
cout << "Queue is empty." << endl << endl;
else {
cout << "The queue is:" << endl;
while(currentPtr != NULL) {
cout << currentPtr->cust_n << " <-- ";
currentPtr = currentPtr->nextPtr;
}
cout << "NULL" << endl << endl;
}
}

Explanation / Answer

Please find the fixed code :

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
struct queueNode {
/* self-referential structure */
int cust_n;
int serv_t;
int arr_t;
struct queueNode* nextPtr;
};
typedef struct queueNode customer;
typedef customer* customerPtr; /* function prototypes */
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; // track global time in secs
int t_arrival = 0; // track arrival time
int t_depart = -1; // track departure and hence time needed to serve last customer
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) {
cout << "TIME =" << t_time << " ,MAX_SERV_TIME = " << MAX_SERV_TIME << "TOT WAIT TIME = " << tot_wait_t;
if(t_time == t_depart && !isEmpty(headptr)) {
// its time to depart and list isn't empty
cout << "customer " << headptr->cust_n << endl << endl;
serv_time = headptr->serv_t; // present customer's service time
tot_wait_t += (t_time - headptr->arr_t);
dequeue(&headptr, &tailptr); // customer 'n' gets serviced
if(!isEmpty(headptr))
t_depart = serv_time + t_time; // cutomer 'n+1' will get serviced at
}
if(t_time == t_arrival) {
// customer arrives
customer_n++;
serv_time = rand() % 4 + 1; // get service time
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); // customer joins the Q
t_arrival += rand() % 4 + 1;
cout << "customer " << tailptr->cust_n << " joins at " << tailptr->arr_t << " serv_time " << tailptr->serv_t
<< " new_cust " << t_arrival;
}
printQueue(headptr);
getchar();
t_time++;
}
customer_n = (tailptr->cust_n - headptr->cust_n) + 1; // total no. of customers unserved
cout << "average wait time = " << tot_wait_t / customer_n << " max service time = " << MAX_SERV_TIME
<< " tot custs unserved " << customer_n;
for(i = 0; i < customer_n; i++) {
// puts("TEST");
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 = static_cast<queueNode*>(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
cout << "No memory available." << endl;
}
void dequeue(customerPtr* headPtr, customerPtr* tailPtr)
{
// char value;
customerPtr tempPtr;
// value = ( *headPtr )->data;
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)
cout << "Queue is empty." << endl << endl;
else {
cout << "The queue is:" << endl;
while(currentPtr != NULL) {
cout << currentPtr->cust_n << " <-- ";
currentPtr = currentPtr->nextPtr;
}
cout << "NULL" << endl << endl;
}
}