Code Simulation issue

Hi there,

I have been working on a program that has the following features and have come pretty close to what I believe is a final product.

1) Choose a random integer from 1 to 4 to determine the minute at which the first customer
arrives.

2) At the first customer’s arrival time:
Determine customer’s service time (random integer from 1 to 4);
Begin servicing the customer;
Schedule arrival time of next customer (random integer 1 to 4 added to the current time).

3) For each minute of the day:
If the next customer arrives,
Say so, enqueue the customer, and schedule the arrival time of the next
customer;

If service was completed for the last customer;
Say so, dequeue next customer to be serviced and determine customer’s
service completion time (random integer from 1 to 4 added to the
current time).


Now the issue I am having is that when I run my program the starting customers when I scroll up are starting around 620 - 630 instead of 1 to 4 like I would expect. Below is my code, could any one point me in the right direction?

thank you!



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
#include <iostream>
#include <string>
#include<list>
#include<ctime>
#include<cstdlib>
#include<iomanip>

using namespace std;

const int FULL_DAY = 720;

unsigned int determineServiceTime();
unsigned int determineArrivalTime();


int main()
{
	
	srand(static_cast< int>(time(0)));
	
	unsigned int minute_counter = 1, arrival_time = 0, service_time = 0;
	
	list <int> arrivalTime;
	list <int> serviceTime;
	
	
	
	
	while(minute_counter < FULL_DAY)
	{
		arrival_time += determineArrivalTime();
		arrivalTime.push_back(arrival_time);
		cout << "Customer will arrive at mintute " << arrivalTime.front() << endl;
		
		service_time += determineServiceTime();
		serviceTime.push_back(service_time);
		cout << "Customer will be served at mintute " << serviceTime.front() << endl;
		

		
		
		if(minute_counter == arrivalTime.front())
		{
			
			cout << "Costumer has arrived at minute: " << arrivalTime.front() << endl;
			cout << "Customer has been enqueued." << endl;
			
			arrivalTime.pop_front();
			
			
			}
			
		if(minute_counter == serviceTime.front())
		{
			cout << "Customer has been served at minute " << serviceTime.front() << endl;
			cout << "Customer has been dequeued." << endl;
			serviceTime.pop_front();
			
			}	
			
				minute_counter++;
			
		}
		
				
	}



unsigned int determineServiceTime()
{
	unsigned int chance = 1 + rand() % 4;
	
	return chance;
}

unsigned int determineArrivalTime()
{
	unsigned int chance = 1  + rand() % 4;
	
	return chance;
}


The particular problem is that on lines 32 and 36 you're pushing on the back, and on lines 33 and 37 you're printing the values on the front.

If you're going to use queues you should probably use two loops; one that populates with data and one that consumes the data. I have a hard time reasoning about what your code will do once it actually runs.
Also, consider these contents of the queues:
arrivalTime = (front)[4, 8, 12, 16, ...]
serviceTime = (front)[1, 2, 3, 4, ...]
Nothing prevents your code from generating a service time for a given customer that is less than that customer's arrival time. Therefore, in this example your code would start by servicing three non-existent customers.
Now the issue I am having is that when I run my program the starting customers when I scroll up are starting around 620 - 630 instead of 1 to 4 like I would expect.

That's because your console only keeps so many lines so you can only 'scroll up' so far.

I have been working on a program that has the following features and have come pretty close to what I believe is a final product.

Close, but no cigar. There is a reason step 1 is separate from step 2. Do you wait 'til the first customer's arrival time to determine the servicing time then begin serving him? What happens if two customers are scheduled to arrive and the customer you encounter first in the list is scheduled to come later than the person next in the list?
Topic archived. No new replies allowed.