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

#include <stdio.h> #include <stdlib.h> #include <time.h> typedef struct customer

ID: 3626019 • Letter: #

Question

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct customerStruct
{
int numItems;
int processTime;//(item process time * num items)
int waitTime;
struct customerStruct *next;
}client;

typedef struct lines
{
struct customerStruct *first;
struct customerStruct *last;
int served;//number of customers handled
int sale;//amount of items sold
int totalProcessTime;//amount of time it has taken for customers to be processed
int totalWait;//total customer wait time
}lines;

struct settings
{
int runTime;// total run time in seconds.
int maxCustomerInterval;// max time it takes for next customer to show up.
int completeTime;// time it takes to complete one order
int regLane;// amount of registers (4 or 5)
int itemMax;// max customer items
int expLane;// amount of express lanes (1 or 0)
int expMax;// max items in express lane
int seed;// seed value
};

int menu(struct settings *simulation);
int newCustomer(lines *line, struct settings *simulation);
client* add(client *lastCustomer, int items);
void processCustomers(lines *line, struct settings *simulation);
// This function has two behaviors. If mode is 1, the function
// will return the amount of people in the line that is passed.
// If the mode is 0, the function will return a 1 to indicate that
// there are still people waiting in line. Mode 1 is used in newCustomer
// and mode 0 with in the main simulation loop.
int customerCheck(lines *line, int mode);

int main()
{ // Simulation will hold all settings.
struct settings simulation;

// Create the lines that will serve the clients.
// Mode 1: 4 registers 1 express.
// Mode 2: 5 registers 0 express
lines *line = (lines*)malloc(5*sizeof(line));
// Initialize the declared lines.
int i;
for(i=0;i<5;i++)
{
line[i].first = line[i].last = NULL;
line[i].sale = line[i].served = line[i].totalProcessTime = line[i].totalWait = 0;
}
// The total time in which the simulation will run.
// This time may become larger than the run time in order
// to serve remaining customers have store closes.
// This value is declared outside the while since it will be used
// for computing the output.
int totalTime = 0;// Period that simulation has been running.
// Program loop.
while(menu(&simulation) == 1)
{
int timeNextCustomer = rand()%simulation.maxCustomerInterval; // interval of when next customer will arrive.

int bonusCustomer; // Incase the arrival of next customer will be 0
if(timeNextCustomer == 0)
bonusCustomer = 1;
else
bonusCustomer = 0;

int customerQueued = 0;
// Simulation loop.
while((simulation.runTime > totalTime) || (customerQueued > 0))
{
// Will create new customer so long as there is enough simulation time, and the count down to next customer is zero.
while((timeNextCustomer == 0)&&(simulation.runTime > totalTime))
{
// This will handle the bonus customer.
for(i=0;i<=bonusCustomer;i++)
{
// Function that does the actual adding. Passes in all lines.
// If it returns a 1, there was an error allocating memory,
// program wil quit.
if(newCustomer(line, &simulation))
return 1;

// Set up next customer interval
timeNextCustomer = rand()%simulation.maxCustomerInterval;
// Just in case it is zero again, this will cause for loop once more.
// Note that for loop will not leave until timeNextCustomer is greater than zero.
if(timeNextCustomer == 0)
bonusCustomer += 1;
}
// Reset the bonus customer.
bonusCustomer = 0;
}

// Simulate the check out line. Passes in all lines, and simulation settings.
processCustomers(line, &simulation);

// Determine if there is any customers left.
// If so increment current time, and count down until the next customer arrives.
// Otherwise if there is no customers in the store
// then there is no need to cycle through processCustomers,
// we can just add to the time for the next customer.
customerQueued = customerCheck(line, 0);
if((customerQueued == 0) && simulation.runTime >= totalTime)
{
totalTime += timeNextCustomer;
timeNextCustomer = 0;
}
else
{
// Determine if it is necessary to subtrack from timeNextCustomer.
// Note that after store closing no more customers will arrive.
if(simulation.runTime >= totalTime)
timeNextCustomer--;
// Increment time by one second.
totalTime++;
}
}
// Display output.
int totalCustomers = 0;
int totalSale = 0;
float overallWait = 0;
float overallProcess = 0;
int d = 0;
while(d < 3)
{
float hours = totalTime/3600;
if(d == 0)
printf(" Customers Served per Hour ");
if(d == 1)
printf(" Average Waiting Time per Customer (minutes) ");
if(d == 2)
printf(" Average Number of Items Processed (hour) ");

for(i=0;i<5;i++)
{
if(d == 0)
printf(" Line %d: %f", i, line[i].served/hours);
if(d == 1)
{
float minWait = line[i].totalWait/60;
printf(" Line %d: %f", i, minWait/line[i].served);
}
if(d == 2)
printf(" Line %d: %f", i, line[i].sale/line[i].served);

totalCustomers += line[i].served;
totalSale += line[i].sale;
overallWait += line[i].totalWait;
overallProcess += line[i].totalProcessTime;
}
}
printf(" Total Customers Served: %d", totalCustomers);
printf(" Total Items Sold: %d", totalSale);
// Convert to minutes
overallWait = overallWait/60;
printf(" Total Wait per Customer (minutes): %f", overallWait/totalCustomers);
printf(" Total Process Time per Customer (minutes): %f", overallProcess/totalCustomers);
}// End of Program Loop
return 0;
}// End of main.

