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

-----------------------USING C++ AND NO QUEUE LIBRARIES OR VECTORS--------------

ID: 3808825 • Letter: #

Question

-----------------------USING C++ AND NO QUEUE LIBRARIES OR VECTORS-----------------------------

------------------------USE YOUR OWN PRIORITY QUEUE CLASS & LIST-------------------------

Programming Problems 6 and 7 (except 7a.) on Page 413-414. Remember that Section 13.4 provides details of how to implement simulations.

Altogether, there should be 3 variations of the bank simulation:

- Single queue for one teller
- Three queue for three tellers (events take turn in the order of arrival or enter the shortest line)
- Single queue for three tellers

Randomly generate 100 events with a random start time within the range from 1 to 540 and a duration in the range of 1 to 20 to be used as the input to the simulation. Note that the input file should be the same when different queue methods are compared. Create 5 sets of input data and run all 3 variations of the simulation on each set.

You should first generate the random values for event start time, and then sort them. Then, for each start time you can assign your randomly generated values for event duration. These values should be written into a file that will be later read by simulator.

Deliverables:

Randomly generated input files
Source code for your classes
A main file that tests the classes with randomly generated data
Output of your code, formatted as described in the book, plus the additional statistics in part 7b.
Table including your comparisons of each simulation for each data set.

6. Implement the event-driven simulation of a bank that this chapter described. A queue ofarrival events will rep- resent the line of customers in the bank. Maintain the arrival events and departure events in a priority queue, sorted by the time oftheevent. Use a link-based implementation for the event list. The input is a text file of arrival and transaction times. Each line of the file contains the arrival time and required transaction time for a customer. The arrival times are ordered by increasing time. Your program must count customers and keep track of their cumulative waiting time. These statistics are sufficient to compute the average waiting time after the last event has been processed. Display a trace of the events executed and a summary of the computed statistics (the total number of arrivals and average time spent waiting in line). For example, the input file shown in the left columns ofthe following table should produce the output shown in the right column Input file Output from processing file on left Simulation Begins Processing an arrival event at time 1 Processing an arrival event at time: 2 Processing an arrival event at time: 4 20 Processing a departure event at time: 6 22 24 Processing a departure event at time: 11 Processing a departure event at time: 16 26 Processing an arrival event at time: 20 28 Processing an arrival event at time: 22 30 Processing an arrival event at time: 24 88 Processing a departure event at time: 25 Processing an arrival event at time 26 Processing an arrival event at time: 28 Processing an arrival event at time 30

Explanation / Answer

copyable code:

//myEvent.h

//Include the needed header files
#ifndef MyEvent_h
#define MyEvent_h
//Class name
class myEvent
{
   //Access specifier
   private:
       //Declare a variables
       char evType;
       unsigned int cusArrTime;
       unsigned int lenOfTime;
  
   public:
      
       //Default constructor
       myEvent(void);
      
       //Constructor
       myEvent(unsigned int incusArrTime, unsigned int inLenOfTime);
       //Constructor
       myEvent(char inEType, unsigned int incusArrTime, unsigned int inLenOfTime);
       //Destructor
       ~myEvent();
      
       //Declare the needed methods
       char getEvType() const;
       unsigned int getECATime() const;
       unsigned int getTimeLen() const;
       int setEvType(char inEType);
       int setECATime(unsigned int incusArrTime);
       int setTimeLen(unsigned int inLenOfTime);
};
#endif


