Write a simulation program to simulate lines at a grocery store for a typical da
ID: 3601395 • Letter: W
Question
Write a simulation program to simulate lines at a grocery store for a typical day’s business. You will assume there are three cashier lines at the grocery store. Customers will enter randomly to check out (this is a time stamp in the input file) and will enter the shortest line. If the lines are equal, then the first line in the queue will be selected. Each transaction takes a random amount of time to complete (this also is a number from the input file).
The grocery store operates on a ten hour business day and only 6 customers per queue will be able to wait for service at any time. If a customer arrives and there are 6 in all the queues, the customer will be turned away.
To perform the simulation, a number, a time stamp, and a service time (randomly generated between 1 and 20 minutes) is given for each customer. This information will be in an input file. The first item in the input file will be the number of customers in the file. To keep the simulation simple, this time stamp will be a count of the number of minutes elapsed from the start of the business day. The time stamps will be in order in the file. So the time could be any values from 0 to 570 (no new business will be allowed 30 minutes before closing, so the waiting queue can be cleared out, hopefully, before the end of the business day.) There will be a time counter which will start at 0 when the simulation starts. No two customers can arrive at the same minute. Customers should be taken out of the queue when the cashier starts serving them.
This simulation will inform the owner how many customers were serviced, how many were turned away, the average time any customer in the queue spent waiting, and the total time customers spent waiting.
You will generate your own input file to test your program that will have a number, a time stamp from 0 to 570 in order, and a service time. Those will be your customers. The grader will use a standard file to test all the projects.
To simulate the time, you will use a loop that increments the “timer” by 1 each time through the loop.
The Results are written on an output file.
The following classes are examples of classes you might need:
class Node
{
};
class Queue
{
};
class Customer
{
int number; // Assign
int arrival_time;
int serv_time;
…
};
class Cashier
{
int timer; // minutes in use
char status; // ‘a’ - available, ‘i’ – inactive
int time_limit; // depends on customer serv_time
...
};
class Sim_data_type
{
int time_cnt; // ranges in values from 0 to 570
int num_serviced; // number of customers serviced so far
long tot_wait_time; // of all customers in the queues
Iit num_turned_away; // count of customers turned away
…
};
Sample data:
1 10 1
2 17 3
3 20 7
4 22 2
5 25 5
.
.
.
30 73 2
Sample output data:
Total number of customers serviced: 35
Total number of customers turned away: 1
Average wait time: 6
Total wait time: 12
Some definitions:
Wait time – is the time the customer enters the queue and does not include the service time.
Arrival time – is the time the customer can go into the queue.
Service time – is how long it takes for a cashier to service a customer. During that time, no other customers can be serviced by that cashier.
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 " );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.