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

USING C (NOT C++ or C SHARP) Write a program to simulate two lines in a grocery

ID: 3667306 • Letter: U

Question

USING C (NOT C++ or C SHARP)

Write a program to simulate two lines in a grocery store, which will allow us to better understand how to select the fastest line.

For this assignment you will need to simulate two lines using queues. This will require that you develop enqueue ( ) (insert), dequeue ( ) (delete), and printQueue ( ) operations for a queue. Although you will instantiate two queues, each one of these will consist of the same kind of queue nodes. Define a queue node in the following manner:

typedef struct queueNode

{

     int customerNumber; // Unique identifier; starts at 1; after 24 hours should be reset to 1

     int serviceTime;   // Random time; varies between express and normal lanes; units in minutes

     int totalTime;     // totalTime = serviceTime + sum of serviceTimes of customers in line before this customer; units in minutes

     struct queueNode *pNext;

} QueueNode;

One of your queues will represent the express lane and the other a normal lane. You will randomly generate arrival times and service times of customers into each lane. The express lane has customers arrive every one to five minutes, and customers arrive every three to eight minutes in the normal lane. Service times vary from one to five minutes, and three to eight minutes, for express and normal lane customers, respectively. As customers arrive into each line print out a message indicating in which line each customer arrives, along with the overall corresponding arrival time and customer number. When customers have finished checking out, print out a message indicating which line the customer was in, along the corresponding customer number and totalTime in the line. Allow for the simulation to run for nnumber of minutes, where n is inputted by the user.

The general program flow is as follows:

1. Generate a random number between 1 – 5 and 3 – 8 for express and normal lanes, respectively. This represents the arrival time of the first customer into each lane. Set the variable for total time elapsed to 0.

2. As customers arrive into each line, randomly generate a service time for each. Start processing the customers in the lanes based on service time. Randomly generate the arrival time of the next customer into each line. Elapsed time should be updated by one unit.

3. As each minute elapses, a new customer may arrive and/or another customer may be done checking out. Display the appropriate messages as described above.

4. For every 10 minutes, print out the entire queue for each line

5. Repeat steps 2 through 4 for n minutes of simulation.

Hints: since this is a simulation one minute is really one unit of time. Thus, the incrementing of an integer variable could represent one minute of time elapsing.

Explanation / Answer

main.c

