Question: #include #include #include #include #include #include #include #include using namespace std; //Create a struct or class to represent a customer. //Include these data members: arrival
#include #include #include #include #include #include #include #include using namespace std;
//Create a struct or class to represent a customer. //Include these data members: arrival time, service time, and completion time, all as whole numbers. struct customer { int arrival_time; int service_time; int completion_time; char ID; customer() : service_time(0), completion_time(0), ID(' '){} };
struct serviceEvent { int clock_time; int service_number; bool operator <(const serviceEvent& a) const { return (clock_time > a.clock_time); } };
int getRam(double average) { int req = 0; double num_of_requests = exp(-average); for (double random = (double)rand() / RAND_MAX; (random -= num_of_requests) > 0.0; num_of_requests *= average / (double)++req); return req; } int main() {
srand((float)time(0)); rand();
// read the input values from a text file, one per line, in the order specified above. ifstream fin; fin.open("test.txt"); if (!fin.good()) throw "Failed to read file"; int num_servers; float averageArr; int max_wtime; int min_sInterval; int max_sInterval; int clock_time;
fin >> num_servers; fin.ignore(1000, 10); fin >> averageArr; fin.ignore(1000, 10); fin >> max_wtime; fin.ignore(1000, 10); fin >> min_sInterval; fin.ignore(1000, 10); fin >> max_sInterval; fin.ignore(1000, 10); fin >> clock_time; fin.ignore(1000, 10); //output read results cout << "number of servers: " << num_servers << endl; cout << "customer arrival rate: " << averageArr << " per minute, for 50 minutes" << endl; cout << "maximum queue length: " << max_wtime << endl; cout << "minimum service time: " << min_sInterval << endl; cout << "maximum service time: " << max_sInterval << endl;
// declare and create and assign arrays and queues to be used in the solution queue cQueue; queue sDone; priority_queue sEvent; customer* servers = new customer[num_servers]; bool* status = new bool[num_servers]; string buf; int n = 0; int turn_away_c;
//initiallize bool status for (int i = 0; i < num_servers; i++) status[i] = false;
// the clock time loop for (int time = 0;; time++) // the clock time { // handle all service events scheduled to complete at this clock time //while (event queue is not empty AND clock time of the top service event equals the current clock time) while (!sEvent.empty() && sEvent.top().clock_time == time) { //remove the top service event from the queue//determine the server associated with the removed service event serviceEvent temp = sEvent.top(); sEvent.pop(); int sNumber = temp.service_number; sDone.push(servers[num_servers]); //set this server's current customer's completion time to the current clock time //copy this server's current customer to the served customers queue //set this server to idle servers[num_servers].ID = ' '; status[num_servers] = false;
}
// handle new arrivals -- can be turned away if wait queue is at maximum length! //if time is less than the time at which new arrivals stop if (time < max_wtime) { int random = getRam(averageArr); //get the #of of arrivals from the Poisson process(a whole number >= 0) --see the lecture topic notes! for (int i = 0; i < random; i++) //for each new arrival {// if the wait queue is full if (cQueue.size() >= max_wtime) turn_away_c++; // "turn away the customer" by adding one to the count of turned away customers else //else { customer newc; //create a new customer object, setting its arrival time equal to the current clock time newc.arrival_time = time; newc.ID = (char)('A' + (n++ % 26));//copy the new customer to the wait queue cQueue.push(newc); // for idle servers, move customer from wait queue and begin service } } } for (int i = 0; i < num_servers;i++) //for each server { //if (server is idle AND the wait queue is not empty) if (!status[i] && !cQueue.empty()) { customer temp = cQueue.front(); cQueue.pop(); //remove top customer from wait queue servers[i] = temp; //copy it to the server array temp.service_time = time; //set the copied customer's service time to the current clock time status[i] = false; int ran = rand() % 6; //use random number generation to determing the service time interval for the customer serviceEvent sTemp; //create a new service event for the server, for the current clock time PLUS the service time interval sTemp.clock_time = time + ran; sTemp.service_number = i; sEvent.push(sTemp); //add the service event to the event queue } } // output the summary cout << "Time: " << time << endl; //output the current time // output a heading for the visual prepresentation of the servers and the wait queue cout << "-------------------------------------------" << endl; cout << "line now serving wait queue" << endl; cout << "-------------------------------------------" << endl; //for each server for (int i = 0; i < num_servers; i++) { cout << setw(5) << i; cout << setw(10) << servers[i].ID; //output the server's index number (zero-based) // show the ID of the customer being served by that server(blank if idle) if (i == 0)// for server 0 only, show the IDs of customers in the wait queue cout << setw(15) << servers[i].ID; cout << endl; } cout << "-------------------------------------------" << endl; // summarize performance float sum = 0; float sum_Count = 0; float sum_Time = 0;
//if time is greater than zero if (time != 0) { //iterate over the served customers to get the average time between arrival and completion queue average = sDone; sum_Count = average.size(); //and if any, output the average of their "completion time" minus their "arrival time" while (!average.empty()) { sum_Time += (average.front().completion_time - average.front().arrival_time); average.pop(); } sum = sum_Time / sum_Count; }
if (sum_Count != 0) cout << "Avg time: " << sum; else cout << "Average Time: 0" << endl; if (turn_away_c != 0) //output the rate of customers turned away as total turned away divided by the current time cout << "Turned away per minute: " << (float)turn_away_c / time << endl; else cout << "Turned away permitue: 0" << endl; // if the user wants to quit, break out of this loop cout << "Press ENTER to continue, X-ENTER to exit..." << endl; getline(cin, buf); if (buf == "X" || buf == "x") break; else continue; } //clear memory delete[] servers; delete[] status; }
This program now has runtime error: terminating with uncaught exception of type char const* abort trap:6
test.txt should be like this:
4
2.5
8
3
10
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
