 As I mentioned in the morning, I had got exactly five requests for discussion. One related to the need for additional problems for practice solving. As I mentioned from week after the mid sem, I will be posting about 8 to 10 problems every week on that theme. So, you can sort of try to work those out. So, we are going to do some discussion on basic control structures which are while and for loops. I am actually surprised that people have difficulty in appreciating that. I will take the context of both simple iteration for computations as well as iterations which will move over arrays, simple one dimensional array. Subsequently, I want to consider the problem of reversing a number. As I said, we will discuss the for and while loop construction, the synthesis, how do we do that? In the process, we will go through the notion of array, array index handling and the specific problem we will discuss is what was raised as an issue by one of the students. Namely, if given a number, how do I extract digits of that number and how do I invert those digits to form a number in the reversing? It is actually a simple problem. Most of you would have it, but I will use this opportunity to illustrate how you can use for loop, how you can use while loop. We will use combination of both. We will use actually an array and so on. In the process, I will also comment on how to use the debug facility in case we make an error. We will try and make some deliberate errors while writing the program. So, I am not showing a prefixed program here. We will evolve it. To begin with, I want to quickly go back and check absolutely fundamental basics. The notion of data structures is the notion of representation of data. So, we represent data through either constants. By the way, we are all talking about C plus plus programming language now, not about operating system, not about other programming language, specific language. So, what kind of constants do we write? 25. Agreed? If it is a constant, minus 71 constant, 1, 4, 2, 7, 9, 2, 4, 6, 1, 4, 9. Is it a integer constant? Yes. Who said yes? You remember, we said that integer constants have a maximum length. Nobody said long. I said integer constant, people said yes. There are small things which you have to absolutely ingrain in your mind. This is not a valid integer constant. Is it a valid long? Still a valid long? Yes. If you say yes, I will keep writing on either side. The point is, do you remember how many digits a long can have? You have wondered why. Good. This is about integer constants. What about floating point constants? 1, 2, 3, 4, e minus 17. Valid floating point constant? Yes. Valid floating point constant? No. Why not? Sorry, 9. I am talking about floating point. This is not an integer number. This is a floating point number. In a floating point number, a mantissa cannot be larger than so many digits of precision, but exponent can be large. The exponent is definitely within the range. Your problem when you said no, you are actually converting it to integer number and you are finding it is much larger than the full integer number, but the internal representation is not integer. In this particular case, this floating point number will have exact representation because luckily it does not have other digits of precision, but try to write 8, 9 digits of precision. The magnitude will still remain 10 to the power 12, but the precision will be lost. This is the fundamental difference between integer and floating point representation. So, this is float. This is int. We had seen other things. For example, we had strain. This is a string constant. This is not the way the string constants are internally represented. We have not seen how strings are internally represented yet. We have said we do not bother. We take a string variable and carry on with it. We have the notion of an array. A single dimensional array is actually a list. So, when I say a 10, I actually have. So, what I am showing here, these are independent locations. Each location contains a value. Instead of calling these locations by variable names x, y, z, alpha, beta, I want to call them by a single name, but giving an index. So, please understand that a 0, a 1, a 2, a 3, etcetera are no different from x, y, alpha, etcetera. No different. They are exactly the same. The difference is that if x and y come into the corresponding locations of Mr. Dumbo, by using the same name, I cannot refer to the other. I have to use one name to refer to one, another name to refer to other. The only advantage of an array is that all elements share the same name. Therefore, we have the notion of index which we write inside square brackets. Array indices in C plus plus always start with 0 and end up with one value less than the active size of the array. So, if I have declared an array a 10, it will have 10 elements. First will be a 0, last will be 8. The importance of using this data structure is that I can refer to an element by a i or I can refer to an element by a j multiplied by 3 minus k. And I can use this wherever I could use x, y or something. This is like a variable, but which variable? So, it is a variable variable. Depending upon the value of the index, this will point to this element or this will point to that element. And that is the beauty that I can now scan a series of elements one after another by using or manipulating the index value. So, consequently the expression that you write here will be known as index expression. And it is like any other arithmetic expression that you write in evaluating some value which you assign to some A, B, C or whatever, no different. Naturally, you have to take care that the result of the index expression is within the range of that array. That is your responsibility. But as long as you do that, now you can see that if I have this kind of reference possible, then by varying the value of i or by varying the value of j, iteratively I can address successive elements or arbitrary elements in any order that I desire of an array. This is a facility which does not exist with explicitly named variables. If I say x, y, a, b, I cannot have an automatic iteration around. And that is why array as a list is an extremely important concept. And it is the first important data structure per se. Variables are not really data structure. Constants are constants. Variables are individual entity. A structure of data is when you put these entities together in some kind of form. And the important part of the form for array is that every element of an array structure exactly has the same data type. You cannot have a 0 integer, a 1 float, a 2 string. That is not possible. The notion of an array is that it represents a collection of exactly similar objects. Either all integers, all float, whatever, whatever. This is clear? Now let us go over to the iteration. The fundamental concept of an iteration is that when I am executing some actions, I do some assignment, something, something. I suddenly want to check a condition. I will just represent that condition by a question mark. If this is true, I will come down here and maybe execute some instructions here. And after doing that, I want to go back and repeat this. If this condition is false, I want to come out. And go to the next statement. And this will follow, just as this follows here. Just for simplicity, I will write this as p, which means this is a previous statement and n, which is the next statement. In fact, to be still precise, I will write this as priv indicating a previous statement and next as the next statement. The question mark represents a condition. And these are the statements in the block. So this is called the iteration block. Observe that suppose this condition, every time I come here, remains true for some time, 3 times, 4 times, 5 times. Then this block will be executed. Again you will go back. This block will be executed. Again you will go back. This block will be executed. If this condition remains true perpetually, you will never get out of it. That is called an infinite iteration or infinite loop. Obviously we have to avoid that because once you start, the program will continue to execute doing the same thing again and again till some error occurs because some statement cannot be executed. So it is your responsibility to ensure that within the block, you execute at least one statement, which is likely to change this condition and in a desirable fashion, not in an arbitrary fashion, in a desirable. So I will give you an example. Suppose I want to add given numbers. I will give a number, input number. I add it. Second number, add it to the first. Third number, add it to the previous number. Fourth number, add it to the previous number. How long you want to add numbers? Your answer is I do not know at the beginning. I want you to give me a program, which I can use to add as many numbers as I want. Today I want to run the program. I want to add three numbers. Tomorrow I want to add 25 numbers. Suppose I say tell me how many numbers you want to add? I do not know. Where will you face this situation? You go to a shop, a mall or whatever, and you want to buy this. You want to buy this. You want to buy this. You want to buy this. Now that fellow is keeping the count, adding 5 rupees, 25 rupees, 27 rupees, 37, 50 paisa something. He is about to say, are you done? He says no, no, no. He has paid me little. So you do not know how many. Now in such a situation, I need to set up an iteration to keep calculating the growing sum till I get some indication, no more. To a shopkeeper, what will you say when you ask any more? He said no more. Now in numerical calculations, we have to devise an artificial way of saying no more. So suppose I am preparing a sum for calculating the total money that you have to pay for the purchases that you make. How will I write a program here? Assume that the value that I gave is represented by V. I will call it float V. Assume that the sum that I want to make of various Vs, first V, second V, third V, etc. is called sum. The algorithm is very simple to me. I will begin by saying sum is equal to 0.0. So initially I have not purchased anything. The clerk at the store will start with 0. Let us say as a calculator. Exactly how you do that? Calculators start with clear, 0. Now you buy 1 kilo of sugar. I do not know how much sugar costs. 30 rupees? Let us make it 32 rupees, 50 paisa. So you will give 32, 50. He will add it to sum. Next you buy something worth 10 rupees. You give 10. He will add it to sum. You want to keep collecting numbers from him. Till some point when you say no, now we have to decide how will we indicate no in such a situation. One very simple thing. Since we are talking about calculating the sum of prices of things which I purchase, it is unlikely that I will purchase something which costs minus 7 rupees. Agreed? I mean nobody sells you something and gives you 7 rupees, right? It is not possible. So any negative number is a false number. We can therefore decide on a convention. This is an artificial convention that I want to keep adding numbers till someone gives me a negative number. Now I know what condition I should check. I should read a number. If the number is negative, I will stop. If the number is not negative, I should continue. And of course this sum, I mean the price will be either equal or whatever, greater than or equal to 0. Suppose I artificially say V is equal to 0. It is perfectly fine because I have not purchased anything. It is an artificial thing. And now I say why V not equal to 0? How do I write not equal to? Yes, is that the correct wording? What did we agree? If the value is positive, I should continue. If the value is negative, I should stop. So what is the correct condition? While V is greater than equal to 0, I want to do something. What is it that I want to do? I want to read a number and add it. How will I do that? Look at what I am doing. V is 0, so it is greater than equal to 0. I will come in. I will read a number. I will add it to sum. Then I will again go back. If this number is positive, I will continue doing that again and again and again. Somewhere I will read a negative number. Next time I go back, V will not be greater than equal to 0. I will come out. However, I have a major goof up here. I am reading the number here. So suppose this number is minus 7. So actually I will benefit. The store will lose because it will subtract 7. Then I will find out that it is minus 7 and then I will come out. I will give me total. This is wrong. So what should I do? Outside what? Read the first number outside and read one number again later for the cross check. The same number you won't keep checking. You already checked it once. So what should I write now? Let me fold this here because we want to keep the high loop in our mind. So I will now say, is this clear? Will this work? Yes, no? Sorry? Yes. No, it will not keep going. If you enter 0, it will go. If you again enter 0, it will go. Why should any sensible human being keep entering 0, 0, 0 again? Remember you are standing at the store. You are saying, I will purchase this. No. That's the only time you will get 0. So when you finish, we have agreed. I am the storekeeper. You are the purchaser. We have agreed that for my computer, whenever you finish your purchases, you will give me some negative number. If you don't give, you are misbehaving. If you misbehave, don't expect my Dumbo to behave properly. But he is making an important point. Between the storekeeper and a customer, a decent behavior is expected. But between a computer and a user, user may be very indecent. 0, 0, 0. So we have to terminate. That's the reason why when you actually sum up values, you rarely will give input on the keyboard. That is the reason why we said we will have files in which we will prepare the input. And the files physically end sometime. Somebody will get tired of writing 0s. File will end. But you are very right. If you are not, this goes to only show one thing. Yes, we should take care of such a situation where somebody stupidly give wrong values. But also, we are not designing our programs for such funny behavior. We are designing our programs to take care of such funny behavior if it happens by mistake sometime. However, I will again make the point here. Here we are not looking at summing up prices in a store. We are looking at how the file loop functions. You agree that the file loop functions correctly? What are the variations possible? The variations possible is that I have a do-while structure. In a do-while structure, I will have a separate structure which actually starts with the previous statement. But the block is here and then you go back. There is a mistake here. Please disregard this. This is the condition. And if it is true, this structure is called do-while. So, this is the previous statement. This is the next statement. And I will write it like this. Previous, semicolon, do. This statement does not check the condition at the beginning. It does whatever work you do. At the end, it checks the condition. If the condition is true, the iteration is repeated. Otherwise, you come out. There are situations where this is more easy and natural to write. There are situations where the file form or the earlier form is easier to write. This is a matter of your choice. What you must ensure, however, is that you design these statements carefully such that after every iteration, this condition is likely to change. If this condition does not change, you will never come out. Same thing applies to the earlier one. And now you can make use of the fact that you can write some statement which will change the condition to actually set up an iteration to go over successive elements of an array. How you can do that? Consider this. I have an array here. And this is an array called a. So, this shall be a0. This shall be a1. This shall be a2. And so on. This is an. The actual array may be much larger. So, let us say I declare here I want to do a simple thing. Same store example, you go to the store and you keep collecting a value. But this time, you do not want to forget the value. You do not want only the final sum. You want to retain each individual value in the array because you want to ask the store keeper, please print a bill. Print all the values that you have got. So, that means he has to store all the values somewhere. We use an array for that. We declare an array of 100 elements. But we shall purchase only n elements. We do not know the value of n. Again the same problem. So, I am using actually the same problem to illustrate the point that I can use a while loop. I know the first purchase I make, the value should go here. Second purchase I make, the value should go here. And so on. What is here means a0. Here means a1. That means in the same while loop, I should change my index. First value it should be 0. Second value it should be 1. Third value it should be 2, etc. Till I give a negative value when it should stop. So, if I were to set up exactly the same logic which I had used here. Exactly the same logic. I will still define float v and sum. So, I have these two variables here. This is v and this is sum. I will put this logic here. What I was doing? I was setting sum to 0. I was reading a value. I will do the same thing here. I will set sum equal to 0. And I will read a value. I have not put it in the array. Now I get a chance to say while v is greater than equal to 0.0. Yes, it is a positive value. 37 rupees, 45 rupees. Put it in the array. The array is a. So, I will say ai equal to v. But there is a problem. What is the value of i? There is no value. I have not put any value. This is wrong. I cannot use an array expression unless it can be evaluated. Now if I look at this array, the first value I wanted to put here, next value I wanted to put next time and so on. Obviously, I should start with 0. And that 0 starting should happen before the loop. So, I will say i is equal to 0. And I will declare int i. This is something I would like to insist that you do as a standard discipline. Whenever you are writing a program, suddenly you may think of some x, y or something. And you may start writing a program. Every time you write a variable, cross check whether you have declared it or not. Just like I thought of writing i. And I remembered I have not declared int i. I must do that. In general, it is grossly bad practice to use any variable and declaring it explicitly yourself, so that the compiler knows what it is. So, I start with i equal to 0. First number which I read, if it is greater than equal to 0, should not be equal to 0 really greater than 0. Then I will say ai equal to 0. Can I add it to some? Of course I can. You understand this syntax? Some plus equal to v is the same as some result. Now, I want to go back. Sorry, I should put a brass here, because I want to execute all these statements. That is the grouping. Is it all right if I did this? What will happen? It will read the next value. Where is the next value? No. There is no next value. So, this loop will check whether v is greater than 0. Yes. So, first time I purchase 10 rupees 35 if I say, it will put 10 rupees 35 if I say into a 0. Add this to some. Then it will put again the same 10 rupees 35 if I say into a 0 only, but add to my sum. At the end of this business, if at all it ever ends, when the computer gives up or whatever, or the night that fellow is going to stop, I would have exactly one tiny thing for 10 rupees 75, I say, and a bill for 20,000 rupees. Not acceptable? So, what I must do? I must first read a new value of v. This alone is not sufficient. Because what this will do is, it will keep taking a new value and keep suffocating a 0 a 0 a 0. So, there are two things which we are achieving. One, we are ensuring that the condition is likely to change. Sometimes somebody will give me a negative value and I will get out of here. Second, we are simultaneously ensuring that the index of this array increases by 1. So, when I come out here, if I say see out some, I will get that. You are sure this algorithm is correct? You are sure it will work? 100 percent, no. There is some condition. So, our sarcastic friend should find that error out. You are very good at supplying 0s. So, can you find out why this will not work or when this will not work? Yes. Everybody forgot that we declared a value float a 100. There are 100 elements in the array. Psychologically, we are still in that store and none of us purchase 100 things at a time. So, we believe that decently we will give some negative value. But I do not know if, for example, a very rich lady goes to a lady, please excuse me, rich lady and she has, let us say, 4 attendants with bags. Now, what is 100 things? Purchase 500 things. So, my C++ Dumbo will give up. What it will do? After the 100th value, it will not creep. We are still positive. It will put it in the element a 100, a 100. No, a 100. No, a 99 is legal. But when it becomes 100, it will try to put it in a 100. Then you try to put it in a 101. Which are those a 100 and a 101? Let us look at this map. This is a 100, let us say. A 99, sorry. Let us assume that C++ Dumbo, when we have given these declarations, he has put V here and some here. So, the next value will go into V. The next value will go into my sum. I will be very happy because the sum will now be reduced to a small value. But the next value will go here. What is this? The way operating system works, it can run many programs simultaneously. Your program, let us say, ends here. Your program instructions were here. This was your data. This was the first instruction of his program. His program will now receive 10.75 as an instruction. Or his program may have a sum. Or he is finding out Nth Fibonacci number. He will get 10.75. Sometimes things will go so heavy that the machine will crash. This is the problem with any array manipulation. Consequently, this condition is not adequate. I must ensure that I also remains in less than or equal to 99 or less than 100. How can I ensure that? Well, I do not put this here. I say and I less than 100. And then I put this. Do you agree this will work now? The only problem is when you come out here, you will not know whether you have come out because somebody gave a negative number. That means somebody purchased only 10 or 12 things. Or you came out because I exceeded 99. That is one question. That is about writing more decent algorithm. 100 point is made here. There are situations, particularly when I am scanning arrays, that I will have to go through the array elements 1, 2, 3, 4, 5. And I observe that I am doing three things here. One, initialize I. Second, check whether I meet some terminal condition. And third, increment I. And of course, I am reading V to change this, etc., etc. You will agree that in general, while I am manipulating array elements, I will invariably need these three things. Initialize the index. Check whether the index is within certain range, in which case set up the iteration. And at the end of that iteration, change the value of index. This is most common requirement. And that is the reason why we are given the four structure. And this structure, yeah. It will go back again. It will go back because this says go back. This closing brass. It will go back because I am taking those. Going back has nothing to do with what you are doing here. Going back has to do when you encounter this. So, it is very important to see this. What this while loop does is it does this. The pre-statement is anything that you have written up to this point. This is my prev. The next statement is whatever I write here. This is my next. Inside whatever I write is equal to this flow chart. Now, let it be very explicit. What I have written in the bracket is the question mark. This opening brass is this. One second. This closing brass is this. Now, here is the point. Absolutely independent of what you do here and here. You will always go back. Absolutely independent. Here you say abrakadabrakad, nonsense, print, whatever. It does not matter. The very nature of the wild statement is if you enter this brass, then at the closing brass you will necessarily go out. It does not matter what you do in between. That is iteration. The iteration setting is not dependent on what you do inside. Yes. This is the one. Without an iteration? No. Without the iteration, which one you are referring to? Let us write this again. No problem. I understand what you are saying. Let us go back. Call it page 0. Just saying sum is equal to 0, c in v, sum is equal to sum plus v. Where is the question of going back? There is no question. It is not a loop. What this will do is it will take one value and add it to sum. That is it. What this does is it will add one value. If I want to add multiple values, I want to set up a loop. The only way I can set up a loop is to say while this or do this while this. There is no other way. So, automatically nothing goes back, not stop here. It will execute the next instruction. Whatever is the next instruction, going back will not happen. If you say return 0 or whatever, the whole program will terminate and you will go back to the operating system. If you do not write c plus plus, Dumbo will shout at you because this is not a valid program. Return 0 has to be the last statement of every program. Remember this. Normally when you write instructions, they are executed one after another. If you want to set up an iteration, you need such a control flow. For that, you need to write this or you need to write this or if you want to store things inside an array, you need to write this. What I am trying to say is if you write something like this where you have to manipulate array elements, indexes, you will generally start with some initial value. You will check for a terminal condition and you will change the value of index. Typically, when you are scanning the array in a linear fashion, you will change the index by 1. This is precisely what is supported when you say this. For i equal to 0, i less than 100, i plus plus, this implies an equivalent of a wide loop but it does not check any arbitrary condition. It checks only whether the index is less than 100. This is what I had shown even in the class. This is equivalent of saying i equal to 1, checking whether i is less than 100. If the answer is true or yes, going down here, doing whatever statement you write, then adding 1 to i and then going back. If this is false, it will come out and you will execute the next statement. Is i equal to 1 the previous statement as we saw in the earlier flow chart? No, it is not. Previous statement is what you execute even before this. So, the equivalent of p is some previous statement here. You execute that, then you come to this point. What you write here is equivalent of setting up an initial value of i once and only once. This is not part of the loop but it is part of the definition of the loop. Then you check this condition. If it is true, you come down and execute this statement 1, statement 2. Ordinarily in the while loop, you would have simply gone back. Here, the value of i is also incremented but this specification is given here, not at the end. So, you must understand that out of these three things, i equal to 0 happens even before the loop begins. i equal to i plus plus happens just before the loop ends and this is then equivalent to while i less than. Is this very clear? Initialization outside the iteration, increment just before the end of the iteration and in between whatever blocks you say. Now, you can see how easy it is to write our earlier thing wherein the previous thing I would have said sum is equal to 0.0. I would have now said whatever c in v. There is one problem here. I was terminating my loop when v was negative. Earlier, the problem with my program was if v became negative, I would come out but as long as v is positive, I will keep going and exceed the array limit. This particular form loop guarantees that I will not stuff anything into an array beyond the array limits. But if I give negative value, there is no way to come out. So, I will have to check that condition separately and exit this loop if I ever find a negative value. If I don't find a negative value, I add it and I keep going back. How can I do that here? Right. So, I will say first of all sum is 0. I will now say read in v. If v less than 0, that's it. I don't have to put brasses or anything. I just want to get the hell out of here. If v is negative, I don't want to do anything. If v is not negative, I will continue. Break, what does break do? If anywhere here if you say break, it will come out from the innermost loop in which the break occurs. Since this break occurs inside this iteration, it will come out of this. I will go to the next step. Otherwise, it will come to the next segment. Very simple. I can say a i equal to v and sum plus equal to v. Do you agree that this algorithm will work? Whether to use this algorithm or any other algorithm, entirely your choice. But generally, if you are manipulating arrays, it is prudent to use an index expression which is automatically incremented, automatically checked by a single specific. So, this is what for and yes, what she is pointing out is that this for loop while it is traditionally used for manipulating index, it is actually not limited to that. So, she is reminding us of the general description of the for loop which I gave in the class. The general description of the for loop says something here, something here, something here. This is something all of which is done at the initial time before the loop starts. This is something which is a condition which is checked. It does not say that it has to be a condition only on the index. It can be a combination of any conditions. So, what she is saying, instead of saying this, you check it here itself. There is some problem which I will mention, but it is possible. That means I can actually have a combination of array index movement as well as checking for some other condition. And this also need not be only I plus class. It can be something which is a set of assignments, set of increments, whatever, whatever. So, there is a very general form. You should read it both from the lecture notes and from Cohen's book. We will describe whatever it is. But for the time being, let us implement her suggestion and see what happens. So, instead of this, let me chop this thing off. So, what you are suggesting is I need not say this. And I will say here I less than 100 and what was the condition? v greater than 0, greater than equal to 0. So, let us look at this statement now. For I equal to 0, I less than 100 and v greater than 0, do this. Keep making I plus plus. Will this work? I have a problem. When I do this and come here, there is no value for v at the beginning. So, how will this condition be checked? So, I read a value of v outside. Now, I read this value here. I come in. Is v greater than 0? Yes. Then I read the next value. The first thing I purchase is free. The shopkeeper will throw you as a programmer and the computer dump out. So, okay. I think all of you have got the gist, right? The point being made here is you have to be very careful that when you write some condition, whether the condition is correctly initialized, whether the condition makes sense when you execute that. And that is why at least when you are not a practice programmer, every time you write this, just execute that algorithm for a couple of values and you will suddenly see this point. We are getting one thing free. It is wrong. Whatever. So, extreme condition should be checked. Okay. Does this give you enough clarity on for and while constructions? Good. In case you have a problem anywhere, suppose you are not very sure what is the value of v, what is the value of c, this is where you can put the debugging statements. You can reverse. ai is equal to v. So, I put this and I put c in at v. But the first v, how will it check? So, you read one value, check that value, put it in the array, add it to some and read one more value. Perfectly fine. That is exactly what we are doing in the while. But we were explicitly incrementing i, initializing i, which we do not have to do. No. How will it work? Because the array has only 100 elements. If I do not say i less than 100, then it will keep going and it will go 100 and 100 and 203. There are no elements like that. Sorry? No. I mean, I would not be able to put it in the array. If the idea is to only sum any number of numbers, it is a different story. Okay. So, is this clear now? Fine. I will solve this problem. I have a number 561342. What I want to do is I want to store the digits of this number in an array like 561342. And I want to produce another number. So, let me call this number as n1. I want to produce another number n2, which consists of the same digits in reverse order. So, I want to print 2, 4, 3, 1, 6, 5. This is the problem that somebody asked. How will I do this? I do not know whether the given number will be a 6 digit number, 7 digit number, 2 digit number, 3 digit number, 1 digit number. I do not know that. Somebody gives me a number. I want to extract digits from that number, push them into an array. And at the end, I want to also construct a reverse number, which is composed of putting the same digits in the reverse order. This is my problem. How will I do this problem? So, I start by first defining int n1, n2. Let us call this array as digit array. How many digits I should put? It does not matter. I can have 20, 30, 40. In old days, when computers had kilobytes of memory, then if you needed 9 elements and put 10, the chief manager will shout at you. Today, the computer memory is enormous. Of course, when you are talking about millions of elements, you have to be careful. But when you are talking about 8, 10, 15, 20, 100, it does not make a difference. Besides, later on we shall see when we deal with object classes, which have been artificially defined to do what we call high precision arithmetic. I might actually be having a mechanism to represent 100 digit numbers, 30 plus 100 digit numbers and so on. Sorry, you have a question? Sorry? Int n1, n2. n1, n2 are two numbers. So, there is no meaning. I am saying that today there is no meaning because the maximum number of digits is 7. Oh, because I am saying int. Okay. What should be the number? 10. The problem with 9, 10, 7, 8, etcetera is many times the number of digits depends on the implementation. So, while I agree entirely that 100 is too much, too bigger number, but arguing between 7, 8, 9, 10, one should not say 7 or 8 or 9. In general, from our knowledge, for example, tomorrow if instead of int, I just say long, but I have said digit 7, then I will have to change that specification also. And that is one of the reasons why I would be liberal with the array specification. However, here the point is not to illustrate and argue upon what should be this value, but to illustrate that, I will in general have a larger array and I will be using only a portion of it. That is the point I am trying to make, nothing more. So, whatever, let's say digit 10. I have n1, I have n2, I have digit 10. Is there any other variable that I might require? Since I am going to scan the array, I will need one variable to represent the index of the array. So, I might as well represent that by i. Anything else that I may need? Well, so far it looks like okay. I will go ahead and if I need something else, I will come back and define it. So, first thing of course is I read the value of, now I have to successively identify 2, then 4, then 3, then 1, then 6 and 5. What is the method that I choose? This is the analysis I should do before writing my program. One method that occurs to me is that if I divide this number by 10, I will get this. If I multiply it by 10 and subtract the result from this, which means I take modulus of 10, I will get 2. So, essentially then I have a number which is n1 originally. If I take modulus of that n1, I will get this digit. And if I divide this number by 10, I will get this number. I can repeat the process. I can take this modulo 10, I will get 4 and I divide it by 10, I will get 5, 6, 1, 3. If I keep doing this, somewhere the modulo 10 will be 5, when I divide 5 by 10, I will get 0. If I get 0, then there is no more number left. So, I can now see an iteration cooking up in my mind. I keep taking modulo, pushing the result in an array element and keep dividing it by 10. Keep doing this, keep doing this till the number becomes 0. And I have to push every element in an array. So, let me use the first method where I did not use a for loop. For loop is useful when you are traversing an array, when you know the number of elements or maximum number of elements. But I can use that effectively if I have a break or while condition etc. But here what I will do is, I know I am starting with n1. I will start with i equal to 0. Now I will say, why? Why n1 is greater than 0? It is either 5,60,000, 342 or 56,134 or 5,013, 613 or 561 or 56 or 5. I want to keep doing it. If that number becomes 0, I want to stop. It is obvious that I am going to successively reduce n. Initially n1 is this, then n1 will become less, less, less. What I have to do? I have to extract first digit of n1. n1 is not negative. What is the extraction? I take modulo 10. Where do I want to put it? In the first element of the array. Is that correct? Yes. But I do not know what is the last element. I am going to contain a 0 element. No, that is okay. What do I do with the first digit? I am getting this digit 2. My i is starting with 0. But I have written here, I want to put digits like this. You can start with the first digit. He is really a dreamer. He says, since I want to put 5 here, instead of starting from here, start extracting the largest digit. How will I do that? No, so how will you find out? 10 raise to what? I do not know that. If you say 10 raise to 5, I will give you 565. Your algorithm goes into culture. Okay. Now I will tell you, this is the problem. This problem is happening because somehow for some reason we decided that we will store the digits in this order. Why is it necessary to store the digits in this order? We simply want separate identity of digits. We will sky fall on our heads if we store these digits as 2, 4, 3, 1, 6 and 5. Is it not identifying separate digits and keeping them separately? Just because we made the mistake of imagining that we will be storing these digits like this, we are getting into a problem, because I can start with i equal to 0, but I cannot start with i equal to something which is not known here. I do not know how many digits are there in N1. So please re-examine your assumptions and mindset. You want to separate out digits. Nobody says you must separate out digits in this order. Even if somebody says I want the most significant digit first, then next, etc., etc., etc. I will first extract digits like this and then invert the array, because then by the end I will know how many digits are there. So that is the logic that you have to apply. If I say not like this, but I want to store like this, then it is very obvious to me that I can say digit R is equal to N1 modulo 10. Agreed? I will divide this by 10. Take the modulo remainder. It is 2. 2 will go here. But now I have to reduce the value of N1. Simple. N1 is equal to N1 by 10. When I divide N1 by 10, I will get 56,134. And now, do you agree that I will extract all the digits? At some point N1 will be not greater than 0. At that time I will come out. When I come out, what will be the value of I? The total number of digits in that number. Remember, if there are 6 digits, this will be 0th element, 1st element, 2nd element, 3rd element, 4th element and 5th element. You would have made a last assignment to digit 5 and you would have incremented I to 1. So, I will become 6. So, the total number of digits in your number is the last value of I with which you come out. Let us say we had one more thing called number digit. So, number of digits. I can simply say number digits is equal to I. Do you agree with this? So, now I have an array in which I have extracted all digits. I know that the least significant digit is in the first position. The most significant digit is in a position denoted by N0 digits minus 1. So, if I want to print a number or construct a number N2 which consists of reverse order, what should I do? I should take this number multiplied by 10 raised to 0. Add this number multiplied by 10 raised to 1. Add this number multiplied by 10 raised to 2, etc., etc. till I reach 0. So, I travel this array in a reverse order. Last number 10 raised to 0 is nothing but the last digit itself. Consequently, what I will write beyond this, we have extracted these numbers and we have got our algorithm to extract these numbers here. Now, as I said, I want to go in the reverse order. I know the total number of digits. I know the last digit. The last digit is what? N0 digit minus 1. Now, I want to have an index which will move downwards from N0 digit to something else. Can I not now say N2 which I want to construct? I start with N2 equal to 0. Now, I say for I is equal to N0 digits minus 1. Remember, N0 digits would have been 6 here. So, N0 digits minus 1 is 5. I want to start with 5, 4, 3, 2, 1, 0. When do I want to stop? I want to continue this as long as I is greater than equal to 0. And I want to increment, decrement I. I want to reduce I by 1 by 1. And all that I need to do here is to N2, I have to add what? Multiplied by what? If I multiply this by 10, then I go back. I take this digit 6. I multiply that also by 10. 10 raised to power. How do I calculate 10 to the power of I minus 1? So, here is a simpler technique. I use an additional variable here called m, which is the multiplying factor. Initially, I said m equal to 1. And I multiply this by 10 and m. m is 1 initially, so I am effectively multiplying by 10. Now, I say m is equal to m into 10. Will this work? First time, m is 1. I multiply digit I by 10 into 1, that is 10. I add it to N2. So, I will get the value 5. Next time, sorry, this is the correct value. So, digit I into m. Initially, m is 1. I will multiply by 5 by 1. N2 will become 5. Then, m is incremented to the next power of 10. 10 raised to 1. When I go back here, I will go to the next digit, which is 6. This time, I will multiply 6 by 10. So, I will add 60 to 5. m will become 100 now. I will go back again. This time, I will take 1 multiplied by 100. So, 100 will get added to 65. Slowly, I am composing this number. Is this clear? The reason I elaborated it so much is because these are the kind of pitfalls that you will come across when you construct an algorithm. Please remember, analyzing a given algorithm is relatively simple. You have to simply execute for some values and see whether it works or not. But designing or constructing an algorithm requires you some kind of imagination. That imagination should come from what you are trying to do here. You know, you are going to try to multiply this by 1, multiply this by 10, multiply this by 100 to get this number. All that you need to do is set up an iteration, which does this multiplication properly. Is that clear? Yes. It is kind of like just see-out digitized algorithm. If you write see-out digitized, it will appear as if you have got the final number, but it will be only printed. If you were to use this resulted number for some other purpose in your algorithm, then you would not have that number available. So, you remember something. You do not write programs for printing something or reading something. You write programs for computing something, which is likely to be used for something else. Remember, so far in our course, we have been writing programs with a single purpose. Do this, do that, do that. That is not what software is about. In your project, you are probably writing software, which may be anywhere between 10,000 or 20,000 lines of code. You will break it up into smaller pieces. Now, you cannot say, oh, I have done this, I will just print it. Because whatever you have calculated, somebody else will be required to use it. So, always make assignments and then go ahead and do. There is, but there is no need to do that. This is a neater algorithm. Calculating power, there are functions. They are costlier. For example, when you calculate power 10 raised to power 4, you are effectively doing 10 x 10 x 10 x 10 x 10. Calculating 10 to the power 10 x 10 x 10 x 5 times. See, you already calculated 10 raised to power 4. Why do you want to calculate 10 raised to power 5 independently from the beginning? You multiply that factor by 10, you will get it. I can see that people are getting impatient, but I have covered all the material for which I was asked to solve the problems. I will just request you to keep looking at similar problems, keep writing programs and keep cross checking the program that you write with each other. All the best. Thank you.