#include "File_Header.h" // Inclusion of File Header
int main(){

   // Initialization of variables
   Queue Express_Queue = { NULL, NULL };
   Queue Normal_Queue = { NULL, NULL };
   int sim_length = 0, count_cust = 0, exp_service = 0, norm_service = 0,
       express_time = 0, normal_time = 0, deexpress_hold = 0,
       denormal_hold = 0, express_count = 0, normal_count = 0, i = 0;
  
   // Introduce the user to the program and detail a short description
   // of what this program will try to accomplish.
   printf("Welcome to the Grocery Store Simulation ");
   printf(" This program is intended to simulate the experience ");
   printf("of being at a grocery store. Customers will be randomly selected ");
   printf("and placed in express and normal checkout lanes. This simulation ");
   printf("will run for as long as you would like. ");
   // Pause to allow user to read the information.
   system("pause");
   system("cls"); // Clear screen

   // Ask the user how long they would like to run the simulation. The iterator will be stored.
   printf("Please enter how long you would like this simulation to run (in minutes): ");
   scanf("%d", &sim_length);
   system("cls"); // Clear screen.

   /* We intend to iterate through this process until it is complete.
   We will place a customer in a Que or Grocery Lane, display a message as to
   what lane the customer was placed in. Then iterate through a few more steps
   until their service time has been completed.*/

   for (i = 0; i < sim_length; i++){

       count_cust += 1; // assign customers a number 1 through 24

       if (count_cust == 25){ // When the 25th customer appears the count will be reset for
           count_cust = 0;    // those customers.
       }

       exp_service = (rand() % 5) + 1;       // This is essentially how the lanes will be give a que time and then subsuquentily dequed
       norm_service = (rand() % 5) + 3;   // The service time is given a random number 1 - 5 for express lanes and 3 - 8 for normal lanes.

       // to print the Que of each lane we will need to wait until 10mins. has passed.
       // Once 10 mins has passed we will print out the que for each lane.
       if (i >= 1){
           if ((i % 10) == 0){
               printf(" The Express Lane currently has: ");
               printQue(Express_Queue); // Printing of Express Lane
               printf(" ");
               printf(" The Normal Lane currently has: ");
               printQue(Normal_Queue); // Printing of Normal Lane.
               printf(" ");
           }
       }
      
       express_time += exp_service; // We would also like a collection of the total time of service of each customer in the lane.
                                   // This assign essentially performs that operation.

       // I place an identifier string literal to show the user which process has placed a customer in a lane.
       printf("The Process: ");
       // Display the customer information to the user to verify simulation results.
       printf("Customer %d has been placed into the Express Lane with Service Time %d at Time %d ", count_cust, exp_service, i);
       // Push he information we have just gathered from this iteration to the Que of the Express Lane.
       Enqueue(&Express_Queue, count_cust, exp_service, express_time);
      
       normal_time += norm_service; // Again, push a total collective time to be placed within the Que.
       printf("The Process: "); // Identifier.
       printf("Customer %d has been placed into a Normal Lane with Service Time %d at Time %d ", count_cust, norm_service, i); // Display iterative process of the information collected.
       Enqueue(&Normal_Queue, count_cust, norm_service, normal_time); // Push the infromation from the Normal Lane to the Normal Lane Que.
      
       express_count++; // Iterative step to check the service time in each Lane.
       normal_count++; // This is required for the comparison to release a customer from a lane.

       // The check will be in place to not only check if the customer can be released if their service time has been completed
       // but will also check if their is a customer in the lane at the time of release.
       if ((Express_Queue.pHead != NULL) && (Express_Queue.pHead->serviceTime == express_count)){
           express_count = 0;   // reset the express count so that it will process and release the next customer once the service time has been reached.
           deexpress_hold = deQueue(&Express_Queue); // return which customer was released from the lane from the deQue function.
           printf(" Customer %d has finished their service in the Express Lane ", deexpress_hold); // Display which customer was released.
       }

       // Again, same check - this might be more useful if the random number generation where synced to only one lane.
       if ((Normal_Queue.pHead != NULL) && (Normal_Queue.pHead->serviceTime == normal_count)){
           normal_count = 0; // reset the express count so that it will process and release the next customer once the service time has been reached.
           denormal_hold = deQueue(&Normal_Queue); // Gather customer information from time of release.
           printf(" Customer %d has finished their service in the Normal Lane ", denormal_hold); // Display Customer that has finished thier service.
       }
   }

   // For the end of the simulation I wanted to pause so that the user can look
   // over the available information that was given in this run.
   system("pause");
   printf(" The simulation has ended "); // Display identifier that that sim has ended.
   return 0;
}

File_Header.h

#ifndef FILE_HEADER // File Header 'lock'.
#define FILE_HEADER

// inclusion of preprocessor directives.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Creation of Queue Node for the storage of data nodes.
typedef struct queueNode
{
   int customerNumber;
   int serviceTime;
   int totalTime;
   struct queueNode *pNext;
}QueueNode;

// Creation of Que with two nodes that point to both the header
// and tail of the Que.
typedef struct queue
{
   QueueNode *pHead;
   QueueNode *pTail;
}Queue;

// Function prototypes initialized here.
// Healper function makeNode for memory allocation.
QueueNode *makeNode(int cust_num, int serv_time, int total_time);
// Enqueue function to add nodes to a Que.
void Enqueue(Queue *pLane, int cust_num, int serv_time, int total_time);
// Printing the contents of the Que.
void printQue(Queue pLane);
// Deallocate and remove nodes from the Que.
int deQueue(Queue *pLane);

#endif // End of 'Lock'.

Function.c

#include "File_Header.h" // Inclusion of header file.

// Makenode helper function to allocate dynamic memory for use in the Enqueue function call.
QueueNode *makeNode(int cust_num, int serv_time, int total_time)
{
   // Initialization of variables.
   QueueNode *pMem = NULL;

   pMem = (QueueNode *)malloc(sizeof(QueueNode)); // create the QueueNode which is creating space in memory.

   // Should the memory allocation be successful we will assign data given from
   // main into this node.
   if (pMem != NULL){
       // Assigning data from main to node.
       pMem->customerNumber = cust_num;
       pMem->serviceTime = serv_time;
       pMem->totalTime = total_time;
       pMem->pNext = NULL; // Node will point to NULL unless new node is aquired.
   }
   else{
       // Display to user if memory was not able to be allocated that something went wrong.
       printf("MEMORY ALLOCATION ERROR -- unable to allocate enough memory space. ");
   }

   return pMem; // return the Node.
}

