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

Write a C++ program that simulates the behavior of a bank. A queue of arrival ev

ID: 3559781 • Letter: W

Question

Write a C++ program that simulates the behavior of a bank. A queue of arrival events will represent the line of customers. Maintain arrival departure events in a priority queue, sorted by the time of event. Use link-based implementation for the event list. The program will read data from an input text file that displays the arrival time and transaction length of each customer. An example of the text would be:

0          3
2          2
8          6
20        4
22        3

The program will output a text file that displays that the simulation began, along with each event taking place. In addition, it will display the total number of people processed and the average time spent waiting. And example of the output file would be:

Simulation Begins
Processing an arrival event at time: 0
Processing an arrival event at time: 2
Processing a departure event at time: 3
Processing a departure event at time: 5
Processing an arrival event at time: 8
Processing a departure event at time: 14
Processing an arrival event at time: 20
Processing an arrival event at time: 22
Processing a departure event at time: 24
Processing a departure event at time: 27

Final Statistics:
    Total number of people processed: 5
    Average amount of time spent waiting: .6

Explanation / Answer

Indented code looks and feels good.

#include <bits/stdc++.h>
using namespace std;

struct Node {
int arrivalTime;
int transactionLength;
Node* next;

Node(int arrivalTime, int transactionLength) {
this -> arrivalTime = arrivalTime;
this -> transactionLength = transactionLength;
this -> next = NULL;
}
};

struct PriorityQueue {
Node* front;
Node* rear;
int size;

PriorityQueue() {
this -> rear = NULL;
this -> front = NULL;
this -> size = 0;
}
};

bool isEmpty(PriorityQueue* events) {
return (events -> size) == 0;
}

void enqueue(PriorityQueue* events, int arrivalTime, int transactionLength) {
Node* newEvent = new Node(arrivalTime, transactionLength);
Node* crawler = events -> front;
Node* prvs = NULL;
while(crawler != NULL && crawler -> arrivalTime < newEvent -> arrivalTime) {
prvs = crawler;
crawler = crawler -> next;
}
if(prvs == NULL) {
events -> front = newEvent;
}
else {
prvs -> next = newEvent;
newEvent -> next = crawler;
}
events -> size += 1;
}

Node* dequeX(PriorityQueue* events) {
Node* result = events -> front;
if(events -> front != NULL) {
events -> front = events -> front -> next;
}
events -> size -= 1;
return result;
}

int main() {

freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);

PriorityQueue* pq = new PriorityQueue();

int arrivalTime, transactionLength;
int totalNumberOfEvents = 0;

while(cin >> arrivalTime) {
cin >> transactionLength;
++totalNumberOfEvents;
enqueue(pq, arrivalTime, transactionLength);
}

int currentTime = 0;
int totalWaitingTime = 0;
vector<int> arrivals, departures;

while(!isEmpty(pq)) {
Node* event = dequeX(pq);
arrivalTime = event -> arrivalTime;
arrivals.push_back(arrivalTime);
transactionLength = event -> transactionLength;
totalWaitingTime += abs(currentTime - arrivalTime);
totalWaitingTime += transactionLength;
currentTime = max(currentTime, arrivalTime);
currentTime += transactionLength;
departures.push_back(currentTime);
}

sort(arrivals.begin(), arrivals.end());
sort(departures.begin(), departures.end());

int pointerToArrival = 0;
int pointerToDeparture = 0;

while(pointerToArrival < arrivals.size() && pointerToArrival < departures.size()) {
if(arrivals[pointerToArrival] < departures[pointerToDeparture]) {
cout << "Processing an arrival event at time: " << arrivals[pointerToArrival]
<< endl;
++pointerToArrival;
}
else {
cout << "Processing a departure event at time: " << departures[pointerToDeparture]
<< endl;
++pointerToDeparture;
}
}

while(pointerToDeparture < departures.size()) {
cout << "Processing a departure event at time: " << departures[pointerToDeparture]
<< endl;
++pointerToDeparture;
}

cout << " Final Statistics: "
<< " Total number of people processed: " << totalNumberOfEvents << endl
<< " Average amount of time spent waiting: "
<< (double) totalWaitingTime / totalNumberOfEvents << endl;

return 0;
}

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