Help with a Counter while using queues

Hey Everyone!

I need a little help for one of my assignments. I don't really need exact coding or anything, just some ideas on how to go about doing this. Basically the program is for managing a parking garage. There are 2 garages and the street is used if necessary. I have all of the program written for the arrival and departures of cars in which a queue is used for each garage. Once the first garage is full then cars are placed in the second garage.

Now the hard part is somehow counting how many times a car has to be moved. So say for example a car that is 3rd position in the First garage queue was to be removed... Then the cars in position 1 and 2 would have to be pulled out to allow for the car to depart. I need to count how many times a car is moved before it departs. Any ideas on how I should go about doing this?

Any help would be greatly appreciated!
Is there a specific number on how many cars in each garage?
Well the maxqueue for all three queues (garage 1, garage2, and the street) are all 5 so a total of 15. If that makes sense, and what you're asking?
Last edited on
Excuse me if I make you facepalm but why are you using queues? Is it required by your assignment that the garages be 1x5, single-end access? That's odd.

I see how being restricted to queues is difficult, but it's hard for me to imagine how to do it without having a sufficient basis. May I see your code, or could you ask a more specific question?
Last edited on
It's the assignment, used to help us better understand how to use them i guess. But yes it is required to be like that... Here's the code:

Scratch-em-up is garage 1
knock-em-dead is garage 2

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <iostream>
#include <fstream>

using namespace std;

const int maxqueue = 5;
class queue_type
{
public:
	void clear_queue();
	bool empty_queue();
	bool full_queue();
	void insert_queue(int numb);
	void delete_queue(int& numb);
	int queue[6];
	int front, rear;
};
	queue_type a, b, c, a2, b2, c2;

//==============

int main()
{
	int x = 0;
	int g = 0; //counter for exit output
	int q = 0; //temp value for popping queue
	char n; //n is arrive, depart, or exit; while x is numb number
	a.clear_queue();
	b.clear_queue();
	c.clear_queue();
	a2.clear_queue();
	b2.clear_queue();
	c2.clear_queue();

	// WELCOME CODE
	
	do
	{
	cout << "Welcome to Scratch Yard Inc. Press a for arrival, d for departure, or q to quit\n" << endl;
	
	cout << "Enter selection letter : ";
	cin >> n;



	if ((!(n=='q')) && (!(n=='Q')))
	{
			cout << "\nEnter a license number : ";
			cin >> x;
	}


	// ARRIVAL CODE

	while ((n=='a') || (n=='A'))
	{
	if (!(a.full_queue()))
	{
		a.insert_queue(x);
		cout << "Vehicle has been parked in Scratch-em-up." << endl;
		
	}
	else if (!(b.full_queue()))
	{
		b.insert_queue(x);
		cout << "Scratch-em-up is full.  Vehicle has been parked in Knock-em-dead." << endl;
		
	}
	else if (!(c.full_queue()))
	{
		c.insert_queue(x);
		cout << "Both garages are full.  Vehicle has been parked on the street." << endl;
		
	}
	else 
	{
		cout << "All garages and the street are full." << endl << endl;
		
	}
	}//End while

	
	// DEPARTURE CODE
	
	if ((n=='d') || (n=='D')) //n is decision, x is plate number
	{
		while (!(a.empty_queue()))
		{
			a.delete_queue(q);
			a2.insert_queue(q);
		}//end while

		while (!(b.empty_queue()))
		{
			b.delete_queue(q);
			b2.insert_queue(q);
		}//end while

		while (!(c.empty_queue()))
		{
			c.delete_queue(q);
			c2.insert_queue(q);
		}//end while

		while (!(a2.empty_queue())) //A queue filling
		{
			a2.delete_queue(q);
				if (x == q)
				{
					cout << "Car with plate " << x << " has left Scratch-em-up." << endl;
				}
				if (!(x == q))
				{
					a.insert_queue(q);
				}
		}//end while

		while (!(b2.empty_queue())) //B queue filling
		{
			b2.delete_queue(q);
				if (x == q)
				{
					cout << "Car with plate " << x << " has left Knock-em-dead." << endl;
					}
				if (!(x == q))
				{
					if (!(a.full_queue()))
					{
						a.insert_queue(q);
					}
					
					if ((a.full_queue()) && (!(b.full_queue())))
					{
						b.insert_queue(q);
					}
				}
		}

		while (!(c2.empty_queue())) //C queue filling
		{
			c2.delete_queue(q);
				if (x == q)
				{
					cout << "Car with plate " << x << " has left the street." << endl;
				}
				if (!(x == q))
				{
					if (!(b.full_queue()))
					{
						b.insert_queue(q);
					}
					
					if ((b.full_queue()) && (!(c.full_queue())))
					{
						c.insert_queue(q);
					}
				}
		}//end while
	}

	}//end do
	while ((!(n == 'q')) && (!(n == 'Q')));
	// below is Scratch-em-up exit output
	if ((n == 'Q') || (n == 'q'))
	{
		while (!(a.empty_queue()))
		{
			a.delete_queue(q);
			g++;
		}
		if (!(g == 0))
		{
		cout << "\n\nYou are leaving the program with " << g << " cars in Scratch-em-up." << endl;
		g = 0;
		}
		else
		{
			cout << "\n\nYou are leaving the program with no cars in either of the garages, or on the street." << endl;
		}
	}
	
	// below is knock-em-dead exit output
	if (((n == 'Q') || (n == 'q')) && (!(b.empty_queue())))
	{
		while (!(b.empty_queue()))
		{
			b.delete_queue(q);
			g++;
		}
		cout << "You are leaving the program with " << g << " cars Knock-em-dead." << endl;
		g = 0;
	}
	// below is the street exit output
	if (((n == 'X') || (n == 'x')) && (!(c.empty_queue())))
	{
		while (!(c.empty_queue()))
		{
			c.delete_queue(q);
			g++;
		}
		cout << "You are leaving the program with " << g << " cars on the street." << endl;
		g = 0;
	}



	return 0;
}