// Function to place node in a Que or grocery lane in this case.
void Enqueue(Queue *pLane, int cust_num, int serv_time, int total_time){
  
   // Initialization of variables.
   QueueNode *pMem = NULL;

   pMem = makeNode(cust_num, serv_time, total_time); // using the makeNode function call we allocate memory for this node.

   // Should memory be allocated we will create checks to place the node in the
   // appropriate location.
   if (pMem != NULL)
   {
       // Should the list be empty we will place the node in the Head and Tail
       // which will both be in the same position (the Head).
       if ((pLane->pHead) == NULL){
           pLane->pHead = pMem;
           pLane->pTail = pMem;
       }
       else{
           // Should the Que not be empty, we will place the new node at the tail
           // section of the que and assign the tail to the new node. FIFO
           pLane->pTail->pNext = pMem;
           pLane->pTail = pMem;
       }
   }
}

// The print function, this will allow us to print the list every 10 mins.
void printQue(Queue pLane){

   // Initialize variables. May not be needed in this case but better to be safe.
   QueueNode *pCur = NULL;

   pCur = pLane.pHead; // Assign the Pointer Node to the head of the Que.

   // While the head is not empty, display important information from node then assign
   // head node to head nodes next node.
   while (pCur != NULL)
   {
       printf("Customer Number %d, Service Time %d minutes, Total Time in lane %d minutes ", pCur->customerNumber, pCur->serviceTime, pCur->totalTime); // Display.
       pCur = pCur->pNext; // Assign head node to head's next node.
   }
}

// The deallocation of memory from the Que and the subsuqent loss of a customer from a lane.
int deQueue(Queue *pLane){

   // Initialization of variables.
   QueueNode *pTemp = NULL;
   int dataHold = 0; // variable to hold customer number that was released.

   // We are assuming the lane or Que is not empty.
   // This assumption is solely based on the programmer's intuition.
   if (pLane != NULL){

       // Using the temp pointer, place the que head in the temp pointer
       // assign the Que head to Que head's next node. Place the customer
       // number to be returned in the data holder.
       pTemp = pLane->pHead;
       pLane->pHead = pLane->pHead->pNext;
       dataHold = pTemp->customerNumber;

       // Should the head of the Que be in the same position as the tail
       // we should assign the tail to null so that no Que position pointer
       // still points to data that has been deallocated.
       if ((pTemp) == (pLane->pTail)){
           (pLane->pTail) = NULL;
       }

       free(pTemp); // Free the head node which is FIFO.
   }

   return dataHold; // return data holder which holds the contents of customer #.
}

Sample Output
                                                                                                                              
Please enter how long you would like this simulation to run (in minutes):                                                                                   
5                                                                                                                                                           
sh: cls: command not found                                                                                                                                  
The Process:                                                                                                                                                
Customer 1 has been placed into the Express Lane with Service Time 4 at Time 0                                                                              
The Process:                                                                                                                                                
Customer 1 has been placed into a Normal Lane with Service Time 4 at Time 0                                                                                 
The Process:                                                                                                                                                
Customer 2 has been placed into the Express Lane with Service Time 3 at Time 1                                                                              
The Process:                                                                                                                                                
Customer 2 has been placed into a Normal Lane with Service Time 3 at Time 1                                                                                 
The Process:                                                                                                                                                                                                                                                                                                  
Customer 3 has been placed into the Express Lane with Service Time 4 at Time 2                                                                              
The Process:                                                                                                                                                
Customer 3 has been placed into a Normal Lane with Service Time 3 at Time 2                                                                                 
The Process:                                                                                                                                                
Customer 4 has been placed into the Express Lane with Service Time 2 at Time 3                                                                              
The Process:                                                                                                                                                
Customer 4 has been placed into a Normal Lane with Service Time 5 at Time 3                                                                                 
                                                                                                                                                            
Customer 1 has finished their service in the Express Lane                                                                                                   
Customer 1 has finished their service in the Normal Lane                                                                                                    
The Process:                                                                                                                                                
Customer 4 has been placed into a Normal Lane with Service Time 5 at Time 3                                                                                 
                                                                                                                                                            
Customer 1 has finished their service in the Express Lane                                                                                                   
Customer 1 has finished their service in the Normal Lane                                                                                                    
                                                                                                                                                            
The Process:                                                                                                                                                
Customer 5 has been placed into the Express Lane with Service Time 5 at Time 4                                                                              
The Process:                                                                                                                                                
Customer 5 has been placed into a Normal Lane with Service Time 4 at Time 4                                                                                 
sh: pause: command not found                                                                                                                                
                                                                                                                                                            
The simulation has ended