int menu(struct settings *simulation)
{
printf("Johnson Center Food Court ");
printf(" Select the simulation mode. ");
printf("1. Four registers, one express lane. ");
printf("2. Five registers, zero express lane. ");
printf("3. Quit. ");

int run = 1;
while(run)
{
int decision = 0;
printf(" Enter your choice: ");
scanf("%d", &decision);

switch(decision)
{
case 1:
simulation->regLane = 4;
simulation->expLane = 1;
run = 0;
break;
case 2:
simulation->regLane = 5;
simulation->expLane = 0;
run = 0;
break;
case 3:
return 0;
break;
default:
printf(" Wrong value! Try again... ");
break;
}
}

run = 1;
while(run)
{
int correct = 0;

printf(" Enter run time (in hours): ");
scanf("%d", &simulation->runTime);

if(simulation->runTime >= 1)
{
simulation->runTime = simulation->runTime*3600;
correct++;
}
else
printf(" Run time needs to be at least 1!");

if(correct == 1)
{
printf(" Enter max customer interval (in seconds): ");
scanf("%d", &simulation->maxCustomerInterval);
if(simulation->maxCustomerInterval >= 1)
correct++;
else
printf(" Customer time interval needs to be at least 1!");
}

if(correct == 2)
{
printf(" Enter time it takes to process one item (in seconds): ");
scanf("%d", &simulation->completeTime);
if(simulation->completeTime >= 1)
correct++;
else
printf(" Item process time needs to be at least 1!");
}

if(correct == 3)
{
printf(" Enter max customer items: ");
scanf("%d", &simulation->itemMax);
if(simulation->itemMax >= 1)
correct++;
else
printf(" Customer items needs to be at least 1!");
}

if(correct == 4)
{
if(simulation->expLane == 1)
{
printf(" Enter max express lane items: ");
scanf("%d", &simulation->expMax);
if(simulation->expMax >= 1)
correct++;
else
printf(" Max express lane items needs to be at least 1!");
}
else
correct++;
}

if(correct == 5)
{
printf(" Enter seed value: ");
scanf("%d", &simulation->seed);
if(simulation->seed >= 0)
{
correct++;
srand(simulation->seed);
}
else
printf(" Seed needs to be at least zero!");
}

if(correct == 6)
run = 0;
else
printf(" Please try again! ");
}
return 1;
}

