 Welcome back, in the previous segment we discussed a very simple real life problem and we wrote a program for it, this was the bargaining problem, now we are going to consider a slightly more complicated problem which is also resembling a real life problem. So this is about simulating a water tank, so we have a water tank which has capacity some C, C liters and initially it is empty and you can perform one of three operations with respect to this tank. So you can add water to it and let us say this operation will be performed and as far as the computer is concerned to add water into the tank you will type plus followed by the amount you want to add in liters. Now if you add water to a tank it may overflow, so you may be adding too much water, so in which case water will be wasted, so you are expected to report, your program is expected to report how much water is wasted or you may attempt to take Q liters out of the tank. So somebody will, somebody say walks to the tank and says give me 15 liters. Now if the tank has 15 liters then you will give 15 liters but if the tank does not have 15 liters you will give whatever is there but you will say how much you actually have given and these commands will keep on coming at you and you will have to respond to them properly and note that means that you need to know at each step how much water is there in the tank, so this is an example of a system in which there is some state, the state is how much water is present and what the external world is doing to the tank, well it is sending some commands as a result of which the system reacts and it produces an output and in addition its state also changes, so it may give you some water, it may take some water from you and at the same time its state will change and it will print out some messages, so these are the things that we want our program to do. So this kind of thing happens quite frequently, you are trying to keep track of a real life system and you are trying to see how it responds as external communication happens to it. So instead of a water tank for example this might be a bank account and you may be withdrawing money from it, you may depositing money from it, it is all really very similar and of course there is an additional command possible X, so if you type X then that is a signal to the program that look, stop, close everything, so just exit the program. So now what we are going to do is we are going to write the code for this and this time I am going to write it along with you, so let us see this is going to be a bit of a long code but I will write it with you. So let me try it out, so let me open a new file say new tank dot cpp, so let me put down hash include simple cpp, so 1m then let me start the main program, so how do we go about this, what variables do we need to have, well we need to clearly keep track of what how much water is there in our tank, so we need to know what is the current content, so we will need a variable for that and we also would like to know what is the capacity, so we will have variables for both of these, so let us just for fun make the capacity be 30 liters and then let us have a variable say called current and let us say initially it is empty, so this variable current tells you how much water is there in the tank at the current time, so this is the, these are the sort of main variables we need, well this is not a variable, capacity is not a variable but instead of putting in 30 everywhere in our program it is better to give it a name, so you could have even made this a constant but never mind it is not so important right now, so what happens next, so now we are going to simulate the execution of the system, so this has to be done as a vile loop which just keeps running, so vile and since it keeps running we are not yet ready to decide whether to terminate it, we will give the condition as being true, so what is the first thing we do in each iteration, well in each iteration we have to ask the user to type something, so we have to expect a command from the user, so that command is going to be a single later command followed by possibly a number, so we want to have some variable in which to receive that command and so it is customary to have say char command and then we will do c in into command, so at this point we have got the first letter that the user has typed into our variable called command, so if that letter is x then we have to exit, so let us just put that down, so if command equal to equal to x and let us say it is just lowercase x, then we will break, so break and that will be the end of the program as well, there will be nothing following this loop, otherwise we should check whether the user wants to add water or the user wants to take out water, so if command is equal to say plus then what should you do, this we want to put over here, so if the user typed a plus then we know that the user is going to add water and so we should expect the user to type in a number, so for that we will have int some quantity and we will say c in into quantity, now here I am assuming that quantity is going to be an integer, sorry let us make it a double, we said this is supposed to be double, so our capacity is double, so items will make this double, but we want this quantity to be a positive number, so if somebody types add, I want to add negative minus 30, so there is probably something wrong, so we should probably be checking, but let us say that such mistakes are not made and after a plus the user will be sensible enough to type in a positive number, so then this is fine, so what should we do at this point, so the user wants to add qty into current, so there is a possibility of an overflow and we are expected to report how much overflow is there, so here is what I am going to do, I am just going to first add it to current, so I am going to say equals current plus qty, now I will check if current equals, sorry if current is greater than capacity then what should I do, well then now I have to print out how much water is wasted, so how much is wasted, so the amount of wastage is, so how much is wasted, well everything above the capacity is wasted, so this quantity, the wasted quantity is current minus capacity, is that all, well no our current is above capacity and we just removed something out of it, so we should leave it with the actual current value, so when we pour it went above capacity but we know that it cannot go above capacity, so whatever goes above capacity is wasted, so we should leave current equals capacity, if current is less than or equal to capacity we have to do nothing and therefore that is enough, so this is the processing we need to do if our command was a plus, else if command is minus what do we need to do, well again we need to do something similar, we are expecting the user to type in a number, a quantity, so let us do that, so double qty and cn into qty, so the user has typed in something and maybe the user has demanded something more than what we have available, so I guess at this point we should check, so we should say if qty is bigger than current then what happens, so if the quantity demanded is bigger than the current quantity then how much are we going to give, so we are going to give qty and we should change the current value, so how much is how much value remains, well we are giving, sorry we are not giving qty, we are giving current because qty was bigger, so how much remains, so then current is equal to 0, so this is what happens if qty is bigger than current, what if it is less than or equal to current, in which case how much are we going to give, I should have not typed semicolon over here but a colon, how much are we giving, well in this case we are giving whatever is demanded and what is the new value, so this is equal to current minus what we gave, so that is it, so that is the end of this minus command processing. Now here maybe the user types in some other character, so maybe we really should put in else cout invalid command and that is the end of the loop, so this is going to get repeated and this if you exit out of it that is going to end the program, so to make this a little bit more understandable, let us say that before the user types anything we will print out how much water is there currently, so we can print say cout currently we have so much water, so let us see how this works, so let me try compiling it, it seems to have compiled no errors maybe I did not make any errors while typing it out, so it is telling me currently we have 0, so let us say let me add something, so let me add maybe 50, so it says you wasted 20, currently we have 30, is that right, well it is because the capacity of that tank is 30, so maybe let us take away something, so let us say minus 15, so it is giving 15 and what remains is 15, so let us try minus 20, so you asked for 20 but it is only giving you 15, is that reasonable, well yes it only has 15 and after giving you 15 it only has 0 which is good, which is as you might expect, so let us try to take a little bit more, so maybe minus 10, so it is not giving you anything because it does not have anything and it continues to have nothing, so maybe we will add say plus 35, let us say let us add plus 25, so it says currently we have 25, but notice that it did not print out a message saying something was wasted because nothing was wasted, so this seems like a reasonable, a correct program, so we can get out of it, let us see will we get out of it, yes, so that is it, so that is this program, so let me get back to the presentation now, so we did this, so what did we discuss in this segment, so we discussed a simple example of a real life system, this system does have state, how much water is in the tank and the system has well defined operations, add water or remove water and the operations are slightly complex because of the possibility of overflow and underflow, so I want to point out that this may seem like a trivial system and in a sense it is, but it is an example of a system which has a state and which has operations allowed, so you will see systems which are just more complicated versions of this and there is a basic loop when dealing with such a system that you wait for the environment to say something, maybe make a request, then the system responds to it and prints out a message and it changes its state, so that is what we wanted to say on this topic, in the next segment we will talk about a different topic that of arithmetic on very large numbers, so we will take a break.