#include <iostream> #include <string> #include <cstdlib> #include \"Simulation.h
ID: 3661486 • Letter: #
Question
#include <iostream>
#include <string>
#include <cstdlib>
#include "Simulation.h"
#include "queueAsArray.h"
using namespace std;
//*************** customerType ************
void customerType::setCustomerInfo(int cN, int arrvTime,
int wTime, int tTime)
{
customerNumber = cN;
arrivalTime = arrvTime;
waitingTime = wTime;
transactionTime = tTime;
}
customerType::customerType(int customerN, int arrvTime,
int wTime, int tTime)
{
setCustomerInfo(customerN, arrvTime, wTime, tTime);
}
int customerType::getWaitingTime() const
{
return waitingTime;
}
void customerType::incrementWaitingTime()
{
waitingTime++;
}
void customerType::setWaitingTime(int time)
{
cout << "See Programming Exercise 8 at the end of this chapter." << endl;
}
int customerType::getArrivalTime() const
{
cout << "See Programming Exercise 8 at the end of this chapter." << endl;
return 0;
}
int customerType::getTransactionTime() const
{
cout << "See Programming Exercise 8 at the end of this chapter." << endl;
return 0;
}
int customerType::getCustomerNumber() const
{
cout << "See Programming Exercise 8 at the end of this chapter." << endl;
return 0;
}
//**************** serverType **********
serverType::serverType()
{
status = "free";
transactionTime = 0;
}
bool serverType::isFree() const
{
return (status == "free");
}
void serverType::setBusy()
{
status = "busy";
}
void serverType::setFree()
{
status = "free";
}
void serverType::setTransactionTime(int t)
{
transactionTime = t;
}
void serverType::setTransactionTime()
{
int time;
time = currentCustomer.getTransactionTime();
transactionTime = time;
}
void serverType::decreaseTransactionTime()
{
transactionTime--;
}
int serverType::getRemainingTransactionTime() const
{
cout << "See Programming Exercise 8 at the end of this chapter." << endl;
return 0;
}
void serverType::setCurrentCustomer(customerType cCustomer)
{
cout << "See Programming Exercise 8 at the end of this chapter." << endl;
}
int serverType::getCurrentCustomerNumber() const
{
cout << "See Programming Exercise 8 at the end of this chapter." << endl;
return 0;
}
int serverType::getCurrentCustomerArrivalTime() const
{
cout << "See Programming Exercise 8 at the end of this chapter." << endl;
return 0;
}
int serverType::getCurrentCustomerWaitingTime() const
{
cout << "See Programming Exercise 8 at the end of this chapter." << endl;
return 0;
}
int serverType::getCurrentCustomerTransactionTime() const
{
cout << "See Programming Exercise 8 at the end of this chapter." << endl;
return 0;
}
//************** serverListType ***********
serverListType::serverListType(int num)
{
numOfServers = num;
servers = new serverType[num];
}
serverListType::~serverListType()
{
delete [] servers;
}
int serverListType::getFreeServerID() const
{
int serverID = -1;
for (int i = 0; i < numOfServers; i++)
if (servers[i].isFree())
{
serverID = i;
break;
}
return serverID;
}
int serverListType::getNumberOfBusyServers() const
{
int busyServers = 0;
for (int i = 0; i < numOfServers; i++)
if (!servers[i].isFree())
busyServers++;
return busyServers;
}
void serverListType::setServerBusy(int serverID,
customerType cCustomer,
int tTime)
{
servers[serverID].setBusy();
servers[serverID].setTransactionTime(tTime);
servers[serverID].setCurrentCustomer(cCustomer);
}
void serverListType::setServerBusy(int serverID,
customerType cCustomer)
{
int time;
time = cCustomer.getTransactionTime();
servers[serverID].setBusy();
servers[serverID].setTransactionTime(time);
servers[serverID].setCurrentCustomer(cCustomer);
}
void serverListType::updateServers(ostream& outF)
{
for (int i = 0; i < numOfServers; i++)
if (!servers[i].isFree())
{
servers[i].decreaseTransactionTime();
if (servers[i].getRemainingTransactionTime() == 0)
{
outF << "From server number " << (i + 1)
<< " customer number "
<< servers[i].getCurrentCustomerNumber()
<< " departed at clock unit "
<< servers[i].getCurrentCustomerArrivalTime()
+ servers[i].getCurrentCustomerWaitingTime()
+ servers[i].getCurrentCustomerTransactionTime()
<< endl;
servers[i].setFree();
}
}
}
//*************** waitQueue ************
waitingCustomerQueueType::waitingCustomerQueueType(int size)
:queueType<customerType>(size)
{
}
void waitingCustomerQueueType::updateWaitingQueue()
{
customerType cust;
cust.setWaitingTime(-1);
int wTime = 0;
addQueue(cust);
while (wTime != -1)
{
cust = front();
deleteQueue();
wTime = cust.getWaitingTime();
if (wTime == -1)
break;
cust.incrementWaitingTime();
addQueue(cust);
}
}
a. Write the definitions ofthe functions setWaitingTime, getArrivalTime, getTransactionTime,and getCustomerNumber of the class customerType defined in the section, ‘‘Application of Queues: Simulation.’’
b. Write the definitions of the functions getRemainingTransactionTime, setCurrentCustomer, getCurrentCustomerNumber, getCurrentCustomerArrivalTime, getCurrentCustomerWaitingTime,and getCurrentCustomerTransactionTime of the class serverType defined in the section, ‘‘Application of Queues: Simulation.’’
c. Write the definition of the function runSimulation to complete the design of the computer simulation program (see the section, ‘‘Application of Queues: Simulation’’). Test run your program for a variety of data. Moreover, use a random number generator to decide whether a customer arrived at a given time unit.
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <string>
#include "Simulation.h"
#include "queueAsArray.h"
using namespace std;
//*************** customerType ************
void customerType::setCustomerInfo(int cN, int arrvTime,
int wTime, int tTime)
{
customerNumber = cN;
arrivalTime = arrvTime;
waitingTime = wTime;
transactionTime = tTime;
}
customerType::customerType(int customerN, int arrvTime,
int wTime, int tTime)
{
setCustomerInfo(customerN, arrvTime, wTime, tTime);
}
void customerType::incrementWaitingTime()
{
waitingTime++;
}
int customerType::getWaitingTime() const
{
return waitingTime;
}
int customerType::getArrivalTime() const
{
return arrivalTime;
}
void customerType::setWaitingTime(int time)
{
waitingTime = time;
}
int customerType::getCustomerNumber() const
{
return customerNumber;
}
int customerType::getTransactionTime() const
{
return transactionTime;
}
//**************** serverType **********
serverType::serverType()
{
status = "free";
transactionTime = 0;
}
void serverType::setFree()
{
status = "free";
}
void serverType::setBusy()
{
status = "busy";
}
bool serverType::isFree() const
{
return (status == "free");
}
void serverType::setTransactionTime(int t)
{
transactionTime = t;
}
void serverType::setTransactionTime()
{
int time;
time = currentCustomer.getTransactionTime();
transactionTime = time;
}
void serverType::decreaseTransactionTime()
{
transactionTime--;
}
void serverType::setCurrentCustomer(customerType cCustomer)
{
currentCustomer = cCustomer;
}
int serverType::getCurrentCustomerArrivalTime() const
{
int cCustomerArrivalTime = currentCustomer.getArrivalTime();
return cCustomerArrivalTime;
}
int serverType::getRemainingTransactionTime() const
{
return transactionTime;
}
int serverType::getCurrentCustomerNumber() const
{
int cCustomerNumber = currentCustomer.getCustomerNumber();
return cCustomerNumber;
}
int serverType::getCurrentCustomerTransactionTime() const
{
int cCustomerTransactionTime = currentCustomer.getTransactionTime();
return 0;
}
int serverType::getCurrentCustomerWaitingTime() const
{
int cCustomerWaitTime = currentCustomer.getWaitingTime();
return 0;
}
//************** serverListType ***********
serverListType::serverListType(int num)
{
numOfServers = num;
servers = new serverType[num];
}
serverListType::~serverListType()
{
delete [] servers;
}
int serverListType::getFreeServerID() const
{
int serverID = -1;
for (int i = 0; i < numOfServers; i++)
if (servers[i].isFree())
{
serverID = i;
break;
}
return serverID;
}
void serverListType::setServerBusy(int serverID,
customerType cCustomer,
int tTime)
{
servers[serverID].setBusy();
servers[serverID].setTransactionTime(tTime);
servers[serverID].setCurrentCustomer(cCustomer);
}
int serverListType::getNumberOfBusyServers() const
{
int busyServers = 0;
for (int i = 0; i < numOfServers; i++)
if (!servers[i].isFree())
busyServers++;
return busyServers;
}
void serverListType::updateServers()
{
for (int i = 0; i < numOfServers; i++)
if (!servers[i].isFree())
{
servers[i].decreaseTransactionTime();
if (servers[i].getRemainingTransactionTime() == 0)
{
cout << "From server number " << (i + 1)
<< " customer number "
<< servers[i].getCurrentCustomerNumber()
<< " departed at clock unit "
<< servers[i].getCurrentCustomerArrivalTime()
+ servers[i].getCurrentCustomerWaitingTime()
+ servers[i].getCurrentCustomerTransactionTime()
<< endl;
servers[i].setFree();
}
}
}
void serverListType::setServerBusy(int serverID,
customerType cCustomer)
{
int time;
time = cCustomer.getTransactionTime();
servers[serverID].setBusy();
servers[serverID].setTransactionTime(time);
servers[serverID].setCurrentCustomer(cCustomer);
}
//*************** waitQueue ************
waitingCustomerQueueType::waitingCustomerQueueType(int size)
:queueType<customerType>(size)
{
}
void waitingCustomerQueueType::updateWaitingQueue()
{
customerType cust;
cust.setWaitingTime(-1);
int wTime = 0;
addQueue(cust);
while (wTime != -1)
{
cust = front();
deleteQueue();
wTime = cust.getWaitingTime();
if (wTime == -1)
break;
cust.incrementWaitingTime();
addQueue(cust);
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdlib>
#include <iomanip>
#include "simulation.h"
#include "queueAsArray.h"
using namespace std;
void setSimulationParameters(int& sTime, int& numOfServers,
int& transTime,
int& tBetweenCArrival);
void runSimulation();
int main()
{
bool more = true;
while (more)
{
// int rNum = rand() % 4;
// cout << rNum;
runSimulation();
string ans = " ";
cout << "would you like to try again?" << endl;
cin >> ans;
if (ans == "n"||ans == "N")
more = false;
}
return 0;
}
void setSimulationParameters(int& sTime, int& numOfServers,
int& transTime,
int& tBetweenCArrival)
{
cout << "Enter the simulation time: ";
cin >> sTime;
cout << endl;
cout << "Enter the number of servers: ";
cin >> numOfServers;
cout << endl;
cout << "Enter the transaction time: ";
cin >> transTime;
cout << endl;
cout << "Enter the time between customer arrivals: ";
cin >> tBetweenCArrival;
cout << endl;
}
void runSimulation()
{
int sTime, numOfServers, transTime, tBetweenCArrival;
serverListType servers(numOfServers);
setSimulationParameters(sTime,numOfServers, transTime, tBetweenCArrival);
waitingCustomerQueueType customers;
int customerNumber = 0;
int customerTime = tBetweenCArrival;
int totalWait = 0;
int numberOfCustomers = 0;
int rNum = 0;
for (int clock = 0; clock <= sTime; clock++)
{
servers.updateServers();
rNum = rand() % tBetweenCArrival;
char outF;
if (!customers.isEmptyQueue())
customers.updateWaitingQueue(); //increment the time time each customer has been waiting.
if (rNum == 1) //if customerTime runs out, a new customer is added to the queue.
{
customerNumber++;
customerType newCustomer(customerNumber, clock, 0, transTime);
customers.addQueue(newCustomer);
customerTime = tBetweenCArrival;
cout << "Customer#: " << customerNumber << " arrived at time unit " << clock << endl;
numberOfCustomers++;
}
if (servers.getFreeServerID() != -1)
{
if (!customers.isEmptyQueue())
{
int serverID = servers.getFreeServerID();
customerType nextCustomer;
nextCustomer = customers.front();
customers.deleteQueue();
numberOfCustomers--;
servers.setServerBusy(serverID, nextCustomer, transTime);
totalWait = totalWait + nextCustomer.getWaitingTime();
}
}
}
cout << "The simulation ran for " << sTime << " time units. "
<< "Number of Servers: " << numOfServers << endl
<< "Average transaction time: " << transTime << endl
<< "Average arrival time difference between customers: " << customerTime << endl
<< "Total waiting time : " << totalWait << endl
<< "Number of customers that completed a transaction " << customerNumber - numberOfCustomers << endl
<< "Total number of customers left in queue: " << numberOfCustomers << endl
<< "Average waiting time: " << totalWait/customerNumber << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.