int newCustomer(lines *line, struct settings *simulation)
{
// Give customer amount of items to order, add 1 to avoid 0.
int items = rand()%simulation->itemMax+1;
// Check to see if there is an express lane, and if customer is allowed
// to go to it.
if((simulation->expLane == 1) && (items <= simulation->expMax))
{
// Add to express lane
line[4].last = add(line[4].last, items);
if(line[4].last == NULL)
return 1;
else
return 0;
}
// Add to shortest regular lane.
else
{
// The variable customers will be used to compare the amount of customers
// in each line. It is being intialized here so that the following
// if statement (in the for loop), functions properly.
int customers = customerCheck((line+0), 1);
// The shortestLine variable keeps track of which line has the least
// amount of people.
int shortestLine;
int i;
for(i=0;i<simulation->regLane;i++)
{
// CustomerCheck will check each line.
if(customers >= customerCheck((line+i), 1))
shortestLine = i;
}
// Add customer to line.
line[shortestLine].last = add(line[shortestLine].last, items);
// If this is the first person in the line.
if(line[shortestLine].first == NULL)
line[shortestLine].first = line[shortestLine].last;

if(line[shortestLine].last == NULL)
return 1;
else
return 0;
}
}

int customerCheck(lines *line, int mode)
{
// Variable to hold the amount of customers.
int customers = 0;
// Grabbing the last person in line.
client *customer = line->last;
// Mode 1 will count everyone in the particular line that was given.
if(mode == 1)
{
// If there is no customers, return 0;
if(customer == NULL)
return 0;
else
{
// Counting the current analyzed customer.
customers++;
// Check and count any other customers.
while(customer->next != NULL)
{
customers++;
customer = customer->next;
}
return customers;
}
}
// Assumed mode zero if not one.
else
{
// This mode simply returns a 1 to indicate that there are still customers.
int i;
for(i=0;i<5;i++)
{
customer = line[i].last;
if(customer != NULL)
return 1;
}
// If function did not exit from for loop, then there are no customers.
return 0;
}
}
client* add(client *lastCustomer, int items)
{
// Create a pointer to new allocated space of customer.
client* newCustomer = (client*)malloc(sizeof(client));
if(newCustomer == NULL)
{
printf(" Error! Program failed to allocate memory! ");
printf("Press any key to exit. ");
return NULL;
}
// Link new customer to list.
newCustomer->next = lastCustomer;

// Add items to customer inventory.
newCustomer->numItems = items;

// Return new customer object.
return newCustomer;
}

void processCustomers(lines *line, struct settings *simulation)
{
// Holds current customer that is being updated.
client *customer = NULL;
// Holds the next customer to be served.
client *nextCustomerServe = NULL;
// Goes to each line and update the stats on each customer waiting, and the
// customer being served.
int i;
for(i=0;i<5;i++)
{
// This section handles customers waiting.
customer = line[i].last;
while((customer != line[i].first) && (customer != NULL))
{
customer->waitTime++;
customer = customer->next;
// The following if is added to find the next customer to be served.
// This is convient to know, incase the customer being processed is ready
// to leave, we can easily set this customer to be the new first.
if(customer->next == line[i].first)
nextCustomerServe = customer;
}
// This section handles first customer in line that is being processed.
customer = line[i].first;
if(customer != NULL)
{
// Note that adding before checking will not be a problem since
// completeTime and numItems are never zero.
customer->processTime++;

// If the process time is equal to the theoretical amount of time
// it takes to process the total order, then the customer has
// been served, and can be removed, allowing the next customer
// to be processed.
if(customer->processTime == (simulation->completeTime*customer->numItems))
{
line[i].served++;
line[i].sale += customer->numItems;
line[i].totalProcessTime += customer->processTime;
line[i].totalWait += customer->waitTime;
// If we are about to remove the only customer in line.
if(customer == line[i].last)
{
line[i].first = line[i].last = NULL;
free(customer);
}
else
{
line[i].first = nextCustomerServe;
free(customer);
}
}
}// End of if (section handles first in line)
}// End of for loop
}// End of proccessCustomer






Explanation / Answer

}

bonusCustomer = 0;

}

}

return 0;

}

int menu(struct settings *simulation)

{

int run=1;

printf("Johnson Center Food Court ");

printf(" Select the simulation mode. ");

printf("1. Four registers, one express lane. ");

printf("2. Five registers, zero express lane. ");

simulation->regLane = 4;

simulation->expLane = 1;

run = 0;

break;

case 2:

simulation->regLane = 5;

simulation->expLane = 0;

run = 0;

break;

case 3:

return 0;

default:

printf(" Wrong value! Try again... ");

break;

}

}