//===============
void queue_type::clear_queue()
{
	front = maxqueue;
	rear = maxqueue;
}
//===============
bool queue_type::empty_queue()
{
	if (rear == front)
		return true;
	else
		return false;
}
//===============
bool queue_type:: full_queue()
{
	int querear;
	if (rear == maxqueue)
		querear = 0;
	else
		querear = rear + 1;
	if (querear == front)
		return true;
	else
		return false;
}
//================
void queue_type::insert_queue(int numb)
{
	if (rear == maxqueue)
		rear = 0;
	else
		rear = rear + 1;
	queue[rear] = numb;
}
//=================
void queue_type::delete_queue(int& numb)
{
	if (front == maxqueue)
		front = 0;
	else
		front = front + 1;
	numb = queue[front];
}
//================= 


Now I need to figure out how to output the amount of times a car is moved before it departs. I believe it's going to have to do with my 'temporary' queues (a2,b2,c2), but honestly I'm lost right now. Also, from the directions, the count will always be at least 1 unless the car is departing from the street.
anyone have any ideas? Will be back in the morning! I'm thinking I may have to do something with increasing a counter every time I switch arrays.. But I'm not sure exactly how to do that!
So based on your assignment .A garage is actually a better example of a "stack" rather then a "queue". A stack operates as first in last out, where a queue is first in first out. now if we are certin that your garage has only one entry/exit point then the idea of pulling the cars 1 & 2 out, and then removing car 3 is correct. As what you discribed, it would be better if instead of using a random temp queue you used your street as a temp queue (if possible) if not, then you could use only one temp queue you dont need 3 different temp queues.

Here is an example:
lets say you have 4 cars that arrive in this order : (0056)(0048)(0087)(0032)
your Scratch-em-up queue look like this:
1
2
3
[(0056)][(0048)][(0087)][(0032)][(empty)] // Scratch-em-up queue
   0        1       2       3       4
// you might also want to defind empty it would make it easier. 

At this point your front should be pointing at position[0] & your rear should be at position[3].
Then lets say car (0087) would like to depart. So use your street to move out (copy) cars(0056) & (0048) in it. Maybe have a push & pop functions to help copy/move cars to empty the queue so that the car(0087) can depart .What pop function could do is remove cars from the queue and return the car being removed and replace that position with empty. Also a push function which will insert car into queue. So your main/depart function maybe would say something like:

1
2
    street_queue.push(Scratch-em-up.pop());// street_queue could also be the temp queue
    count_move++;// keeps count of the moves 

Now, all you have to do have a function called depart which will search for the car that needs to depart, then move the cars that are blocking em and removed the car from the garage.
you also might want to have a repark function which reparks the cars.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[(0056)][(0048)][(0087)][(0032)][(empty)] // Scratch-em-up queue (arrive)
   0        1       2       3       4
[(0056)][(0048)][(empty)][(empty)][(empty)] // temp queue (temp park)
   0        1       2       3       4
[(empty)][(empty)][(0087)][(0032)][(empty)] // Scratch-em-up queue(remove the car
    0        1       2       3       4		//that needs to move)
[(empty)][(empty)][(empty)][(0032)][(empty)] // Scratch-em-up queue(car removed)
    0        1       2       3       4		
[(empty)][(empty)][(empty)][(empty)][(empty)] // temp queue(re park cars back
    0        1       2       3       4		//to Scratch-em-up queue
[(0056)][(0048)][(empty)][(0032)][(empty)] // Scratch-em-up queue
    0        1       2       3       4
[(0056)][(0048)][(0032)][(empty)][(empty)] // Scratch-em-up queue (repark by 
   0        1       2       3       4		//moving (0032) to position[2]
//- insert(4 cars)
//- pop & push from garage to temp
//- pop from garage the car that needs to depart
//- pop & push from temp to garage
//- repark (resort) garage. 


Note:that you front and rear will never rap around the queue, meaning your Front or rear will never pass maxqueue which is 5 cars.

Hope this gives you some kind of idea?
Topic archived. No new replies allowed.