Finishing Parking Garage program

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <ctime>

using namespace 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;
	    }
	    else if (currentTime < 25200){ // time 0600-0700
	        int nextRange = 180;
	        nextTime = rand() % nextRange;
	    }
	    else if (currentTime < 28800){ // time 0700-0800
	        int next_range = 60;
	        nextTime = rand() % nextRange;
	    }
	    else if (currentTime < 39600){ // time 0800-1100
	        int nextRange = 12;
	        nextTime = rand() % nextRange;
	    }
	    else if (currentTime < 54000){ // time 1100-1400
	        int nextRange = 60;
	        nextTime = rand() % nextRange;
	    }
	    else if (currentTime < 68400){ // time 1400-1800
	        int nextRange = 180;
	        nextTime = rand() % nextRange;
	    }
	    else if (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;
}
Anybody?
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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) { }
        static bool 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;
        }
        else if ((*it).type == GarageEventType::DEPARTURE)
        {
            --currentCars;
        }
    }
    return maxCars;
}
Topic archived. No new replies allowed.