run = 1;

while(run)

{

int correct = 0;

printf(" Enter run time (in hours): ");

{

simulation->runTime = simulation->runTime*3600;

correct++;

}

else

{

printf(" Enter max customer interval (in seconds): ");

scanf("%d", &simulation->maxCustomerInterval);

if(simulation->maxCustomerInterval >= 1)

correct++;

else

printf(" Customer time interval needs to be at least 1!");

}

if(correct == 2)

{

printf(" Enter time it takes to process one item (in seconds): ");

scanf("%d", &simulation->completeTime);

if(simulation->completeTime >= 1)

correct++;

else

printf(" Item process time needs to be at least 1!");

}

if(correct == 3)

{

printf(" Enter max customer items: ");

scanf("%d", &simulation->itemMax);

if(simulation->itemMax >= 1)

correct++;

else

printf(" Customer items needs to be at least 1!");

}

if(correct == 4)

{

if(simulation->expLane == 1)

{

printf(" Enter max express lane items: ");

scanf("%d", &simulation->expMax);

if(simulation->expMax >= 1)

correct++;

else

printf(" Max express lane items needs to be at least 1!");

}

else

correct++;

}

if(correct == 5)

{

printf(" Enter seed value: ");

scanf("%d", &simulation->seed);

if(simulation->seed >= 0)

{

correct++;

srand(simulation->seed);

}

else

printf(" Seed needs to be at least zero!");

}

if(correct == 6)

run = 0;

else

printf(" Please try again! ");

}

return 1;

{

int items = rand()%simulation->itemMax+1;

if((simulation->expLane == 1) && (items <= simulation->expMax))

{

line[4].last = add(line[4].last, items);

if(line[4].last == NULL)

return 1;

else

return 0;

}

else

{

int customers = customerCheck((line+0), 1);

int shortestLine;

int i;

for(i=0;i<simulation->regLane;i++)

{

if(customers >= customerCheck((line+i), 1))

shortestLine = i;

}

line[shortestLine].last = add(line[shortestLine].last, items);

if(line[shortestLine].first == NULL)

line[shortestLine].first = line[shortestLine].last;

if(line[shortestLine].last == NULL)

return 1;

else

return 0;

}

}

int customerCheck(lines *line, int mode)

{

int customers = 0;

client *customer = line->last;

if(mode == 1)

{

if(customer == NULL)

return 0;

else

{

customers++;

while(customer->next != NULL)

{

customers++;

customer = customer->next;

}

return customers;

}

}

{

int i;

for(i=0;i<5;i++)

{

customer = line[i].last;

if(customer != NULL)

return 1;

}

return 0;

}

}

client* add(client *lastCustomer, int items)

{

client* newCustomer = (client*)malloc(sizeof(client));

if(newCustomer == NULL)

{

printf(" Error! Program failed to allocate memory! ");

printf("Press any key to exit. ");

return NULL;

}

newCustomer->next = lastCustomer;

newCustomer->numItems = items;

return newCustomer;

}

void processCustomers(lines *line, struct settings *simulation)

{

client *customer = NULL;

client *nextCustomerServe = NULL;

int i;

for(i=0;i<5;i++)

{

customer = line[i].last;

while((customer != line[i].first) && (customer != NULL))

{

customer->waitTime++;

customer = customer->next;

if(customer->next == line[i].first)

nextCustomerServe = customer;

}

customer = line[i].first;

if(customer != NULL)

{

customer->processTime++;

if(customer->processTime == (simulation->completeTime*customer->numItems))

{

line[i].served++;

line[i].sale += customer->numItems;

line[i].totalProcessTime += customer->processTime;

line[i].totalWait += customer->waitTime;

if(customer == line[i].last)

{

line[i].first = line[i].last = NULL;

free(customer);

}

else

{

line[i].first = nextCustomerServe;

free(customer);