The following code simulates customers at a bank using the data below. Explain h
ID: 3807836 • Letter: T
Question
The following code simulates customers at a bank using the data below. Explain how the program works as well as you can from exploring the code. Append your run results to your answer.
//Bank simulation
#include <iostream>
#include <cstdlib>
#include <queue> //
using namespace std;
int option(int percents[]) {
register int i = 0, choice = rand()%100+1, perc;
for (perc = percents[0]; perc < choice; perc += percents[i+1], i++);
return i;
}
int main() {
int arrivals[] = {15,20,25,10,30};
int service[] = {0,0,0,10,5,10,10,0,15,25,10,15};
int clerks[] = {0,0,0,0}, numOfClerks = sizeof(clerks)/sizeof(int);
int customers, t, i, numOfMinutes = 100, x;
double maxWait = 0.0, currWait = 0.0, thereIsLine = 0.0;
cout << "sizo = " << sizeof(int) << endl;
queue<int> simulQ;
cout.precision(2);
for (t = 1; t <= numOfMinutes; t++) {
cout << " t = " << t;
for (i = 0; i < numOfClerks; i++)
if (clerks[i] < 60)
clerks[i] = 0;
else clerks[i] -= 60;
customers = option(arrivals);
for (i = 0; i < customers; i++) {
x = option(service)*10;
simulQ.push(x);
currWait += x;
}
for (i = 0; i < numOfClerks && !simulQ.empty(); )
if (clerks[i] < 60) {
x = simulQ.front();
simulQ.pop();
clerks[i] += x;
currWait -= x;
}
else i++;
if (!simulQ.empty()) {
thereIsLine++;
cout << " wait = " << currWait/60.0;
if (maxWait < currWait)
maxWait = currWait;
}
else cout << " wait = 0;";
}
cout << " For " << numOfClerks << " clerks, there was a line "
<< thereIsLine/numOfMinutes*100.0 << "% of the time; "
<< "maximum wait time was " << maxWait/60.0 << " min.";
return 0;
}
Explanation / Answer
I have added the comments to the program as much as i can.
#include <iostream>
#include <cstdlib>
#include <queue> //
using namespace std;
int option(int percents[]) {
register int i = 0, choice = rand()%100+1, perc;
for (perc = percents[0]; perc < choice; perc += percents[i+1], i++);
return i;
}
int main() {
int arrivals[] = {15,20,25,10,30}; //This is arrival of how many customers
int service[] = {0,0,0,10,5,10,10,0,15,25,10,15}; // Service time given to customers
int clerks[] = {0,0,0,0}, numOfClerks = sizeof(clerks)/sizeof(int); //Total clerks are 4 are mentioned
int customers, t, i, numOfMinutes = 100, x; //Decalring variables
double maxWait = 0.0, currWait = 0.0, thereIsLine = 0.0; //Setting All time variables to 0 first
cout << "sizo = " << sizeof(int) << endl; //Displaying Sizo size of int which is 4
queue<int> simulQ; //Decalring Queue simulQ
cout.precision(2); //Setting precision to 2 which means only 2 digitis will be displayed after point
for (t = 1; t <= numOfMinutes; t++) { //Looping t from 1
cout << " t = " << t;
for (i = 0; i < numOfClerks; i++)
if (clerks[i] < 60) //if clerks 1 has less then 60 then assign him 0
clerks[i] = 0;
else clerks[i] -= 60; //if more than 60 then minus 60 from it
customers = option(arrivals);
for (i = 0; i < customers; i++) { //looping in from customers
x = option(service)*10; //setting service time mulitpled by 10
simulQ.push(x); //pushed x time for servive
currWait += x; //increases wait time
}
for (i = 0; i < numOfClerks && !simulQ.empty(); ) //loops now through clerks
if (clerks[i] < 60) {
x = simulQ.front();
simulQ.pop();
clerks[i] += x;
currWait -= x;
}
else i++;
if (!simulQ.empty()) { //if queue is not empty
thereIsLine++; //line is incremented
cout << " wait = " << currWait/60.0; //wait time is displayed
if (maxWait < currWait)
maxWait = currWait;
}
else cout << " wait = 0;"; //else wait time is 0 customer ready to be serverd
}
cout << " For " << numOfClerks << " clerks, there was a line " //Displaying total clerks
<< thereIsLine/numOfMinutes*100.0 << "% of the time; " //line and wait time average
<< "maximum wait time was " << maxWait/60.0 << " min.";
return 0;
}
Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.