Using the STL class, write a C++ program that simulates a visit to the doctor\'s
ID: 3588233 • Letter: U
Question
Using the STL class, write a C++ program that simulates a visit to the doctor's office. You can make the following assumptions:
1. Patients arrive at the doctor's office one at a time and are served one at a time, in the order of arrival.
2. The doctor does not plan to not be in his office for more than 1 hour, but will stay if there are patients waiting and remain there until no more patients are waiting. Model an hour as a minute in your program.
3. The arrival rate is a random number, but no new arrivals can arrive after 1 hour (1 minute in the program) has passed.
4. The service rate is a random number in minutes (seconds in your program). You can assume this number is the data value in each student (queue item) which pauses the professor from popping the next student from the queue for that duration.
Run the program 100 times using a loop and find the following:
1. The average time a patient spends waiting during a doctor's office visit
2. The average time a patient spends with the doctor during a doctor's office visit
3. The average time a doctor spends at his office beyond the 1 hour he intends to
Explanation / Answer
Solution to given problem is provided below. Also sample execution output is provided for reference.
File: DrService.cpp
#include <iostream>
#include <queue>
#include <thread>
#include <chrono>
#include <stdlib.h>
using namespace std;
class PatientRecord
{
private:
int arrivalTime;
int serviceStartTime;
int serviceDuration;
public:
PatientRecord(int arrivalTime)
{
this->arrivalTime = arrivalTime;
this->serviceDuration = 0;
}
int getArrivalTime()
{
return arrivalTime;
}
void setArrivalTime(int arrivalTime)
{
this->arrivalTime = arrivalTime;
}
int getServiceDuration()
{
return serviceDuration;
}
void setServiceDuration(int serviceDuration)
{
this->serviceDuration = serviceDuration;
}
int getServiceStartTime()
{
return serviceStartTime;
}
void setServiceStartTime(int serviceStartTime)
{
this->serviceStartTime = serviceStartTime;
}
int getWaitTime()
{
if (serviceStartTime > arrivalTime)
{
return (serviceStartTime - arrivalTime);
}
return 0;
}
};
void simulateArrival(queue<PatientRecord *> & patientInQ)
{
int patientArrivalTime = 0;
srand(time(0));
do
{
int random = rand() % 60;
patientArrivalTime += random;
if (patientArrivalTime <= 60)
{
patientInQ.push(new PatientRecord(patientArrivalTime));
}
} while (patientArrivalTime < 60);
}
void simulateService(queue<PatientRecord *> patientInQ)
{
int currentTime = 0;
int arrivalTime;
int serviceDuration;
while (patientInQ.empty() == false)
{
PatientRecord *patientRecord = patientInQ.front();
// wait for patient to arrive
arrivalTime = patientRecord->getArrivalTime();
if (arrivalTime > currentTime) {
this_thread::sleep_for(chrono::seconds(arrivalTime - currentTime));
currentTime = arrivalTime;
}
// wait for doctor service
serviceDuration = rand() % 60;
patientRecord->setServiceStartTime(currentTime);
patientRecord->setServiceDuration(serviceDuration);
this_thread::sleep_for(chrono::seconds(serviceDuration));
currentTime += serviceDuration;
patientInQ.pop();
}
}
void updateAvgs(queue<PatientRecord *> patientInQ, int & patientTotal,
int & patientWaitTimeTotal, int & patientServiceDurationTotal,
int & doctorOvertimeTotal)
{
while (patientInQ.empty() == false)
{
PatientRecord *patientRecord = patientInQ.front();
patientInQ.pop();
patientTotal++;
patientWaitTimeTotal += patientRecord->getWaitTime();
patientServiceDurationTotal += patientRecord->getServiceDuration();
if (patientInQ.empty())
{
int endTime = patientRecord->getServiceStartTime() + patientRecord->getServiceDuration();
if (endTime > 60)
{
doctorOvertimeTotal += (endTime - 60);
}
}
}
}
void clearQueue(queue<PatientRecord *> & patientInQ)
{
while (patientInQ.empty() == false) patientInQ.pop();
}
int main()
{
queue<PatientRecord *> patientInQ;
queue<PatientRecord *> patientOutQ;
int numIterations = 100;
int patientTotal = 0;
int patientWaitTimeTotal = 0;
int patientServiceDurationTotal = 0;
int doctorOvertimeTotal = 0;
for (int i = 0; i < numIterations; i++)
{
simulateArrival(patientInQ);
simulateService(patientInQ);
updateAvgs(patientInQ, patientTotal, patientWaitTimeTotal, patientServiceDurationTotal, doctorOvertimeTotal);
clearQueue(patientInQ);
}
cout << "Average patient wait time: " << (patientWaitTimeTotal * 1.0) / patientTotal << endl;
cout << "Average patient service time: " << (patientServiceDurationTotal * 1.0) / patientTotal << endl;
cout << "Average doctor over time: " << (doctorOvertimeTotal * 1.0) / numIterations << endl;
}
Sample Execution Output:
$ g++ DrService.cpp -std=c++11
$ ./a.out
Average patient wait time: 6
Average patient service time: 22.3333
Average doctor over time: 19.5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.