Suppose that a certain airport has one runway. Each airplane takes landingTime m
ID: 3670728 • Letter: S
Question
Suppose that a certain airport has one runway. Each airplane takes landingTime minutes to land and takeOffTime to take off, and that, on the average, planes arrive at random instants of time. There are two types of queues: a queue of airplanes waiting to land and a queue of airplanes waiting to take off. Because it is more expensive to keep a plane airborne than have one waiting on the ground, we assume that the airplanes in the landing queue have prioritiy over those in the takeoff queue.
Write a program to simulate this airport's operation. You might assume a simulated clock that advances in five-minute intervals. For each tic generate two random numbers. If the first is less than landingRate, a landing arrival has occurred and is added to the landing queue; and if the second is less than takeOffRate, a takeoff arrival has occurred and is added to the takeoff queue. Your program should also calculate the average queue length and the average time that an airplane spends in a queue.
Use a linked list implementation of a queue in Java
Explanation / Answer
main.cpp
// AirplaneSimulation.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <new>
#include <sstream>
#include <stdlib.h>
#include <time.h>
#include "queue.h"
#include <thread> // std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds
#include "Airplane.h"
using namespace std;
#define RAND_MAX 21474
int _tmain(int argc, _TCHAR* argv[])
{
Airplane averagePlane(6.0, 4.0);
Queue landingQ(30);
Queue takeOffQ(30);
bool runWay = false;
double lrRand, trRand;
int i = 0;
srand(time(NULL));
Airplane genericAirplane;
while (i < 500){
std::this_thread::sleep_for(std::chrono::milliseconds(200));
lrRand = (double)rand()/RAND_MAX;
trRand = (double)rand()/RAND_MAX;
printf("%0.3f %0.3f ", lrRand, trRand);
if (lrRand < averagePlane.getLandingTime()){
landingQ.Enqueue(genericAirplane);
}
if (trRand < averagePlane.getTakeOffTime()){
takeOffQ.Enqueue(genericAirplane);
}
i++;
}
int num;
std::cin >> num;
int * foo;
foo = new (nothrow) int[num];
if (foo == nullptr){
cout << "Error: memory could not be allocated";
}
else{
cout << "Job done well";
}
return 0;
}
/*
int main()
{
Queue line(3);
int qv;
qv = 77;
cout << "Adding queue value: " << qv << endl;
line.Enqueue(qv);
qv = 78;
cout << "Adding queue value: " << qv << endl;
line.Enqueue(qv);
qv = 79;
cout << "Adding queue value: " << qv << endl;
line.Enqueue(qv);
qv = 80;
cout << "Adding queue value: " << qv << endl;
line.Enqueue(qv);
qv = line.Dequeue();
cout << "Removed " << qv << " from the queue" << endl;
qv = line.Dequeue();
cout << "Removed " << qv << " from the queue" << endl;
qv = line.Dequeue();
cout << "Removed " << qv << " from the queue" << endl;
qv = line.Dequeue();
}
*/
Queue.cpp
#include "stdafx.h"
#include "queue.h"
#include "Airplane.h"
Queue::Queue(int qs)
{
ArraySize = qs;
array = ArraySize ? new Airplane[ArraySize] : NULL;
Clear();
}
Queue::~Queue()
{
if (array) delete[] array;
}
void Queue::Clear()
{
CurrentSize = 0;
Front = 0;
Rear = ArraySize - 1;
}
// Add element to the list
void Queue::Enqueue(Airplane v)
{
if (CurrentSize >= ArraySize)
{
cout << "Warning: queue overflow" << endl;
return;
}
if (Rear == ArraySize - 1)
Rear = 0;
else
Rear++;
array[Rear] = v;
CurrentSize++;
}
// Remove element from the list
Airplane Queue::Dequeue()
{
Airplane tmp;
if (IsEmpty())
{
cout << "Warning: queue underflow" << endl;
return tmp;
}
tmp = array[Front];
if (Front == ArraySize - 1)
Front = 0;
else
Front++;
CurrentSize--;
return tmp;
}
queue.h
//define queue
#include "stdafx.h"
#include <iostream>
#include "Airplane.h"
using namespace std;
#define DEFAULT_SIZE 5
class Queue
{
private:
Airplane *array;
int Front, Rear;
int ArraySize;
int CurrentSize;
public:
Queue(int qs = DEFAULT_SIZE);
~Queue();
// Add element to the queue
void Enqueue(Airplane v);
// Remove element from the queue
Airplane Dequeue();
// Clear the queue
void Clear();
// Is queue empty?
short IsEmpty() { return (CurrentSize == 0) ? 1 : 0; }
};
stdafx.cpp
// stdafx.cpp : source file that includes just the standard includes
// AirplaneSimulation.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
stdafx.h
// stdafx.cpp : source file that includes just the standard includes
// AirplaneSimulation.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
targetver.h
#pragma once
// Including SDKDDKVer.h defines the highest available Windows platform.
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
#include <SDKDDKVer.h>
Airplane.cpp
/*Suppose that a certain airport has one runway, that each airplane takes landingTime minutes to land and takeOffTime to take off, and that on the average, takeOffRate planes take off and landingRate planes land each hour. Assume the the planes arrive at random instants of time. There are two types of queues: a queue of airplanes waiting to land and a queue of airplanes waiting to take off. Because it is more expensive to keep a plane airborne than to keep one waiting on the ground, we assume that the airplanes in the landing queue have priority over those in the takeoff queue.
Write a program to simulate this airport's operation. You might assume a simulated clock that advances in one minute intervals. For each minute, generate two random numbers: If the first is less than landingRate/60, a "landing arrival" has occurred and is added to the landing queue; and if the second is less than takeOffRate/60, a "takeoff arrival" has occurred, and is added to the takeoff queue. Next, check whether the runway is free. If it is, first check whether the landing queue is non-empty, and if so, allow the first airplane to land; otherwise consider the takeoff queue.
Have the program calculate the average queue length and the average time that an airplane spends in a queue. You should also investigate the effect of varying arrival and departure rates to simulate the prime and slack times of the day, and what happens if the amount of time to land or take off is increased or decreased.
Create your own data structures.*/
#include <iostream>
#include "stdafx.h"
#include "Airplane.h"
using namespace std;
Airplane::Airplane(){
setValues(6.34, 3.3);
}
Airplane::~Airplane(){
setValues(0, 0);
}
void Airplane::setValues(double l, double t) {
landingTime = l;
takeOffTime = t;
}
Airplane.h
#include "stdafx.h"
using namespace std;
class Airplane {
private:
double landingTime, takeOffTime;
public:
Airplane() {};
Airplane(double l, double t) : landingTime(l), takeOffTime(t) { };
~Airplane();
double getTakeOffTime(){ return takeOffTime; }
double getLandingTime() { return landingTime; }
void setValues(double landingTime, double takeOffTime);
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.