// so my program keep displaying wrong results.
// the problem is that it keep displaying only one airplane took off in every simulation. no matter what I input..
// program description
//https://drive.google.com/open?id=1xiIIij8nrMZmBPqxoDvuJIZmmrVJYheqDXKRlm7g8YM
#include<iostream>
#include <iomanip>
#include <math.h>
#include <queue>
usingnamespace std;
//to check if the plane going to queue
staticbool ifPlaneComingIntorunwayQueue(int avgInterval)
{
return rand() / ((double)RAND_MAX + 1) < 1.0 / avgInterval;
returntrue;
}
// to check if plane crashed
staticbool if_PlaneCrashed(int in, int out, int interval)
{
if (out - in > interval)
{
returntrue;
}
else
{
returnfalse;
}
}
int main()
{
cout << " The amount of time needed for one plane to land: " << endl;
int land_Time; //the amount of time needed for a plane to land (in minutes)
cin >> land_Time;
cout << " The amount of time needed for one plane to take off: " << endl;
int take_offTime; //the amount of time needed for a plane to takeoff (in minutes)
cin >> take_offTime;
cout << " The average amount of time between arrival of planes to the landing queue: " << endl;
int avg_ArrivalInterval; //the average amount of time between landing
cin >> avg_ArrivalInterval;
cout << " The average amount of time between arrival of planes to the takeoff queue: " << endl;
int avg_DepartureInterval; //the average amount of time between takeoff
cin >> avg_DepartureInterval;
cout << " The maximum amount of time that a plane can stay in the landing queue without running out of fuel and crashing: " << endl;
int stayinqueue; //the amount of time an airplane can stay in the air before running out of fuel
cin >> stayinqueue;
cout << " The total length of time to be simulated: " << endl;
int total_Time; //the total amount of time the simulation will run (in minutes)
cin >> total_Time;
int totalTimeSpentInLandingQueue = 0,
totalTimeSpentInTakeoffQueue = 0;
int N_PlanesLanded = 0; //number of planes that requested landing
int N_PlanesTookoff = 0; //number of planes that requested takeoff
int N_PlanesCrashed = 0; //number of planes that crashed
queue<int> landingQueue; // to put landing planes in queue
queue<int> takeoffQueue; // to put takeoff planes in queue
for (int i = 0; i < total_Time; ++i)
{
if (ifPlaneComingIntorunwayQueue(avg_ArrivalInterval))
{
landingQueue.push(i);
}
if (ifPlaneComingIntorunwayQueue(avg_DepartureInterval))
{
takeoffQueue.push(i);
}
while (true)
{
while (!landingQueue.empty() && if_PlaneCrashed(landingQueue.back(), i, stayinqueue))
{
landingQueue.pop();
N_PlanesCrashed++;
}
if (!landingQueue.empty())
{
int nextPlane = landingQueue.front();
landingQueue.pop();
N_PlanesLanded++;
totalTimeSpentInLandingQueue += (i - nextPlane);
int j;
for (j = i; j < land_Time + i && j < total_Time; ++j)
{
if (ifPlaneComingIntorunwayQueue(avg_ArrivalInterval))
{
landingQueue.push(j);
}
if (ifPlaneComingIntorunwayQueue(avg_DepartureInterval))
{
takeoffQueue.push(j);
}
}
i = j;
if (i >= total_Time)
{
break;
}
}
else
{
break;
}
}
if (!takeoffQueue.empty())
{
int nextPlane = takeoffQueue.front();
takeoffQueue.pop();
N_PlanesTookoff++;
totalTimeSpentInTakeoffQueue += (i - nextPlane);
int j;
for (j = i; j < take_offTime + i && j < total_Time; ++j)
{
if (ifPlaneComingIntorunwayQueue(avg_ArrivalInterval))
{
landingQueue.push(j);
}
if (ifPlaneComingIntorunwayQueue(avg_DepartureInterval))
{
takeoffQueue.push(j);
}
}
i = j;
}
}
while (!landingQueue.empty() && if_PlaneCrashed(landingQueue.back(), total_Time, stayinqueue))
{
landingQueue.pop();
N_PlanesCrashed++;
}
cout << "The number of planes that took off in the simulated time is " << N_PlanesTookoff << endl;
cout << "the number of planes that landed in the simulated time is " << N_PlanesLanded << endl;
cout << "the number of planes that crashed because they ran out of fuel before they could land is " << N_PlanesCrashed << endl;
cout << "the average time that a plane spent in the takeoff queue is " << totalTimeSpentInTakeoffQueue / (double)N_PlanesTookoff << endl;
cout << "the average time that a plane spent in the landing queue is " << totalTimeSpentInLandingQueue / (double)N_PlanesLanded << endl;
system("pause");
return 0;
}
Are you expecting rand() to return a floating point value from 0.0 to 1.0 ? Because that's not the case. It returns an integer from 0 to RAND_MAX. To turn that into a floating point value from 0.0 to 1.0, you need to divide by (double)RAND_MAX (or (double)RAND_MAX+1 if you don't want the slim possibility of 1.0).
Thanks for adding the code tags. But you should have re-copy/pasted your original code so that the original indentation would be preserved. It's a lot easier to read with the indentation. :-)
I don't understand your current question. Can you elaborate? If a certain sequence of inputs causes the problem, then what are those inputs?
There's another bug in ifPlaneComingIntorunwayQueue(). If average interval is 10 then you want a plane to arrive if a random number is less than 1/10, not greater than.
When writing a simulation, it's really helpful to add temporary code that prints the details of what's happening in the simulation as it runs. Here is your code with the fixes to ifPlaneComingTorunwayQueue, and code to print what the simulation is doing. In the next reply, I'll shows the output and some more comments.
#include<iostream>
#include <iomanip>
#include <math.h>
#include <queue>
usingnamespace std;
//to check if the plane going to queue
staticbool
ifPlaneComingIntorunwayQueue(int avgInterval)
{
double drand = rand() / (double) RAND_MAX;
if (drand < (1.0 / avgInterval))
returntrue;
elsereturnfalse;
}
// to check if plane crashed
staticbool
if_PlaneCrashed(int in, int out, int interval)
{
if (out - in > interval) {
returntrue;
} else {
returnfalse;
}
}
int
main()
{
cout << " The amount of time needed for one plane to land: " << endl;
int land_Time; //the amount of time needed for a plane to land (in minutes)
cin >> land_Time;
cout << " The amount of time needed for one plane to take off: " << endl;
int take_offTime; //the amount of time needed for a plane to takeoff (in minutes)
cin >> take_offTime;
cout << " The average amount of time between arrival of planes to the landing queue: "
<< endl;
int avg_ArrivalInterval; //the average amount of time between landing
cin >> avg_ArrivalInterval;
cout << " The average amount of time between arrival of planes to the takeoff queue: "
<< endl;
int avg_DepartureInterval; //the average amount of time between takeoff
cin >> avg_DepartureInterval;
cout <<
" The maximum amount of time that a plane can stay in the landing queue without running out of fuel and crashing: "
<< endl;
int stayinqueue; //the amount of time an airplane can stay in the air before running out of fuel
cin >> stayinqueue;
cout << " The total length of time to be simulated: " << endl;
int total_Time; //the total amount of time the simulation will run (in minutes)
cin >> total_Time;
int totalTimeSpentInLandingQueue = 0, totalTimeSpentInTakeoffQueue = 0;
int N_PlanesLanded = 0; //number of planes that requested landing
int N_PlanesTookoff = 0; //number of planes that requested takeoff
int N_PlanesCrashed = 0; //number of planes that crashed
queue < int >landingQueue; // to put landing planes in queue
queue < int >takeoffQueue; // to put takeoff planes in queue
for (int i = 0; i < total_Time; ++i) {
cout << "Top of loop t=" << i << '\n';
if (ifPlaneComingIntorunwayQueue(avg_ArrivalInterval)) {
cout << "Plane arrives on landing queue\n";
landingQueue.push(i);
}
if (ifPlaneComingIntorunwayQueue(avg_DepartureInterval)) {
cout << "Plane arrives on takeoff queue\n";
takeoffQueue.push(i);
}
while (true) {
while (!landingQueue.empty()
&& if_PlaneCrashed(landingQueue.back(), i, stayinqueue)) {
int nextPlane = landingQueue.front();
landingQueue.pop();
N_PlanesCrashed++;
cout << "Landing plane that arrives at " << nextPlane
<< " has crashed.\n";
}
if (!landingQueue.empty()) {
int nextPlane = landingQueue.front();
landingQueue.pop();
N_PlanesLanded++;
cout << "Landed plane that arrived at " << nextPlane << '\n';
totalTimeSpentInLandingQueue += (i - nextPlane);
int j;
for (j = i; j < land_Time + i && j < total_Time; ++j) {
if (ifPlaneComingIntorunwayQueue(avg_ArrivalInterval)) {
cout << j << ": Plane arrives on landing queue\n";
landingQueue.push(j);
}
if (ifPlaneComingIntorunwayQueue(avg_DepartureInterval)) {
cout << j << ": Plane arrives on takeoff queue\n";
takeoffQueue.push(j);
}
}
i = j;
if (i >= total_Time) {
break;
}
} else {
break;
}
}
if (!takeoffQueue.empty()) {
int nextPlane = takeoffQueue.front();
cout << "Launching plane that arrived at " << nextPlane << '\n';
takeoffQueue.pop();
N_PlanesTookoff++;
totalTimeSpentInTakeoffQueue += (i - nextPlane);
int j;
for (j = i; j < take_offTime + i && j < total_Time; ++j) {
if (ifPlaneComingIntorunwayQueue(avg_ArrivalInterval)) {
cout << j << ": Plane arrives on landing queue\n";
landingQueue.push(j);
}
if (ifPlaneComingIntorunwayQueue(avg_DepartureInterval)) {
cout << j << ": Plane arrives on takeoff queue\n";
takeoffQueue.push(j);
}
}
i = j;
}
}
cout << "End time reached\n";
while (!landingQueue.empty()
&& if_PlaneCrashed(landingQueue.back(), total_Time, stayinqueue)) {
int nextPlane = landingQueue.front();
cout << "Landing plane that arrives at " << nextPlane
<< " has crashed.\n";
landingQueue.pop();
N_PlanesCrashed++;
}
cout << "The number of planes that took off in the simulated time is " <<
N_PlanesTookoff << endl;
cout << "the number of planes that landed in the simulated time is " << N_PlanesLanded
<< endl;
cout <<
"the number of planes that crashed because they ran out of fuel before they could land is "
<< N_PlanesCrashed << endl;
cout << "the average time that a plane spent in the takeoff queue is " <<
totalTimeSpentInTakeoffQueue / (double) N_PlanesTookoff << endl;
cout << "the average time that a plane spent in the landing queue is " <<
totalTimeSpentInLandingQueue / (double) N_PlanesLanded << endl;
system("pause");
return 0;
}
The amount of time needed for one plane to land: 4
The amount of time needed for one plane to take off: 3
The average amount of time between arrival of planes to the landing queue: 10
The average amount of time between arrival of planes to the takeoff queue: 8
The maximum amount of time that a plane can stay in the landing queue without running out of fuel and crashing: 25
The total length of time to be simulated: 60
Top of loop t=0
Top of loop t=1
Top of loop t=2
Top of loop t=3
Top of loop t=4
Top of loop t=5
Plane arrives on landing queue
Landed plane that arrived at 5
6: Plane arrives on takeoff queue
7: Plane arrives on takeoff queue
Launching plane that arrived at 6
11: Plane arrives on takeoff queue
Top of loop t=13
Plane arrives on takeoff queue
Launching plane that arrived at 7
Top of loop t=17
Launching plane that arrived at 11
Top of loop t=21
Launching plane that arrived at 13
21: Plane arrives on takeoff queue
Top of loop t=25
Launching plane that arrived at 21
27: Plane arrives on landing queue // Why doesn't this plane land immediately?
Top of loop t=29
Landed plane that arrived at 27
29: Plane arrives on landing queue
Landed plane that arrived at 29 // It landed 2 planes at t=29
Top of loop t=38
Plane arrives on landing queue
Landed plane that arrived at 38
38: Plane arrives on takeoff queue
Launching plane that arrived at 38
Top of loop t=46
Top of loop t=47
Top of loop t=48
Plane arrives on takeoff queue
Launching plane that arrived at 48
49: Plane arrives on takeoff queue
Top of loop t=52
Launching plane that arrived at 49
Top of loop t=56
Top of loop t=57
Top of loop t=58
Plane arrives on landing queue
Landed plane that arrived at 58
End time reached
The number of planes that took off in the simulated time is 8
the number of planes that landed in the simulated time is 5
the number of planes that crashed because they ran out of fuel before they could land is 0
the average time that a plane spent in the takeoff queue is 4.25
the average time that a plane spent in the landing queue is 1.2
You can see some errors. You can fix them and greatly simplify your code by creating a new variable that indicates that the runway is busy. Let's call it busyUntil. When a plane starts to take off or land, you set busyUntil to the time when the runway will be available again.
Now your main simulation loop runs once for every minute and it handles all the possibilities. When I made this change the simulation seemed to run wonderfully. Here is what I did, with the comments in place but the code removed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
for (int i = 0; i < total_Time; ++i) {
cout << "Top of loop t=" << i << '\n';
// Queue up planes that arrive in this minute
// If the runway is busy then go to next minute
// process crashed planes
// If a plane is in the landing queue then land it and set busyUntil
// else if a plane is in the takeoff queue then launch it and update busyUntil
}
// Process planes that crash after simulation ends.