So I have to use schedule aircrafts to the gates. Time is measured in minutes st
ID: 3677934 • Letter: S
Question
So I have to use schedule aircrafts to the gates. Time is measured in minutes starting at 0.
I have a Stack for the number of gates(which is input through user)
So the picture below shows for example the arrival in minutes (2) LAX -> JFK. I have created the planes departure in an array it is dep[i]. The departure is 62 minutes.
I am confused on how I set this up. I have the flight numbers, arrival and depature in seperate arrays but the airplane gates in a Stack. Should I put the flights, arrival and depature in a queue. ( not sure if each line in 1 queue or seperate each flight, arrival, and depature in queues. If so how will i implement the queue...
#include <iostream>
#include <string>
#include <queue>
#include <time.h>
#include <stdlib.h>
#include <iomanip>
#include "vector"
#define MAX_SIZE 50
using namespace std;
/*
class MinHeap
{
private:
vector<int> _vector;
void BubbleDown(int index);
void BubbleUp(int index);
void Heapify();
public:
MinHeap(int* array, int length);
MinHeap(const vector<int>& vector);
MinHeap();
void Insert(int newValue);
int GetMin();
void DeleteMin();
};
MinHeap::MinHeap(int* array, int length) : _vector(length)
{
for (int i = 0; i < length; ++i)
{
_vector[i] = array[i];
}
Heapify();
}
MinHeap::MinHeap(const vector<int>& vector) : _vector(vector)
{
Heapify();
}
MinHeap::MinHeap()
{
}
void MinHeap::Heapify()
{
int length = _vector.size();
for (int i = length - 1; i >= 0; --i)
{
BubbleDown(i);
}
}
void MinHeap::BubbleDown(int index)
{
int length = _vector.size();
int leftChildIndex = 2 * index + 1;
int rightChildIndex = 2 * index + 2;
if (leftChildIndex >= length)
return; //index is a leaf
int minIndex = index;
if (_vector[index] > _vector[leftChildIndex])
{
minIndex = leftChildIndex;
}
if ((rightChildIndex < length) && (_vector[minIndex] > _vector[rightChildIndex]))
{
minIndex = rightChildIndex;
}
if (minIndex != index)
{
//need to swap
int temp = _vector[index];
_vector[index] = _vector[minIndex];
_vector[minIndex] = temp;
BubbleDown(minIndex);
}
}
void MinHeap::BubbleUp(int index)
{
if (index == 0)
return;
int parentIndex = (index - 1) / 2;
if (_vector[parentIndex] > _vector[index])
{
int temp = _vector[parentIndex];
_vector[parentIndex] = _vector[index];
_vector[index] = temp;
BubbleUp(parentIndex);
}
}
void MinHeap::Insert(int newValue)
{
int length = _vector.size();
_vector[length] = newValue;
BubbleUp(length);
}
int MinHeap::GetMin()
{
return _vector[0];
}
void MinHeap::DeleteMin()
{
int length = _vector.size();
if (length == 0)
{
return;
}
_vector[0] = _vector[length - 1];
_vector.pop_back();
BubbleDown(0);
}
*/
//-------------------------------------------------------------------------------
class Stack
{
private:
int A[MAX_SIZE]; // array to store the stack
int top; // variable to mark the top index of stack.
public:
// constructor
Stack()
{
top = -1; // for empty array, set top = -1
}
// Push operation to insert an element on top of stack.
void Push(int x)
{
if (top == MAX_SIZE - 1) { // overflow case.
printf("Error: stack overflow ");
return;
}
A[++top] = x;
}
// Pop operation to remove an element from top of stack.
void Pop()
{
if (top == -1) { // If stack is empty, pop should throw error.
printf("Error: No element to pop ");
return;
}
top--;
}
// Top operation to return element at top of stack.
int Top()
{
return A[top];
}
// This function will return 1 (true) if stack is empty, 0 (false) otherwise
int IsEmpty()
{
if (top == -1) return 1;
return 0;
}
// ONLY FOR TESTING - NOT A VALID OPERATION WITH STACK
// This function is just to test the implementation of stack.
// This will print all the elements in the stack at any stage.
void Print() {
int i;
printf("Stack: ");
for (i = 0; i <= top; i++)
printf("%d ", A[i]);
printf(" ");
}
};
int main() {
srand(time(NULL));
priority_queue<int> queue;
int n;
int dep[14];
//int ngate[n];
int flight[15] = { 1000,1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1011,1012,1013 };
int arrival[14] = { 2,3,4,9,11,14,17,19,28,33,37,44,49,55 };
int time = 0;
int endTime = 1440;
int numOfgesAv;
Stack gates;
int temp;
string ev1[14] = { "LAX continuing to JFK","SMF continuing to RDU","IAH continuing to MSP","ABQ continuing to ATL","SJC continuing to BOS","PHX continuing to MIA","SFO continuing to ORD","SLC continuing to AUS","TUL continuing to ONT","DCA continuing to LAS","BNA continuing to SEA","MAF continuing to AMA","HOS continuing to DEN","ELP continuing to MEM" };
cout << "Number of gates (10-40): ";
cin >> n;
while (n<10 || n>40) {
cout << "Input a value from 10-40: ";
cin >> n;
}
//-------------------------------------------
for (int i = 1; i <= n; i++) {
queue.push(i);
}
for (int i = 0; i<14; i++) {
int chance = rand() % 100 + 1;
if (chance >= 40) {
int delay = rand() % 40 + 20;
dep[i] = arrival[i] + delay + 60;
cout << dep[i] << endl;
}
else if (chance<40) {
dep[i] = arrival[i] + 60;
}
}
//--------------------------------------------
cout << "~_~_~_~_~_~_~AIRLINE~_~_~_~_~_~_~" << endl;
cout << "GATE#" << "||" << "FLIGHT#" << "||" << "ARRIVAL" << "||" << "EVENT" << " ||" << "DEPARTING" << endl;
while (time < endTime) {
time++;
cout << time << endl;
for (int i = 0; i < 14; i++) {
if (!queue.empty()) {
cout << " " << left << setw(2) << queue.top() << right << setw(2) << "||" << flight[i] << " ||" << setw(2) << arrival[i] << " ||" << ev1[i] << "||" << dep[i] << endl;
//queue.pop();
}
else {
cout << "~_~_~_~_~_~AIRPORT FULL~_~_~_~_~_~" << endl;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (arrival[j] = time) {
gates.Push(j);
//queue.pop();
n--;
}
if (dep[i] = time) {
gates.Pop();
n++;
}
if (n == 0) {
cout << "Airport Full" << endl;
}
}
}
}
Explanation / Answer
#include <iostream>
#include <string>
#include <queue>
#include <time.h>
#include <stdlib.h>
#include <iomanip>
#include "vector"
#define MAX_SIZE 50
using namespace std;
/*
class MinHeap
{
private:
vector<int> _vector;
void BubbleDown(int index);
void BubbleUp(int index);
void Heapify();
public:
MinHeap(int* array, int length);
MinHeap(const vector<int>& vector);
MinHeap();
void Insert(int newValue);
int GetMin();
void DeleteMin();
};
MinHeap::MinHeap(int* array, int length) : _vector(length)
{
for (int i = 0; i < length; ++i)
{
_vector[i] = array[i];
}
Heapify();
}
MinHeap::MinHeap(const vector<int>& vector) : _vector(vector)
{
Heapify();
}
MinHeap::MinHeap()
{
}
void MinHeap::Heapify()
{
int length = _vector.size();
for (int i = length - 1; i >= 0; --i)
{
BubbleDown(i);
}
}
void MinHeap::BubbleDown(int index)
{
int length = _vector.size();
int leftChildIndex = 2 * index + 1;
int rightChildIndex = 2 * index + 2;
if (leftChildIndex >= length)
return; //index is a leaf
int minIndex = index;
if (_vector[index] > _vector[leftChildIndex])
{
minIndex = leftChildIndex;
}
if ((rightChildIndex < length) && (_vector[minIndex] > _vector[rightChildIndex]))
{
minIndex = rightChildIndex;
}
if (minIndex != index)
{
//need to swap
int temp = _vector[index];
_vector[index] = _vector[minIndex];
_vector[minIndex] = temp;
BubbleDown(minIndex);
}
}
void MinHeap::BubbleUp(int index)
{
if (index == 0)
return;
int parentIndex = (index - 1) / 2;
if (_vector[parentIndex] > _vector[index])
{
int temp = _vector[parentIndex];
_vector[parentIndex] = _vector[index];
_vector[index] = temp;
BubbleUp(parentIndex);
}
}
void MinHeap::Insert(int newValue)
{
int length = _vector.size();
_vector[length] = newValue;
BubbleUp(length);
}
int MinHeap::GetMin()
{
return _vector[0];
}
void MinHeap::DeleteMin()
{
int length = _vector.size();
if (length == 0)
{
return;
}
_vector[0] = _vector[length - 1];
_vector.pop_back();
BubbleDown(0);
}
*/
//-------------------------------------------------------------------------------
class Stack
{
private:
int A[MAX_SIZE]; // array to store the stack
int top; // variable to mark the top index of stack.
public:
// constructor
Stack()
{
top = -1; // for empty array, set top = -1
}
// Push operation to insert an element on top of stack.
void Push(int x)
{
if (top == MAX_SIZE - 1) { // overflow case.
printf("Error: stack overflow ");
return;
}
A[++top] = x;
}
// Pop operation to remove an element from top of stack.
void Pop()
{
if (top == -1) { // If stack is empty, pop should throw error.
printf("Error: No element to pop ");
return;
}
top--;
}
// Top operation to return element at top of stack.
int Top()
{
return A[top];
}
// This function will return 1 (true) if stack is empty, 0 (false) otherwise
int IsEmpty()
{
if (top == -1) return 1;
return 0;
}
// ONLY FOR TESTING - NOT A VALID OPERATION WITH STACK
// This function is just to test the implementation of stack.
// This will print all the elements in the stack at any stage.
void Print() {
int i;
printf("Stack: ");
for (i = 0; i <= top; i++)
printf("%d ", A[i]);
printf(" ");
}
};
int main() {
srand(time(NULL));
priority_queue<int> queue;
int n;
int dep[14];
//int ngate[n];
int flight[15] = { 1000,1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1011,1012,1013 };
int arrival[14] = { 2,3,4,9,11,14,17,19,28,33,37,44,49,55 };
int time = 0;
int endTime = 1440;
int numOfgesAv;
Stack gates;
int temp;
string ev1[14] = { "LAX continuing to JFK","SMF continuing to RDU","IAH continuing to MSP","ABQ continuing to ATL","SJC continuing to BOS","PHX continuing to MIA","SFO continuing to ORD","SLC continuing to AUS","TUL continuing to ONT","DCA continuing to LAS","BNA continuing to SEA","MAF continuing to AMA","HOS continuing to DEN","ELP continuing to MEM" };
cout << "Number of gates (10-40): ";
cin >> n;
while (n<10 || n>40) {
cout << "Input a value from 10-40: ";
cin >> n;
}
//-------------------------------------------
for (int i = 1; i <= n; i++) {
queue.push(i);
}
for (int i = 0; i<14; i++) {
int chance = rand() % 100 + 1;
if (chance >= 40) {
int delay = rand() % 40 + 20;
dep[i] = arrival[i] + delay + 60;
cout << dep[i] << endl;
}
else if (chance<40) {
dep[i] = arrival[i] + 60;
}
}
//--------------------------------------------
cout << "~_~_~_~_~_~_~AIRLINE~_~_~_~_~_~_~" << endl;
cout << "GATE#" << "||" << "FLIGHT#" << "||" << "ARRIVAL" << "||" << "EVENT" << " ||" << "DEPARTING" << endl;
while (time < endTime) {
time++;
cout << time << endl;
for (int i = 0; i < 14; i++) {
if (!queue.empty()) {
cout << " " << left << setw(2) << queue.top() << right << setw(2) << "||" << flight[i] << " ||" << setw(2) << arrival[i] << " ||" << ev1[i] << "||" << dep[i] << endl;
//queue.pop();
}
else {
cout << "~_~_~_~_~_~AIRPORT FULL~_~_~_~_~_~" << endl;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (arrival[j] = time) {
gates.Push(j);
//queue.pop();
n--;
}
if (dep[i] = time) {
gates.Pop();
n++;
}
if (n == 0) {
cout << "Airport Full" << endl;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.