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

C++ PLZ Help Note: parts of this assignment were inspired by Deitel and Deitel’s

ID: 3724157 • Letter: C

Question

C++ PLZ Help

Note: parts of this assignment were inspired by Deitel and Deitel’s Supermarket Simulation problem. We’ve all had the pleasant experience of standing in line at the grocery store. As I’m standing in line I’m always trying to figure out if I chose the line with the fastest service. In most cases, I fail miserably. Let’s 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:

class Data

{

public: // Member functions

private:

     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

}; // This memory needs to be allocated on the heap!

class QueueNode

{

public: // Member functions

private:

     Data *pData;    // The memory for Data will need to be allocated on the heap as well!

     QueueNode *pNext;

};

You must also define a queue in the following manner:

class Queue

{

public: // Member functions

private:

     QueueNode *pHead;

     QueueNode *pTail;

};

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 n number 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.

BONUS:

Modify QueueNode such that it contains a pointer to the start of a dynamic singly linked list. The linked list will consist of grocery items corresponding to one person. These items should be strings like “cereal”, “milk”, “steak”, etc. Adjust the serviceTime of the QueueNode so that it is no longer random, but proportional to the number of items for the person served.

Explanation / Answer

#include "pa4.h"

/****
* Function Name: isEmpty                                                                          *
*                                                                       *
* Description: function checks if queue is empty                                                  *
* Input Parameters: queue                                                                          *
* Returns: 1 if empty 0 if not                                                                      *
* Preconditions: queue is initilized in main                                                      *
* Post Conditions: queue head pointer is checked if null, return 1 if yes                          *
* Test Conditions: NOT TESTED                                                                      *
*****/
int isEmpty(Queue queue)
{
   int status = 0; //False is 0 True is 1

   if (queue.pHead == NULL)
   {
       status = 1;
   }

   return status;
}

