 Welcome back, in the last segment we discussed how arrays can be used to represent polynomials. In this segment we are going to discuss another interesting use of arrays called in a problem called the taxi dispatch problem. So in this problem you are at some taxi terminal where taxi drivers arrive and wait for passengers and the idea is that the driver that arrives first must be given a passenger before anyone that comes in later. So we need some kind of a queue over here. So we need to somehow record who came first, who came second and so on. So we are going to get the IDs. So these will be numbers of the drivers that arrive and we are going to put them in some kind of a queue. We will see exactly what, how to do this but you know what a queue is in real life. So we need to do something like that. So we said that the driver ID is going to be an integer for simplicity but later on we can have some more complicated driver ID as well. Well maybe a customer arrives. So if a customer arrives and if a taxi is waiting then the first driver in the queue should be assigned to that customer. If no taxi is waiting then the customer is asked to call again sometime later. So imagine that you are the controller of this taxi dispatch center and you need a program to keep track of everything that is going on. So what would that program look like? So we are asked to write this program. So that is the problem which has been given to us. So what are the requirements? Well you need to remember the IDs of the drivers who are waiting to pick up customers that is one requirement and you also need to remember the order in which they arrive. And when the customer arrives you assign the earliest driver and when you assign the earliest driver you should remove the driver ID of the assigned driver from your memory. That is basically it. When driver arrives you add the driver's ID to memory but you kind of put it last. So how do you remember the driver IDs? So if you have to remember a large number of numbers of the same type of course you have to use an array. So there is no escaping from that. So we will use an array. So let us say we are going to remember some 500 driver IDs. So we are going to use a name instead of the number 500 because the name tells us what it is that this number is representing. I could have made this name more, more informative. I could have called let us say end drivers but let me just keep it simple. Let me just keep it compact because otherwise my slides become too large. So I am going to define an array with 500 elements. So this array is going to be called driver ID. So if more than 500 drivers happen to be waiting then we will have a problem. So we might have to tell the driver that look please go back and come again later because we do not have space to record your ID. Ideally this should not happen but let us say just we have to take care of every possibility when we write a program. Now the important question is in what order should we store the IDs in our array? So besides the ID is there any other information that we need to remember? And we need to say what should we do when the customer arrives? What should we do when a driver arrives? So these are the questions we need to answer. So here is the first idea for answering these questions. So the earliest driver who has not yet been served let us say we store in driver ID 0. The next earliest in driver ID 1 and so on and we also remember how many drivers are waiting. So for that let us say we use a variable called n-waiting. So that is basically it. We have pretty much decided what variables we need at this point. So these are sort of the important variables. So we may need to have some local small variables which will be needed for a short while but really the important information as far as this program is concerned is going to be stored in this array driver ID and this variable n-waiting. So here is the outline of our program. So we are going to define our arrays and there is the constant and then there is the variable n-waiting. And we should at the beginning make n-waiting 0 because no driver has come in yet. And our basic loop is going to be we are going to wait for drivers or customers to come. So for this purpose the controller will type in a command. So whatever the controller types in will be read into this variable called command. So this variable is going to be of type char. So our convention is going to be that if a driver has come in then the controller who is going to use our program will type in a d, d for driver. So if the controller type in d then we need to somehow process the driver arrival. So how to do that we will decide in a minute. But if the command was a c then we should process a customer arrival, c for customer. So our instructions to the controller are that if you see a driver come in and you want to tell the program that a driver has come in then you should type a d and then give the information needed corresponding to driver. If a customer comes in you should type a c and after that give the information corresponding to a customer. Otherwise if a command x is given then we should stop. X starts stands for exit usually and in this case. And if you give any other command then the program will say this is an illegal command and it will not abort, it will not stop but it will just go back and wait for another command. That is going to be our overall high level program. So what I am going to do over here is fill in all the details. So how am I going to fill in all the details and this is a bit of a complicated program so let us really make a proper plan. Let us decide what exactly our variables are going to mean and then let us stick to those decisions. So this is like what we had said long ago. So before you write complicated things make a proper plan and write down invariants for our variables. So we have already talked about our variable n waiting, this is the number of waiting drivers. So we want this variable to take a value in the range 0 through n, n is the largest number of drivers that we can have waiting. So we should be careful that we never increase n waiting beyond n. Then we should know where the idea of the earliest waiting driver is going to be. So let us make our plan and let us make a decision. Let us say that the idea of the idea of the earliest waiting driver is always going to be in driver ID 0. The next driver is going to be in driver ID 1 and so on. The last driver is going to be in driver ID n waiting minus 1. Why this position? Well there are n waiting drivers. So starting here all the way till this you will have exactly n waiting slots. So if I want to insert a new driver who has just arrived where should I put that driver? That driver should be put in driver ID n waiting. So what happens on driver arrival? If n waiting is n then I am going to say that the queue is full otherwise we are going to get the driver ID and for this let us say we allocate a variable d. And then as we just said that this information goes into driver ID n waiting. So this d is put over there. So this is the ID of the last driver, the driver that just arrived. But because the driver has just arrived what has happened? Well the number of waiting drivers has increased and therefore we should increase n waiting. That is it. That is what we need to do if a driver has arrived. Of course we have made some invariants. But for the invariants we should make sure that n waiting never goes beyond n. So if you go through this program you will see that we incremented only if n waiting is smaller than n and therefore it will never go. Similarly the other invariants you should also check they will also hold. What happens when a customer arrives? Well what we need to do is something like this. We can process the customer only if n waiting is bigger than 0. There had better be some taxi drivers waiting if we want to do something with this customer. Otherwise we have to just tell the customer to try again. But if n waiting is bigger than 0 then we have to assign the earliest unassigned driver to the customer. The earliest unassigned driver is stored, the ID of that driver is stored in driver ID 0. So we can pick that up and give it. But now the second earliest should become the new earliest because the earliest has gone away. So all the drivers that are waiting, the one that was the second earliest before this driver went away should now become the earliest and so on. So we need to take care of that and also n waiting should decrease. So these are the things that should happen according to the plan we have made. So what should we do? So if n waiting is 0 then we tell the customer please try again. Else we are going to say I am going to assign a driver for you for this customer and we will just print the driver ID, the ID of the driver waiting in the 0th position. Will this ID be actually the ID of a driver? Yes, because n waiting cannot be 0, n waiting equal to 0 case went away over here. So here we know that n waiting has to be bigger than 0. So this is a valid driver ID. And then we need to satisfy our other invariants which are that the waiting driver should be in position 0 through n waiting minus 1. So the position 0 driver has gone away so we should shift down all the waiting drivers 1 in this queue so to say that we are maintaining. So this is what this loop accomplishes. So the driver in position i at element i at index i that ID is moved to position i minus 1. And after that we decrement n waiting, we write n waiting equal to n waiting minus 1 or n waiting minus minus. So that is it. So what have we discussed? We have discussed a workable scheme for matching taxis to customers. So taxi drivers are recorded in an array in the order of their arrival and arrival order is not explicitly stored. So we are not saying that this is the 0th driver that arrived the first, this is the driver that arrived the second. We are not storing these numbers 1 and 2. Just because those numbers are in a certain order in our array we know when they have or in the order in which they have arrived. In the next segment we are going to see some improvements to the scheme but we will take a quick break.