 Hello and welcome to the course on an introduction to programming through C++. This is the second lecture sequence of the second week. The topic for today is a program design example and the reading for this is from chapter 4 of the book. So the main problem that we are going to consider in this lecture is how to write programs. And we have been writing some programs, we have written some simple programs and we sort of wrote them automatically or intuitively if you will. Now if you want to write more complex programs, even slightly more complex programs, we need to approach this with care because we might make mistakes and we might find it difficult to get out of mistakes and we really should be worried about mistakes and also about how much time we spend on the writing process. So we should make our writing process efficient and we should also make sure that our programs do not make any mistakes. Now that is because the program could be doing some very important calculations, maybe controlling the flight of a plane or maybe deciding how much radiation to give to a patient. So you really cannot afford to make any mistakes in this. And so we should do sort of due diligence and it is important to learn what are the things that you should do in order to ensure that your programs work correctly. So the goal of the chapter is to develop a slightly more complex program than what we have seen so far and while doing this we will follow a typical program development strategy. So what is this? So this begins with the description of the specification. The specification simply and very concisely usually states what the input data is and what the output required is. Now if you are writing a program to solve a real life problem then the statement that might be given to you might be ambiguous or might be incomplete and so when you write down the specification you should try to make it as precise and as complete as possible. Then we construct the test cases. So for what input, what output do you expect? Then we think about how to solve the problem using pencil and paper. This is kind of the first important seriously creative step. Now here what you are expected is to really just mimic manual computation. So the creativity is a little bit less important right now but you really need to pay attention to how you solve the problem manually. So you need to figure out what the structure of that manual solution process is. Which means for example to decide whether something is repeated if so how many times. After this you get to the program writing stage and here the manual solution process will help you because the structure will get reflected in the program. And you also need to decide what variables to use. You can again reason it out from the way you perform the manual calculations. Once your program is ready you run the test cases and if the program does not work correctly as might happen from time to time then you need to figure out what went wrong and you need to fix it. The problem that we are going to look at is a very simple problem. So we have a series whose value approaches E as N increases or we have a sum. So this sum is 1 upon 0 factorial plus 1 upon 1 factorial plus 1 upon 2 factorial all the way till 1 over n factorial. If you add the terms up and as you take n larger and larger you can see you can prove in fact that this sum is going to get close to E is going to tend to E. So we are supposed to take a program we are supposed to write a program which takes n as input and prints the sum of this series whatever the value of n might be. So first let us come to the specification well the input there is only 1 the integer n. So we have clarified that n has to be a non-negative integer the problem does not make sense if we have n negative and the output is the sum 1 upon 0 factorial plus 1 upon 1 factorial all the way till 1 upon n factorial. So notice that 0 as input makes sense because then that just means that you get the first term which is 1 that is of course nowhere close to E but we did not really promise that it would get close to E for such a small value of n in any case. Now in this particular example for this particular problem the specification is fairly straight forward however notice that even here we have added a bit of value so when we said that input is n we also wrote down that it had better be larger than or equal to 0. In general when you write the specification or maybe say after you write the specification you should ask yourself well I have written this down but are there any tricky points to it can something be misunderstood even by mistake because you do not really want any misunderstanding because those misunderstandings may creep into your program as well. So in this case you may realize that if you are careless you may think of n as also being the number of terms to be added up but that is not correct 1 upon 0 factorial might be regarded as the zeroth term then 1 upon 1 factorial is the first term 1 over n factorial is the nth term so there really are n plus 1 terms to be added up. However the number of additions you perform is actually n so n does have some significance but not as the number of terms. The next step is to construct the test cases so here you just pick some input values and you calculate the required output values so for example if we say n is 0 then the required output is 1 over 0 factorial or 1 if we say n is 1 the required output is 1 over 0 factorial plus 1 over 1 factorial which is equal to 2 if n is 2 then the required output is 1 over 0 factorial plus 1 over 1 factorial plus 1 over 2 factorial which is 2.5 and this program really is meant for calculating E and that happens at some large enough value so maybe we will say that if you pick say n equal to 10 or something like that you would like the answer to be close to the known value of E which to so many decimals is 2.718281828. Now test cases will be really needed after you finish writing the program however if you construct them early it is just a confirmation to yourself that look I understand what is expected of me I have not missed out any point okay so what have we discussed so far so we said that the first steps of the program development process consist of the specification writing the specification and constructing the test cases. And we are going to turn to how to solve the problem manually we will take a break.