 limited representation of precision that we have and how do we solve them? We proceed ahead with the discussion on functions and arrays. I would like to mention however that in this particular case we were able to solve the problem of representation. But suppose in a particular domain area we needed not 50,000 points to be considered but 5 million points to be considered or 50 million points to be considered then this solution would not work whatever data type that we decide to use which is intrinsically provided by C programming language. Consequently we have to have other mechanisms obviously artificial mechanisms by which we can solve this problem. These mechanisms generally are called multi-precision arithmetic implementation and the multi-precision arithmetic implementation is in fact quite common in high end numeric analysis problems. If time permits I will discuss one of such problems in later sessions so that you can get an idea of how such problems will be solved. With this we now go over to the discussion of functions. As most of you would know functions in C are used to isolate certain computational instructions and put them separately. So, consider a quadratic function let us say A x square plus B x plus C the derivative is f dash x equal to 2x plus B. There is a special purpose why I am considering fx and f dash x but it does not matter if I want to compute the value of any one of these two or for that matter any similar function for different values of x again and again and again then instead of putting these multiple instructions at different places in my program I could separate out these instructions and create a separate block. Such a separate block is called a function in C. This function is defined in terms of certain parameters. It takes the values of these parameters from us and it returns a single value. Here are two examples of the functions. The functions which I just displayed through their algebraic definitions they would be written in C as follows. Float my function float A float B float C float X inside this function I define float value calculate the value and then I return that value. Observe that our students would have seen the mechanism of int main and return 0 already working. It is at this stage it will be proper to explain to them that int main is actually also a function which is a block of instructions written separately this time by us but that is treated as a function by the operating system. Here we can emphasize details such as definition of the parameters that are supplied to the function and the definition of the value. We can for example explain that while a computed value such as this variable value can be returned we can also return an expression which is computed here. Needless to add we should also emphatically state that the computations will depend upon parameters which come when this function is invoked and therefore a function typically will not have any input output instruction. Typically a function will not read a value and output a value but a function will collect the value through parameters and will supply the result back through the return statement. Here is an example of two functions my function and my derivative which represent actually the two algebraic functions that we had written earlier. This one as float A float B float C and float X as parameters. It is useful to emphasize that just by writing parameters names nothing is served because what you are writing here are place holders for the values which will come from outside and therefore the type of value is important. In fact either later or at this stage itself when you teach your students about function prototypes you will be mentioning that the names are irrelevant completely. So whether A, B, C, X etc. are irrelevant in my prototype in actual function definition I must have them but in prototype definition I need to define only the types and that is adequate. How do we use these functions? First consider the syntax of this function. I write int my function followed by in parenthesis float A float B etc. and an opening brass below which is the body of the function. So we explain that the first word tells the type of the value that will be written by the function. Implicitly that means a function can return at most a single value directly using the written statement. Students would be easily convinced of this because they would have seen return 0 or return minus 1 in some of our program. Next is the name of the function. The name of the function that we choose. Here I have chosen my function. This is followed by one or more parameters and here again we can explain repeatedly that the parameter values will come from the calling instruction. Note that the function is called from within another program but it is not a line in the program or a segment in the program which cause this function. It is actually a single instruction which cause this function as we shall later on see the invocation. And we also ask the students to note that the return statement, return value it tells us what value will be sent back to the calling instruction. This point is not actually emphatically stated in textbooks which stop by saying that the function is called from within some other program. That is of course a correct statement but that does not accurately represent the exact activity that happens here. I hope to illustrate that through the example that we consider next. But first we describe function in our simplistic model. This all depends upon at what point in time you introduce function to your students. But if you want to introduce it early enough which can be done it may be useful to relate to the simplistic Dumbo model of our computing. So if we think of our computer as a C Dumbo we can imagine that each such function is actually a separate assistant Dumbo. Anytime a function is invoked within an instruction which C Dumbo is executing what C Dumbo does is he simply hands over the given parameters to the assistant Dumbo. The assistant Dumbo then uses these parameters and calculate the function value as per the instructions contained in that function. So the assistant Dumbo himself knows that function all those instructions. So he will execute those instructions, will calculate the function value and will return the same to the main Dumbo. When C Dumbo gets that return value he carries on exactly where he left using that return value. The point to emphasize here is exactly where he left. So if he left in the middle of an instruction to call this function main Dumbo will return exactly to that point in the middle of that instruction. This should be illustrated perhaps by an example similar to what we will see now. First a function called within a program a function is invoked simply by using the function name with appropriate parameters within any expression. It will be useful to tell our students whom we might illustrate the use of functions by writing a function called as a separate statement. It is important to emphasize that a function reference is actually an expression itself if it is standalone. Ordinarily a function reference will occur within a more complex expression. Consider the Newton-Raphson method which we did not discuss in detail yesterday but if we have a value xr and if we want to calculate the next value the Newton-Raphson method says that the i plus 1th value for x will be calculated using the ith value and using the ratio of f of xr divided by f dash of xr. This is a standard simple numerical technique. Now notice that if I want to implement this computation I will be using an existing value of x and calculating a new value of x. So the right hand side is the expression which says take the existing value of x compute the function at that value and use that value substituting it for f of xi representation or reference. Then compute f dash xi that means call another function using the same value of x and use that value in the expression. So please note what is happening at this evaluation is I have some value of xi. My Dumbo will start computing this. It will say this is x so it will get the value of x. Then it says this is a function reference so it will send the same x to that assistant Dumbo and will get a return value which it will substitute here at this point itself and continue evaluating that expression. Now it says slash f dash xi I will get this same parameter x to another Dumbo get f dash xi evaluated come back with the resultant value and substitute that value in this expression. And finally our main Dumbo will calculate the final expression which will be xi minus some value written by the first function divided by some value written by another function. So the function reference occurs at a specific point within the function evaluation or within the expression evaluation of some kind is what is according to be important to emphasize to our student. Suppose the function was fx is equal to ax square plus bx plus c then we could design our program using the two functions which we have written my function and my derivative. If we go back to the previous slide we will notice that we had this function my function defined to calculate ax square plus bx plus c and we had defined my derivative to calculate 2 into ax plus b notice that 2 ax plus b is f dash x if f of x is this. So go three slides further this is the program the Newton Raphson method using the function call to calculate the root of an equation. We are trying to find out one root of a quadratic ax square plus bx plus c provided of course the root exists. So here is the program we defined x a b c and root all as floating point quantities we read a b c values I have not put those input instructions here we start with x equal to 1.0 we need an initial guess for Newton Raphson we are not discussing the Newton Raphson method so I will not spend time in describing how do we arrive at the initial value and so on that is the issue about numerical analysis the point that we are trying to explain to our students in this example is what happens when functions are invoked so I start with x and I have a Newton Raphson iteration setup which iterates for 10 times it is very interesting to note that 10 iterations are often sufficient to get an accurate value of the root so look at the iteration for int i equal to 0 i less than 10 i plus plus so this is the iteration which will be executed 10 times this by the way is also an occasion where I am using that definition of a variable inside an iteration it would be useful to introduce the notion of scope to your students if you choose to use such an example whereby you say that while I could have defined int i here where I have defined other variables I choose to define int i here because this i definition will be limited to just this for loop outside this for loop this variable will not exist this permits students to start imagining the constraints of scope of variables in our programs now see the program itself it is a simple single line expression evaluation so it says x is equal to x minus my function a comma b comma c comma x divided by my derivative in bracket a comma b comma x this is where you may want to take a couple of minutes to explain to your students what is happening when these function calls are made so you say that the computer now is executing this instruction this instruction is an assignment instruction where I have a left hand side LHS variable x that means x will get a new value on the right hand side amongst other things I see x itself very clearly this is an reassignment type of instruction that means there was an original value of x which will be modified by this expression now we come to the right hand side the right hand side actually is a complex expression which says x minus a function call divided by another function so we explain to our students that the computer when executing this instruction it will have to evaluate this expression first because that is the right hand side while evaluating this expression computer will take the value of x from the location x then it will try to take look at this value now this value is not available as a value but it is a function call this is where the computer hands over control to the instructions of the function that we have written and which he will be aware of this function will be invoked now the computer will at this stage take the values of a, b, c and x and pass on these values as parameter to that function as we have seen the instructions in that function the function will use these values and will return a single value what is important to tell our students is that this single value which is returned by that function will be substituted instead of this entire reference so this function reference will be substituted by a value 2.583.21 minus 1.12 whatever whatever there will be a single value here the activity does not stop here because computer has not yet evaluated the expression fully when it looks at the expression further it comes across another invocation of a second function my derivative this time again the computer will collect the values of a, b and x will send these values as parameters to that function my derivative instructions in my derivative will now be executed and whatever value is returned will be brought back here and the computer will continue evaluation of this function it will be useful to write on a board on a white board or on your computer screen itself actually taking some values for x and for each iteration showing what is the value which is returned by my function what is the value which is returned by my derivative then simplify this expression to say something like x minus 2.5 divided by 1.8 or whatever and then saying x was equal to whatever 1.0 and therefore the total value of this expression is so and so and that is the value which is now allocated to x in short a we must describe to our students how exactly the functions are written and the advantage of writing functions namely that the main program becomes extremely simple and more readable provided we have chosen the function names appropriately to reflect what kind of computations they are doing second the method of invocation of a function is from within an expression sometimes I may invoke a function as a single instruction at that time I have to emphasize that that single instruction is nothing but an expression which is self evaluating it does not assign a resulting value to anybody but that is just a function noticeably such functions will either be void functions or functions which do input output or error reporting and such thing computational functions however will almost always be invoked from within a computational expression such as this here is a summary of application of invocation rules when I execute this instruction x is equal to x minus my function abcx divided by my derivative abx when dumbo encounters my function while evaluating the expression it suspends execution of the program goes over to the defined function with the available values of the parameters calculates the value executing given instructions within that function and then returns back to the main program replaces the reference to the function by the return value so this function reference is replaced by the value return and continues evaluation of the remaining expression although this is stated in most textbook and this is what we explained perhaps we do not empathically state that a function returns exactly to the point of its invocation and that whatever remaining things that were being done in the evaluation of that expression continues as if the function call has been replaced by the final value that you get so it will be useful to keep that in mind to emphasize these points there are of course many other issues with the function I am sure you are all familiar with them but you can read the standard textbooks and describe those appropriately when you teach functions it is important to give a lot of examples of the functions and function invocation standard textbooks by the way often try to describe the stack model of function invocation and if a function is invoked within a function what happens and so on and then introduce the notion of what we call recursion my own opinion is that recursion need not be introduced very early in the subject but it should be introduced sometime later when people are more familiar with computers and they have a slightly better understanding of inter-program communication or inter-process communication we will now in the remainder of this session discuss arrays the motivation of arrays should always be given through an example which illustrates why you may need a structure called an array the example that I choose often is first to show what will I do when I have to find out maximum of three numbers so I say here int main int v1 v2 v3 max then I input values of these three variables I start with max equal to v1 I check if v2 is greater than max then I say max is v2 I check if v3 is greater than max then I say max is v3 and then output the maximum number this is the program that I would like to find out the maximum of three given values what if I have to find out the maximum of four given values well I will read four values and find out the max by adding one more if condition what if I have to find out the largest of twelve values well I will write such twelve comparisons then what if I have to find out the maximum of two thousand values now this will become sillier and sillier I cannot keep writing instructions of this kind consequently I use this to motivate students to think that I need a separate mechanism to represent a collection of large numbers or large values large number of values and to do something commonly with each value observe that what is being done is that the value of max which is arbitrarily associated with the first of those values is being compared with each of the remaining values so if I have two thousand values I start with assignment of first value to max and I have to compare max with each of the remaining one thousand nine hundred and ninety nine values rather than writing one thousand nine hundred and ninety nine instructions I would like to write a single iteration which moves let's say a variable I from one to one thousand nine hundred and ninety nine and within each iteration I compare the value of max with a new number I at number let's say and reset the maximum value if that new number happens to be larger than the current value of max the problem is that new number will have to be represented by some name and I can use only one name at a time so in my program if I use V2 well V2 will be the variable which will get compared always now if I have one thousand numbers which are distinct from each other what I want to ensure is that in such an iteration I would like to consider a different number for comparison in every iteration such a mechanism is not available sadly through the definition of simple variables and therefore I conclude that I need some additional mechanism so in short we say that we have situations where we need to handle multiple values of similar kind we cannot define and use different variable names for each of these values firstly it is inconvenient as I said if our 50 values or 12 values I will have to define V1, V2, V3, V4, V5 it's very inconvenient but more importantly it is impossible if we do not know how many exact values we need to handle particularly if we have to handle different number of values at different times while running the same program we can't do that if we use variables we will have to write a separate program for exact number of values that we want to tackle because those exact number of values will have to be represented by exactly those many variables apart from being inconvenienced to find different names for those variables it is an impossible situation because I cannot write 20,000 programs to handle one for handling one number two for handling two numbers etc further we extend this concept by saying that look we may wish to manipulate matrices we may want to add two matrices we may want to multiply two matrices we may want to find inverse of a matrix now such operation where matrix itself is a collection of similar numbers I mean values then we cannot handle them through the conventional mechanism of representing a value by a variable therefore we need a different mechanism we then note that C programming language provides a mechanism called arrays we may also note at this stage that every programming language that is used by people generally has a provision an intrinsic provision of defining arrays not of one dimension but of multiple dimensions we can illustrate this example further by showing how a one dimensional array of hundred integer numbers could be mapped on memory locations so if this is our Draval model of memory locations then I have A1, A2, A3, A4, A5, A100 as ordinary variable names however I would like an organization where each one is not considered a unique and different variable name but each one should be considered as a part of a larger collection as is shown here in the notion of a normal one dimensional array for example if I have an array which contains n numbers then for n equal to 6 these may be sample numbers which are inside the array and I will call this as the first element this as the second element this as the third element this notion will be known to our students already all that we are saying is that in our computer memory model we do not want these numbers to be stored as contained into locations having different unique variable names but we somehow want to retain the identity of this entire thing as a collection and we should be able to address any particular element of this array in a fashion which will permit me to use that addressing mechanism inside my iteration loops so I now show that I can indeed have array elements and explain to my students that an array could be considered to be implemented as a collection in my conventional memory locations except that the entire collection is called simply by single name a so this drawer is a this drawer is a this drawer is a this drawer is a etc of course each drawer contains a different value so to distinguish between different elements of the array I use the notion of indexing consequently we say that each element behaves like any other variable but is referenced by the array name and an array index and we notify to our students that a 0, a 5, a i, a j minus 5 could be valid references to any element of the array a 0 will mean 0th element a 5 will mean 5th element actually 6th in the context of c a i will mean ith element provided i has a valid value a j minus 5 is another valid element provided j has a correct value we indicate to them that index itself can be an expression so j minus 5 for example is an expression and if j is equal to 8 then j minus 5 will be 3 and this will refer to a 3 which is the fourth element in this array we also observe to our students by asking them a question what happens if the index value is less than 0 or greater than 100 if my array is defined to be of size 100 it is very very important to emphatically tell our students that if this happens then computer could give unpredictable results because c programming language does not define any error bound checking on the index values crossing the limits so two things here then we described this in terms of our simplistic model but tell people that the array definition permits us to use a single name to collectively refer to a set of elements here is an example of 100 elements of similar values being stored in an array so 100 locations are reserved for it starting with a 0 to a 99 this as I think I had mentioned earlier is a rather stupid way of representing arrays because in mathematical notation wherever we use arrays we always refer to the first element as a 1 and the last element of a 100 element array as a 100 c has chosen this method of indexing for the simple reason that converting the instructions related to index computation to machine instructions is extremely easy here so the original designers thought that let the array bounds between 0 to n minus 1 if n is the size of the array however we illustrate the fact that we are still talking about first element, second element, third element etc if there are sixth element it so incidentally happens that in c programming language the first element of the array is actually 0th element in terms of the index value and the last element of a 100 element array is having an index value of 99 with this explanation array manipulation can then be discussed in your class here is a way to find maximum of n numbers so for example I have an array defined as a 100 so it is a 100 element array I have a variable defined as max I define n as a variable describing the maximum number of elements and I define i I am sorry this i has become capital when I use Microsoft power point any standalone i is converted to capital i so please remember to correct it in your notes I first read the value of n from input and I read the n elements of the array there is a question here let me take one minute off to answer this question let us go back to that slide just remember that the array a is located in these memory locations a 0, a 1, a 2, a 3, a 4 up to a 99 what the compiler does actually is that it allocates a block of memory that means so many storage locations suppose these are all integer numbers then 4 bytes for each location will be allocated and there will be a base address given to the first location of the array this base address which will be some internal address value let us say this is location number 5000 inside computer's memory then the first element is automatically location number 5000 when it goes to calculating the index for 1 it simply has to add 1 unit of 4 bytes to this base address so if the base address was 5000 the address of this location becomes 5000 into 1 into 4, 4 being the unit of memory allocation so it can simply use the index to multiply the unit of bytes and add it to the base to calculate the exact memory location of that element if I use indexing from 1 to n I will need to do one subtraction and then multiplication by the unit of 4 bytes to calculate the exact address of the machine location so it actually is more efficient way of internal address computation and that is the reason why people have chosen 0 to n minus 1 as the index limits I hope this answers the question so here was the algorithm for finding out maximum of n numbers this algorithm is straight forward and you can explain how within this iteration every time I come across a i it will mean actually a different element of the array because i is different every time so well we still have 10 minutes for the t break so let us have this interaction and I have questions from remote centres can we go back to the remote centres now good morning sir is it possible to create a for loop that never executed is it possible to create a for loop that is never executed very easy put a condition which is false right at the beginning now are you talking about file loop or for loop I was mentioning the for loop a file loop will always be executed at least once because the condition is checked at the end no sorry the repeat until will be executed at least one while our for loop may not be executed even once if the condition is not satisfied right at the beginning I hope that answers your query let us go over to SGS ITS Sir my name is Shailesh Bada from SGS ITS Sir what I want to ask that we teach our students on DOS in which we are having very good facility to trace and debug and add watch and many other facilities but what we are seeing here in Unix based we have get output in dot slash a dot out and to debug a big program it becomes difficult in Unix so what other facilities are we having in Unix to debug and to see how our program can be break and add watch or such type of thing sir thank you sir this was my question I will answer this question but first let me correct your notion you are associating these facilities with the environment for example your question said that in Microsoft we can do these things and Unix what we observe here is we cannot do these things I will recall your attention to what I had mentioned yesterday and I will repeat it again please remember that these facilities for debugging have absolutely nothing to do with either Microsoft or Unix neither of these two which are operating systems provide any one of the facilities that you mentioned the facilities that you mentioned are related to writing, compiling and debugging C programs and therefore these facilities are provided by the C compiler system and the associated tools so and this is very important because if as teachers we use these words to tell our students or if our students tell us that this facility is there in Microsoft but not there in Unix or this facility is there in Unix or not there in Microsoft then we must correct them immediately we must emphatically state that these facilities are absolutely nothing to do with operating system so what you describe as a facility in Microsoft it is not a facility in Microsoft but it is in a tool which happens to be running in Microsoft and being used by many people the correct answer to your question is there are exactly similar tools available in Unix environment for every programming language and there are exactly similar facilities that are available in Unix programming environment but these facilities are made available through separate tools more specifically the GCC compiler that you are using the compiler merely compiles your program correctly but in order to debug and trace for example the values of your variables as they change through iterations etc you require debugging tools a couple of debugging tools should be available in each of the remote centres if I remember correctly and I will ask my assistants to confirm whether the debugger DDD was included as a package when we supplied the packages yes my team confirms that the debugger DDD has been made available it may be so that in the last session so far you may never have been required to use those packages or it may be so that in your earlier exposure to Unix you would not have come across such debuggers but these debuggers exist not only in Unix, not only in Microsoft but practically in every operating system environment so in a nutshell my answer to you would be that please during one of the lab sessions try to use DDD it's an extremely good debugger which will permit you to trace permit you to put stop points, check points execute your algorithm in steps and check how the values of variables are changing okay let's go over to the next institute KJ Somya Mumbai has a query I have a query related to recursion yesterday while teaching iteration you said that there are many applications which can be recursive applications which can be implemented using iteration so could I know the practical application of recursion thank you sir in fact the example which I considered yesterday the Fibonacci series itself is defined in terms of a recursive formula and the example illustrated how we could implement that recursive formula using iteration so what we did yesterday was to take a recursive equation h n is equal to h n minus 1 plus h n minus 2 and implement the computation of that series using an iterative mechanism what I mentioned yesterday is that recursion to be implemented as a recursion is also available in most programming languages including C I will actually discuss recursion explicitly and mention both the advantages and the pitfalls of recursion in one of the sessions next week we are now at the Nagpur has a query Nagpur from BMC College of Engineering my name is Umayad I want to ask you a question that you say that we can start an index element from 0th location but what happens if I start from 1 1 position now over to you the question is not what you want to do the question is what C language permits so if you start using an index from 1 the 0th element of your array which would have been created by the C compiler will just sit there idle however if you have 100 elements in an array and if you insist on using the index from 1 2 3 4 all the way up to 100 things will work well till your index reaches 99 the moment you try to refer to a 100 the C compiler will give up please remember that the C compiler does not introduce range checking when it translates your C programs to machine instructions consequently if you assign a value to a 100 in your program where there are 100 elements and the last element is actually a 99 then what will happen is the value that you are assigning will go into some memory location which may destroy some other variable value in your own program and you will get completely unpredictable results therefore such I am glad you asked this question it is very very important to emphatically tell our students that any index in an array in C always ranges from 0 to max minus 1 if max is the size of the array this is true for 1 dimensional array and this is true for multi-dimensional array as I explained somebody amongst the designers just decided to be more comfortable with the translated instructions being more efficient he or she did not worry about how the programmers will feel about it particularly people who have studied other programming languages where the index varies from 1 to maximum size people find it uncomfortable but unfortunately there is nothing that can be done about it and our students have to get used to the fact that defining an array with some size s the array index will always vary from 0 to s minus 1 so this is a mandate it is not a question of choice for any one of my students I hope that satisfies the explanation we have two more minutes before the end of this session so if there is another quick query we can take it otherwise we will break further t there are no queries that is good news that means everybody is waiting for a cup of tea so am I so thank you very much