/***********************************************************************
* Function Name: initQueue                                                                          *
* Date Created: 2/16/12                                                                              *
* Date Last Modified:2/16/12                                                                      *
* Description: function initializes queue node                                                      *
* Input Parameters: pointer to queue                                                              *
* Returns: NONE                                                                                      *
* Preconditions: address to queue is passed in                                                      *
* Post Conditions: queue head and tail pointer is initialized to NULL                              *
* Test Conditions: NOT TESTED                                                                      *
**************************************************************************************************/
void initQueue(Queue *pQueue)
{
   pQueue ->pHead = NULL;
   pQueue ->pTail = NULL;
}
/**************************************************************************************************
* Function Name: makeQueueNode                                                                      *
*                                                                       *
* Description: function makes a new queue node based on data passed in                              *
* Input Parameters: data in queue node                                                              *
* Returns: pointer to queue node                                                                  *
* Preconditions: data is passed in                                                                  *
* Post Conditions: a node in queue is populated                                                      *
* Test Conditions: NOT TESTED                                                                      *
**************************************************************************************************/
QueueNode *makeQueueNode(QueueNode data)
{
   QueueNode *pTMem = NULL;

   pTMem = (QueueNode *) malloc (sizeof(QueueNode));

   if (pTMem != NULL) //Memory allocated
   {
       pTMem -> customerNumber = data.customerNumber;
       pTMem -> serviceTime = data.serviceTime;
       pTMem -> totalTime = data.totalTime;
       pTMem -> pNext = NULL;
   }

   return pTMem;
}
/**************************************************************************************************
* Function Name: enqueue                                                                          *
*                                                                       *
* Description: function adds new queue to tail of queue/back of queue                              *
* Input Parameters: pointer to queue and data in queue                                              *
* Returns: NONE                                                                                      *
* Preconditions: address to queue is passed in along with data                                      *
* Post Conditions: new queue node is added   to tail end                                              *
* Test Conditions: NOT TESTED                                                                      *
**************************************************************************************************/
void enqueue(Queue *pQueue, QueueNode data)
{
   QueueNode *pTMem = NULL;

   pTMem = makeQueueNode(data);

   if (isEmpty(*pQueue))
   {
       pQueue ->pHead = pTMem;
   }
   else //not empty
   {
       pQueue ->pTail ->pNext = pTMem;
   }

   pQueue ->pTail = pTMem;
  
}
/**************************************************************************************************
* Function Name: dequeue                                                                          *
*                                                                       *
* Description: function takes off queue node form head of queue                                      *
* Input Parameters: pointer to queue                                                              *
* Returns: NONE                                                                                      *
* Preconditions: address to queue is passed in                                                      *
* Post Conditions: head node is freed from memory                                                  *
* Test Conditions: NOT TESTED                                                                      *
**************************************************************************************************/
void dequeue(Queue *pQueue)
{
   QueueNode *pTMem = NULL;

   if (!isEmpty(*pQueue) && (pQueue->pHead == pQueue->pTail)) //Populated Queue with one node
   {
       pTMem = pQueue ->pHead;
       pQueue ->pHead = pQueue ->pHead ->pNext;
       pQueue->pTail = NULL;
       free(pTMem);
   }
   else if (!isEmpty(*pQueue)) //More than one node in queue
   {
       pTMem = pQueue ->pHead;
       pQueue ->pHead = pQueue ->pHead ->pNext;
       free(pTMem);
   }
  
}
/**************************************************************************************************
* Function Name: printQueueRecursive                                                              *
*                                                                       *
* Description: function prints queue                                                              *
* Input Parameters: queue                                                                          *
* Returns: NONE                                                                                      *
* Preconditions: queue is passed in                                                                  *
* Post Conditions: queue is printed in order                                                      *
* Test Conditions: NOT TESTED                                                                      *
**************************************************************************************************/
void printQueueRecursive(Queue queue)
{

   if (!isEmpty(queue))
   {
       printf("Customer: %d ", (queue.pHead)->customerNumber);
       printf("ServiceTimeLeft: %d min ", (queue.pHead) ->serviceTime);
       printf("TotalTime so far: %d min ", (queue.pHead) ->totalTime);
       queue.pHead = queue.pHead ->pNext;
       printQueueRecursive(queue);
   }

}
/**************************************************************************************************
* Function Name: expressLaneQueue                                                                  *
*                                                                       *
* Description: function simiulates queue based on express lane random numbers. It prints the queue*
*               after each new node or loss of node.                                                  *
* Input Parameters: pointer to queue, simulated time scale by user in min                          *
* Returns: NONE                                                                                      *
* Preconditions: address to queue is passed in along with user time                                  *
* Post Conditions: queue is added to and subtracted from until user time is reached                  *
* Test Conditions: NOT TESTED                                                                      *
**************************************************************************************************/
void expressLaneQueue(Queue *pQueue, int userTime)
{
   int randArrivalTime = 0, serviceTime= 0, totalTime = 0;
   int timePassed = 0, custNum = 0, addCount = 0; //addcount = 0 is FALSE
   QueueNode data = {0, 0, 0, NULL};
  
  
   while (timePassed <= userTime)
   {
       addCount = 0; //Reset each add
       randArrivalTime = (rand() % (XPRESS_MAX - XPRESS_MIN + 1) + XPRESS_MIN);
       timePassed = randArrivalTime + timePassed; //Keeps track of customer number based on available service time.
       custNum ++;
       serviceTime = (rand() % (XPRESS_SRV_MAX - XPRESS_SRV_MIN + 1) + XPRESS_SRV_MIN);
       totalTime = serviceTime + totalTime;

       data.customerNumber = custNum;
       data.serviceTime = serviceTime;
       data.totalTime = totalTime;
      
       if(!isEmpty(*pQueue))
       {
              
           if (pQueue->pHead->serviceTime > randArrivalTime) //Person in front not standing long enough to leave yet
           {
               enqueue(pQueue, data);
               addCount = 1; //Node was added
               pQueue->pHead->serviceTime = pQueue->pHead->serviceTime - randArrivalTime; //denotes timepassed while being serviced but not done
           }
           else //Person in front serviced before next addition
           {
               while (pQueue->pHead != NULL && pQueue->pHead->serviceTime <= randArrivalTime) //Keep servicing guy in front till new guy arrives
               {
                   randArrivalTime = randArrivalTime - (pQueue->pHead->serviceTime); //Time less guy already served
                   dequeue(pQueue);
               }
           }

           if (addCount != 1) //No Node added so add next guy
           {
               enqueue(pQueue, data);
           }
       }
       else
       {
           enqueue(pQueue, data);
       }
       printQueueRecursive(*pQueue);      
       system("PAUSE");
       system("cls");
   }
  
}
/**************************************************************************************************
* Function Name: standardLaneQueue                                                                  *
* Description: function simiulates queue based on standard lane random numbers. It prints the      *
*               queue after each new node or loss of node.                                          *
* Input Parameters: pointer to queue, simulated time scale by user in min                          *
* Returns: NONE                                                                                      *
* Preconditions: address to queue is passed in along with user time                                  *
* Post Conditions: queue is added to and subtracted from until user time is reached                  *
* Test Conditions: NOT TESTED                                                                      *
**************************************************************************************************/
void standardLaneQueue(Queue *pQueue, int userTime)
{
   int randArrivalTime = 0, serviceTime= 0, totalTime = 0;
   int timePassed = 0, custNum = 0, addCount = 0; //addcount = 0 is FALSE
   QueueNode data = {0, 0, 0, NULL};
  
  
   while (timePassed <= userTime)
   {
       addCount = 0; //Reset each add
       randArrivalTime = (rand() % (STD_MAX - STD_MIN + 1) + STD_MIN);
       timePassed = randArrivalTime + timePassed; //Keeps track of customer number based on available service time.
       custNum ++;
       serviceTime = (rand() % (STD_SRV_MAX - STD_SRV_MIN + 1) + STD_SRV_MIN);
       totalTime = serviceTime + totalTime;

       data.customerNumber = custNum;
       data.serviceTime = serviceTime;
       data.totalTime = totalTime;
      
       if(!isEmpty(*pQueue))
       {
              
           if (pQueue->pHead->serviceTime > randArrivalTime) //Person in front not standing long enough to leave yet
           {
               enqueue(pQueue, data);
               addCount = 1; //Node was added
               pQueue->pHead->serviceTime = pQueue->pHead->serviceTime - randArrivalTime; //denotes time passed while being serviced but not done
           }
           else //Person in front serviced before next addition
           {
               while (pQueue->pHead != NULL && pQueue->pHead->serviceTime <= randArrivalTime) //Keep servicing guy in front till new guy arrives
               {
                   randArrivalTime = randArrivalTime - (pQueue->pHead->serviceTime); //Time less guy already served
                   dequeue(pQueue);
               }
           }

           if (addCount != 1) //No Node added so add next guy
           {
               enqueue(pQueue, data);
           }
       }
       else
       {
           enqueue(pQueue, data);
       }
       printQueueRecursive(*pQueue);      
       //system("PAUSE");
       //system("cls");
   }


  
}
pa4.h

