(C++ Intro to Programming 2). Any help would be much appreciated. I think I got
ID: 3689029 • Letter: #
Question
(C++ Intro to Programming 2). Any help would be much appreciated. I think I got the first part right but I'm not sure and I also was wondering how to do the second part. Thank you in advance.
//--------------------------------------------------------------------
// file: storesim.cpp
//--------------------------------------------------------------------
// Simulates the flow of customers through a line in a store.
// makes use of queues.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "queuelnk.cpp"
using namespace std;
void main()
{
Queue<int> custQ; // Line (queue) of customers containing the
// time that each customer arrived and
// joined the line
int simLength, // Length of simulation (minutes)
minute, // Current minute
timeArrived, // Time dequeued customer arrived
waitTime, // How long dequeued customer waited
totalServed = 0, // Total customers served
totalWait = 0, // Total waiting time
maxWait = 0, // Longest wait
numArrivals = 0;
cout << endl
<< "Enter the length of time to run the simulator : ";
cin >> simLength;
for (minute = 0; minute < simLength; minute++)
{
if (!custQ.isEmpty())
{
//* Add code here to do the following:
//* 1) dequeue the first customer and capture its return
//* in timeArrived
//* 2) increment totalServed
//* 3) calculate the waitTime of the customer
//* 4) add the waitTime of the customer to totalWait
//* 5) update maxWait if this customer waited longer than any
//* previous customer.
timeArrived = custQ.dequeue();
waitTime = minute - timeArrived;
totalServed++;
totalWait += waitTime;
if (waitTime > maxWait)
maxWait = waitTime;
}
switch (rand() % 4)
{
// The number generated above will be between 0 and 3
// If 0 or 3, then no customers will be added, If 2, then 2
// customers will be added. If 1, then one customer will be added.
case 0:
case 3: numArrivals = 0; break;
case 1: numArrivals = 1; break;
case 2: numArrivals = 2;
}
for (int j = 0; j < numArrivals; j++)
custQ.enqueue(minute);
}
cout << endl;
cout << "Customers served : " << totalServed << endl;
cout << "Average wait : " << setprecision(2)
<< double(totalWait) / totalServed << endl;
cout << "Longest wait : " << maxWait << endl;
}
Explanation / Answer
Here is the modified code any issues please let me know:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main ()
{
QueueLinked<int> custQ;
QueueArray<int> custQ;
int simLength,
minute,
timeArrived,
waitTime,
totalServed = 0,
totalWait = 0,
maxWait = 0,
numArrivals = 0;
srand( time(NULL) );
cout << endl << "Enter the length of time to run the simulator : ";
cin >> simLength;
minute = 0;
int k;
while(minute <= simLength)
{
minute++;
int custTemp = 0;
if(!custQ.isEmpty())
{
timeArrived = custQ.dequeue();
waitTime = minute - timeArrived;
totalWait += waitTime;
totalServed++;
if(waitTime > maxWait)
{
maxWait = waitTime;
}
timeArrived = custTemp;
}
k = rand() % 4;
if(k == 1 )
{
numArrivals++;
}
else if (k ==2)
{
numArrivals+=2;
}
for(int x = 0; x < numArrivals; x++)
{
custQ.enqueue(minute);
}
numArrivals=0;
}
cout << endl;
cout << "Customers served : " << totalServed << endl;
cout << "Average wait : " << setprecision(2)<< double(totalWait)/totalServed << endl;
cout << "Longest wait : " << maxWait << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.