//myEvent.cpp
//Include the needed header files.
#include "myEvent.h"
#include <iostream>
//Definition for default constructor.
myEvent::myEvent(void): evType('A'), cusArrTime(0), lenOfTime(0){}
//Definition for constructor.
myEvent::myEvent(unsigned int incusArrTime, unsigned int inLenOfTime): evType('A'), cusArrTime(incusArrTime), lenOfTime(inLenOfTime){}
//Definition for default constructor.
myEvent::myEvent(char inEType, unsigned int incusArrTime, unsigned int inLenOfTime): evType(inEType), cusArrTime(incusArrTime), lenOfTime(inLenOfTime){}
//Definition for destructor.
myEvent::~myEvent(){}
//Definition for the getter method getEvType().
char myEvent::getEvType() const
{
   //Return event type.
   return evType;
}
//Definition for the getter method getECATime().
unsigned int myEvent::getECATime() const
{
   //Return customer arrival time.
   return cusArrTime;
}
//Definition for the getter method getTimeLen().
unsigned int myEvent::getTimeLen() const
{
   //Return time length.
   return lenOfTime;
}
//Definition for the setter method setEvType().
int myEvent::setEvType(char inEType)
{
   //Check whether the type is not A and B.
   if(inEType != 'A' && inEType != 'D')
   {
       //Invalid input.
       return 1;
   }
  
   //Otherwise set type.
   evType = inEType;
  
   //Return valid.
   return 0;
}
//Definition for the setter method setECATime().
int myEvent::setECATime(unsigned int incusArrTime)
{
   //Check condition
   if(incusArrTime < 0)
   {
       //Return invalid.
       return 1;
   }
  
   //Otherwise
   cusArrTime = incusArrTime;
//Return valid
return 0;
}
//Definition for the setter method setTimeLen().
int myEvent::setTimeLen(unsigned int inLenOfTime)
{
   //Check whether the length of time is positive or not.
   if(inLenOfTime < 0)
   {
       //Return invalid.
       return 1;
   }
   //Otherwise set length of time.
   lenOfTime = inLenOfTime;
   //Return valid.
   return 0;
}


//myNode.h
//Include the needed header files.
#ifndef myNode_h
#define myNode_h
#include "myEvent.h"
//Class name.
class myNode
{
   //Access specifier.
   public:
       //Declare a variable for node data.
       myEvent nodeData;
       //Declare a pointer for next node.
       myNode* nodeNext;
       //Constructor.
       myNode();
       //Constructor.
       myNode(myEvent nData);
       //Constructor.
       myNode(myEvent nData, myNode* nNextNode);
       //Destructor.
       ~myNode();
};
#endif


//myNode.cpp
//Include the needed header files.
#include <iostream>
#include "myNode.h"
//Definition for constructor.
myNode::myNode()
{
   //Assign next pointer as null.
   nodeNext = NULL;
}
//Definition for constructor.
myNode::myNode(myEvent nData)
{
   //Initialize nData as nodeData.
   nodeData = nData;
   //Initialize null as nodeNext.
   nodeNext = NULL;
}
//Definition for constructor.
myNode::myNode(myEvent nData, myNode* nNextNode)
{
   //Assign nData as nodeData.
   nodeData = nData;
   //Assign nNextNode as nodeNext.
   nodeNext = nNextNode;
}
//Definition for destructor.
myNode::~myNode(){}

//myEmptyDataCollectionException.h
//Include the needed header files.
#ifndef myEmptyDataCollectionException_h
#define myEmptyDataCollectionException_h
#include <stdexcept>
#include <string>
using namespace std;
//Class name.
class myEmptyDataCollectionException : public logic_error
{
   //Access specifier.
   public:
   //Constructor.
   myEmptyDataCollectionException(const string& message = "");
};
#endif


//myEmptyDataCollectionException.cpp
//Include the needed header files.
#include "myEmptyDataCollectionException.h"
//Definition for constructor.
myEmptyDataCollectionException::myEmptyDataCollectionException(const string& message): logic_error("Exception: " + message){}

//myQueue.h
//Include the needed header files.
#ifndef myQueue_h
#define myQueue_h
#include "myEvent.h"
#include "myEmptyDataCollectionException.h"
//Declare a constant variable.
const unsigned int QUEUEMAX = 101;
//Class name.
class myQueue
{
   //Access specifier.
   private:
   //Declare a variable for starting position.
   unsigned int sPos;
   //Declare a variable for ending position.
   unsigned int ePos;
   //Declare a pointer for queue data.
   myEvent* queData;
   //Access specifier.
   public:
   //Default constructor.
   myQueue();
   //Default destructor.
   ~myQueue();
   //Declare a function isQueEmpty().
   bool isQueEmpty() const;
   //Declare a function queEnqueue().
   bool queEnqueue(const myEvent& newqData);
   //Declare a function queDequeue().
   bool queDequeue();
   //Declare a function quePeek().
   myEvent quePeek() const throw(myEmptyDataCollectionException);
};
#endif