#ifndef PA4_H
#define PA4_H

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

#define XPRESS_MIN 1
#define XPRESS_MAX 5
#define STD_MIN 1
#define STD_MAX 8
#define XPRESS_SRV_MIN 1
#define XPRESS_SRV_MAX 5
#define STD_SRV_MIN 3
#define STD_SRV_MAX 8


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;

typedef struct queue
{
   QueueNode *pHead;
   QueueNode *pTail;
}Queue;


int isEmpty(Queue queue);
void initQueue(Queue *pQueue);
QueueNode *makeQueueNode(QueueNode data);
void enqueue(Queue *pQueue, QueueNode data);
void dequeue(Queue *pQueue);
void printQueueRecursive(Queue queue);
void expressLaneQueue(Queue *pQueue, int userTime); //Different pointers for each queue remember!!!
void standardLaneQueue(Queue *pQueue, int userTime);


#endif

main.c


#include "pa4.h"

int main(void)
{
   Queue pQueueExpress = {NULL, NULL}, pQueueStd = {NULL, NULL};
   int userTime = 25;


   srand((unsigned)(time(NULL))); //Seed the program for random numbers
      
   printf("Express Lane Sim: ");
   expressLaneQueue(&pQueueExpress, userTime); //Run Express Lan simulation
  

   printf("Standard Lane Sim: ");
   standardLaneQueue(&pQueueStd, userTime); //Run Standard Lan Simulation
   return 0;
}

sample Output


Terminal
ServiceTimeLeft: 3 min                                                                                                                                      
TotalTime so far: 8 min                                                                                                                                     
Customer: 3                                                                                                                                                 
ServiceTimeLeft: 3 min                                                                                                                                      
TotalTime so far: 11 min                                                                                                                                    
Customer: 3                                                                                                                                                 
ServiceTimeLeft: 1 min                                                                                                                                      
TotalTime so far: 11 min                                                                                                                                    
Customer: 4                                                                                                                                                 
ServiceTimeLeft: 6 min                                                                                                                                      
TotalTime so far: 17 min                                                                                                                                    
Customer: 5                                                                                                                                                 
ServiceTimeLeft: 6 min                                                                                                                                      
TotalTime so far: 23 min         

Customer: 5                                                                                                                                                 
ServiceTimeLeft: 5 min                                                                                                                                      
TotalTime so far: 23 min                                                                                                                                    
Customer: 6                                                                                                                                                 
ServiceTimeLeft: 5 min                                                                                                                                      
TotalTime so far: 28 min                                                                                                                                    
Customer: 5                                                                                                                                                 
ServiceTimeLeft: 1 min                                                                                                                                      
TotalTime so far: 23 min                                                                                                                                    
Customer: 6                                                                                                                                                 
ServiceTimeLeft: 5 min                                                                                                                                      
TotalTime so far: 28 min                                                                                                                                    
Customer: 7                                                                                                                                                 
ServiceTimeLeft: 8 min                                                                                                                                      
TotalTime so far: 36 min                                                                                                                                    
sh-4.3$                                             

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote