I'm stuck on an assignment, and I don't understand how to finish implementation. I've been told that I have the majority of what I need, but nobody can help me finish it, or better yet help me understand how to finish it. I just need a good nudge in the right direction, or someone with the patience to help. Please be gentle, I'm trying my hardest in this class, and finishing and understanding this code would mean the world to me.
The assignment is to replicate a parking garage, and output the maximum size it needs to be to hold all of the cars in the vector "garage" at any given time. The only output needs to be the maximum size needed. Arrival times are given in the main function.
No matter what time a car arrived, each car has a departure (exit) time which is a uniform distribution in the range 300 . . . 14400 seconds past the arrival time.
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
usingnamespace std;
class Car
{ // creates a car object, basically just stores arrival and departure times
public:
Car (int); // create a car with an arrival time
int getArrivalTime(); // view arrival time
int getDepartureTime(); //view departure time
private:
int arrivalTime; // arrival time of car
int departureTime; // departure time of car
int parkedTime; // how long the car stays in the garage
int getDepartureTime(); // generates the time car stays in garage
};
class Garage{
public:
vector <Car> arrivals; //starts with all cars before they arrive
int number(){ // returns the total number of car arrivals
return arrivals.size();
}
void gar_sort(); // sift the list out
private:
vector<Car> in_garage; // basically just the garage, after arrival before departure
};
int main()
{
int garageSize = 0;
Garage garage;
int seed = time(NULL); // seed value for pseudo-random number generator
srand(seed);
int currentTime = 0; //current time since midnight in seconds
int stopTime = 86400;
while (currentTime < stopTime){
/*could be shortened since rates are the same at two different times, but it
might be better to leave it to be able to set rates differently later */
int nextTime;
if (currentTime < 21600){ // time 0000-0600
int nextRange = 500;
nextTime = rand() % nextRange;
}
elseif (currentTime < 25200){ // time 0600-0700
int nextRange = 180;
nextTime = rand() % nextRange;
}
elseif (currentTime < 28800){ // time 0700-0800
int next_range = 60;
nextTime = rand() % nextRange;
}
elseif (currentTime < 39600){ // time 0800-1100
int nextRange = 12;
nextTime = rand() % nextRange;
}
elseif (currentTime < 54000){ // time 1100-1400
int nextRange = 60;
nextTime = rand() % nextRange;
}
elseif (currentTime < 68400){ // time 1400-1800
int nextRange = 180;
nextTime = rand() % nextRange;
}
elseif (currentTime < 86400){ // time 1800-2400
int nextRange = 500;
nextTime = rand() % nextRange;
}
cout << "Test" << endl; // ----------------------------------------------------debugging only
currentTime += nextTime;
Car car (currentTime);
garage.arrivals.push_back(car);
garage.gar_sort();
};
// Car definition functions
Car::Car(int t){ // constructor for cars
arrivalTime = t;
parkedTime = getDepartureTime();
departureTime = arrivalTime + parkedTime;
}
int Car::getArrivalTime(){ // return arrival time of car
return arrivalTime;
}
int Car::getDepartureTime(){ // return departure time of car
return departureTime;
}
int Car::getDepartureTime(){ // generate staying time of car
int val;
while (300 > val){
val = rand() % 14400;
}
return val;
}
// Garage definition functions
void gar_sort(){ // sift the list out
int total; // this will be the greatest total number of cars in the garage from any given time
cout << "The garage size needs to be " << total << endl;
}
You may want to consider another class. Something like GarageEvent. Each object of type GarageEvent would have two members: one to indicate what time the event happens, and another to indicate whether it is an ARRIVAL event or a DEPARTURE event.
Every time you add a car to the garage, you add two events: an arrival event and a departure event. So you would end up with a container of all the events that take place in a garage over the course of the day.
When you want to determine the largest number of cars the garage had in it, you sort all your GarageEvents by what time they occur. Iterate over this sorted collection, keeping track of both the current number of cars and the maximum number of cars seen at any one time. When an ARRIVAL event occurs, add 1 to the current number of cars. If this new number is larger than the max number of cars you've ever seen, update max to reflect this. When a DEPARTURE event occurs, subtract 1 from the current number of cars.
class Garage{
public:
void addCarToGarage(Car c);
int getMaxSizeNeeded();
private:
//could have used a boolean 'isArrival' and set to true or false,
//but using an enum is more expressive
enum GarageEventType { ARRIVAL, DEPARTURE };
class GarageEvent //private class, because only Garage is going to use it
{
public:
int time;
GarageEventType type;
GarageEvent(int t, GarageEventType ge) : time(t), type(ge) { }
staticbool sortFunc(GarageEvent i, GarageEvent j) { return i.time < j.time; }
};
vector <Car> allCars; //a vector containing all the cars that ever use a garage during the day
vector <GarageEvent> garageEvents; //a vector containing every event that occurs during the day
};
//...
// Garage definition functions
void Garage::addCarToGarage(Car c)
{
allCars.push_back(c);
GarageEvent gcArr(c.getArrivalTime(), GarageEventType::ARRIVAL);
GarageEvent gcDep(c.getDepartureTime(), GarageEventType::DEPARTURE);
garageEvents.push_back(gcArr);
garageEvents.push_back(gcDep);
}
int Garage::getMaxSizeNeeded()
{
//sort by time! (see how sortFunc is defined above)
std::sort(garageEvents.begin(), garageEvents.end(), GarageEvent::sortFunc);
int maxCars = 0;
int currentCars = 0;
for(vector<GarageEvent>::iterator it = garageEvents.begin();
it != garageEvents.end();
++it)
{
if ((*it).type == GarageEventType::ARRIVAL)
{
++currentCars;
if(currentCars > maxCars) maxCars = currentCars;
}
elseif ((*it).type == GarageEventType::DEPARTURE)
{
--currentCars;
}
}
return maxCars;
}