//myQueue.cpp
//Include the needed header files.
#include "myQueue.h"
#include <cmath>
//Definition for constructor.
myQueue::myQueue(): sPos(0), ePos(0), queData(NULL)
{
   //New queue.
   queData = new myEvent[QUEUEMAX];
}
//Definition for destructor.
myQueue::~myQueue()
{
   //Clean the memory.
   delete[] queData;
   //Free the data.
   queData = NULL;
}

//Definition for isQueEmpty().
bool myQueue::isQueEmpty() const
{
//Check whether the queue is empty or not.
if(sPos == ePos)
{
   //Empty.
   return true;
}
   //Otherwise, not empty.
   return false;
}

//Definition for queEnqueue().
bool myQueue::queEnqueue(const myEvent& newqData)
{
   //Check whether the queue is full or not.
   if((sPos % QUEUEMAX) == (ePos+1) % QUEUEMAX)
   {
       //Return not empty.
       return false;
   }
   //Copy the data into array.
   queData[(ePos) % QUEUEMAX] = newqData;
   //Increment end position.
   ePos++;
   //Return valid.
   return true;
}
//Definition for queDequeue().
bool myQueue::queDequeue()
{
   //Check whether the queue is empty or not.
   if(sPos == ePos)
   {
       //Return not empty.
       return false;
   }
  
   //Decrement start position.
   sPos++;
   //Return valid.
   return true;
}
//Definition for quePeek().
myEvent myQueue::quePeek() const throw(myEmptyDataCollectionException)
{
   //Check whether the queue is empty or not.
   if(sPos == ePos)
   {
       //Throw an exception.
       throw myEmptyDataCollectionException("myQueue is Empty");
   }
  
   //Return the newest data.
   return queData[sPos % QUEUEMAX];
}


//myPriorityQueue.h
//Include the needed header files.
#ifndef myPriorityQueue_h
#define myPriorityQueue_h
#include "myEvent.h"
#include "myEmptyDataCollectionException.h"
#include "myNode.h"
//Class name.
class myPriorityQueue
{
   //Access specifier.
   private:
  
   //Declare a pointer for pqHead node.
   myNode* pqHead;
   //Access specifier.
   public:
   //Constructor.
   myPriorityQueue();
   //Destructor.
   ~myPriorityQueue();
   //Declare a method isQueEmpty().
   bool isQueEmpty() const;
   //Declare a method queEnqueue().
   bool queEnqueue(const myEvent& newqData);
   //Declare a method queDequeue().
   bool queDequeue();
   //Declare a method quePeek().
   myEvent quePeek() const throw(myEmptyDataCollectionException);
};
#endif


//myPriorityQueue.cpp
//Include the needed header files.
#include "myPriorityQueue.h"
#include <iostream>
//Definition for constructor.
myPriorityQueue::myPriorityQueue(): pqHead(NULL){}
//Definition for destructor.
myPriorityQueue::~myPriorityQueue()
{
   //Declare a pointer for pqLast node.
   myNode* pqLast;
   //Declare a pointer for current node.
   myNode* current = pqHead;
   //Execute till the current node is not null.
   while(current != NULL)
   {
       //Update pqLast node as current node.
       pqLast = current;
       //Update current node as current node next.
       current = current->nodeNext;
       //Remove the pqLast node.
       delete pqLast;
   }
   //Update pqHead node as null.
   pqHead = NULL;
}
//Definition for isQueEmpty().
bool myPriorityQueue::isQueEmpty() const
{
   //Check whether the priority queue is empty or not.
   if(pqHead == NULL)
   {
       //If so, return yes.
       return true;
   }
   //Otherwise no.
   return false;
}
//Definition for queEnqueue().
bool myPriorityQueue::queEnqueue(const myEvent& newqData)
{
   //Check whether the priority queue is empty or not.
   if(&newqData == NULL)
   {
       //If so, return yes.
       return false;
   }
   //Condition check.
   if(pqHead == NULL || newqData.getECATime() < pqHead->nodeData.getECATime())
   {
       //Allocate memory for temp node.
       myNode* tempNode = new myNode(newqData, pqHead);
       //Update pqHead node as temp node.
       pqHead = tempNode;
       //Return true.
       return true;
   }
   //Update current node as pqHead node.
   myNode* current = pqHead;
   //Update previous node as pqHead node.
   myNode* previous = pqHead;
   //Execute till this condition this satisfies.
   while(current != NULL && (newqData.getECATime() > current->nodeData.getECATime()) )
   {
       //Update previous node as current node.
       previous = current;
  
       //Update current node as current node next.
       current = current->nodeNext;
   }
   //Condition check.
   if(current != NULL && (newqData.getEvType() == 'D' && current->nodeData.getEvType() == 'A'))
   {
       //Condition check.
       if(newqData.getECATime() == current->nodeData.getECATime())
       {
           //Update previous node as current node.
           previous = current;
      
           //Update current node as current node next.
           current = current->nodeNext;
       }
   }
   //Allocate memory for temp node.
   myNode* tempNode = new myNode(newqData, previous-> nodeNext);
   //Update previous node next as temp node.
   previous->nodeNext = tempNode;
   //Return valid.
   return true;
}
//Definition for queDequeue().
bool myPriorityQueue::queDequeue()
{
   //Check whether the priority queue is empty or not.
   if(pqHead == NULL)
   {
       //Return false.
       return false;
   }
   //Allocate memory for pqHead node next node.
   myNode* headNext = pqHead->nodeNext;
   //Delete the pqHead node.
   delete pqHead;
   //Update headNext as pqHead.
   pqHead = headNext;
   //Return valid.
   return true;
}
//Definition for quePeek().
myEvent myPriorityQueue::quePeek() const throw(myEmptyDataCollectionException)
{
   //Check whether the priority queue is empty or not.
   if(pqHead == NULL)
   {
       //Throw an exception.
       throw myEmptyDataCollectionException("myPriorityQueue is Empty");
   }
   //Return the front data in the priority queue.
   return pqHead->nodeData;
}


