Question

Brief explanation: I wrote a program that implements an event-driven simulation of a bank having one teller. The program reads data from an input file of arrival and transaction times. Each line of the file contains the arrival time and required transaction time for a customer.

My issue: If the arrival events from the input file is not sorted in ascending order, I want the program to print which customer is out of order. To do this, I created a vector and stored all of the arrival events into the vector and did this:

vector<int>::iterator i;
i = is_sorted_until(myVector.begin(), myVector.end());
if (i != myVector.end())
    cout << "The customer out of order is: " << (i - myVector.begin()) << endl;

The problem is that the input file looks like this:

3 5
7 4
10 5
8 2
12 9

If you notice, the 4th customer in the first column is out of order.

And when the program runs, the output is:

Processing an arrival event at time: 3
The size of the vector is: 1
Processing an arrival event at time: 7
The size of the vector is: 2
Processing a departure event at time: 8
Processing an arrival event at time: 10
The size of the vector is: 3
Processing an arrival event at time: 8
The size of the vector is: 4
The customer out of order is: 3
Processing an arrival event at time: 12
The size of the vector is: 5
The customer out of order is: 3
Processing a departure event at time: 12
Processing a departure event at time: 17
Processing a departure event at time: 19
Processing a departure event at time: 28

If you notice, it says the customer out of order is the 3rd customer and that's because at the time that arrival event is processed, there are only 4 elements in the vector (3, 7, 10, 8).

Is there a way to add in ALL the arrival events into the vector before checking if any of them are out of order so that the fourth customer is out of order?

Was it helpful?

Solution

The fourth customer is going to be in myVector[3]. A vector of size n goes from 0 to n-1.

Note that *i == myVector[i-myVector.begin()] and *myVector.begin() == myVector[0], so you are effectively calculating the index of the customer, with that equation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top