//Main.cpp
//Include the needed header files.
#include <iostream>
#include <fstream>
#include <string>
#include "myEvent.h"
#include "myQueue.h"
#include "myPriorityQueue.h"
using namespace std;
//Client function loadInputIntoPQ().
void loadInputIntoPQ(myPriorityQueue &inPQ)
{
   //Declare a variable for temporary time.
   unsigned int ttTime;
   //Declare a variable for temporary length.
   unsigned int ttLength;
   //Declare a variable for file input.
   string fileName;
   //Create an instance for input file stream.
   ifstream fin;
   //Display message.
   cout << "Enter input file name: ";
   //Get file name.
   getline(cin, fileName);
   //Open file.
   fin.open(fileName.c_str());
   //Read values in.
   while(fin >> ttTime >> ttLength)
   {
       //Function call.
       myEvent tmpEvent(ttTime, ttLength);
       //Function call to enqueue in priority queue.
       inPQ.queEnqueue(tmpEvent);
   }
}
//Driver.
int main(void)
{
   //Create an instance for queue.
   myQueue mybankQueue;
   //Create an instance for priority queue.
   myPriorityQueue eventListPQ;
   //Declare a boolean variable.
   bool istellerAvailable = true;
   //Function call to loadInputIntoPQ().
   loadInputIntoPQ(eventListPQ);
   //Declare a variable for departure time total.
   unsigned int deptTimeTotal = 0;
   //Declare a variable for processing time total.
   unsigned int processesTimeTotal = 0;
   //Declare a variable for arrival time total.
   unsigned int arrTimeTotal = 0;
   //Declare a variable for event counter.
   unsigned int evCounter = 0;
   //Declare a variable for wait time.
   float evwaitTime;
   //Display message.
   cout <<endl<< "Simulation Begins" << endl;
   //Execute till the priority queue is empty.
   while(!eventListPQ.isQueEmpty())
   {
       //Peek the data.
       myEvent newerEvent = eventListPQ.quePeek();
       //Declare a variable for present time.
       unsigned int preTime = newerEvent.getECATime();
       //Check whether the event is arrival.
       if (newerEvent.getEvType() == 'A')
       {
           //Dequeue the priority queue.
           eventListPQ.queDequeue();
           //Declare a new bank customer event.
           myEvent bankCust = newerEvent;
           //Condition check.
           if (mybankQueue.isQueEmpty() && istellerAvailable)
           {
               //Declare a variable.
               unsigned int deptTime = preTime + bankCust.getTimeLen();
               //New departure event.
               myEvent newDepartureEvent('D', deptTime, 0);
               //Enqueue operation.
               eventListPQ.queEnqueue(newDepartureEvent);
               //Update boolean variable.
               istellerAvailable = false;
           }
           //Otherwise.
           else
           {
               //Function call to enqueue.
               mybankQueue.queEnqueue(bankCust);
           }
           //Display message.
           cout << "Processing an arrival event at time: " << bankCust.getECATime() << endl;
           //Increment event counter.
           evCounter++;
           //Update arrTimeTotal.
           arrTimeTotal += bankCust.getECATime();
           //Update processesTimeTotal.
           processesTimeTotal += bankCust.getTimeLen();
       }
       //Otherwise.
       else
       {
           //Function call to queDequeue().
           eventListPQ.queDequeue();
           //Condition check.
           if(!mybankQueue.isQueEmpty())
           {
               //Function call to quePeek().
               myEvent bankCust = mybankQueue.quePeek();
               //Function call to queDequeue().
               mybankQueue.queDequeue();
               //Update departure time.
               unsigned int deptTime = preTime + bankCust.getTimeLen();
               //New departure event.
               myEvent newDepartureEvent('D', deptTime, 0);
               //Function call to queEnqueue()
               eventListPQ.queEnqueue(newDepartureEvent);
           }
           //Otherwise.
           else
           {
               //Update boolean variable.
               istellerAvailable = true;
           }
           //Display message.
           cout << "Processing a departure event at time: " << preTime << endl;
           //Update total departure time.
           deptTimeTotal += preTime;
           }
       }
       //Compute wait time.
       evwaitTime = (float)(deptTimeTotal - processesTimeTotal - arrTimeTotal)/evCounter;
       //Display message.
       cout << "Simulation Ends"<<endl<<endl;
       cout << "Final Statistics:" << endl;
       cout << " Total number of people processed: " << evCounter << endl;
       cout << " Average amount of time spent waiting: " << evwaitTime << endl;
       //Pause the output console.
       cin.get();cin.get();
       //Stop.
       return 0;
}

Data.txt

1   5

2   5

4   5

20 5

22 5

24 5

26 5

28 5

30 5

88 3

sh-4.2$ main                                                                                                                                                                   

Enter input file name: data.txt                                                                                                                                                 

                                                                                                                                                                                

Simulation Begins                                                                                                                                                              

Processing an arrival event at time:    1                                                                                                                                      

Processing an arrival event at time:    2                                                                                                                                       

Processing an arrival event at time:    4                                                                                                                                      

Processing a departure event at time:   6                                                                                                                                      

Processing a departure event at time:   11                                                                                                                                      

Processing a departure event at time:   16                                                                                                                                      

Processing an arrival event at time:    20                                                                                                                                     

Processing an arrival event at time:    22                                                                                                                                      

Processing an arrival event at time:    24                                                                                                                                      

Processing a departure event at time:   25                                                                                                                                     

Processing an arrival event at time:    26                                                                                                                                     

Processing an arrival event at time:    28                                                                                                                                     

Processing an arrival event at time:    30                                                                                                                                      

Processing a departure event at time:   30                                                                                                                                      

Processing a departure event at time:   35                                                                                                                                     

Processing a departure event at time:   40                                                                                                                                      

Processing a departure event at time:   45                                                                                                                                      

Processing a departure event at time:   50                                                                                                                                     

Processing an arrival event at time:    88                                                                                                                                     

Processing a departure event at time:   91                                                                                                                                      

Simulation Ends                            

Final Statistics:                                                                                                                                                              

        Total number of people processed: 10                                                                                                                                    

        Average amount of time spent waiting: 5.6

note: answered question 6 repost remaining