 The last point I wanted to make, and this is an important point that you must make to all your students, that the programs which we write are read not only by compilers but also by programmers and other human beings. Therefore, while it is important to write syntactically correct statements which are read and handled by the compiler, it is also important to write our statements in a way in which other human readers can read and understand our programs. That is the reason why programs are expected to be intended properly to distinguish various blocks within the program and that is why the programs must have commands. In fact, this habit should be inculcated in your students right from day one. Any program that is written for example, even a file in program but if it is written without a command should be actively discarded by you saying that no this is not the way to write the programs. So, to make that point, this is the slide which said you should include extra description or commands to help other programmers figure out the logic. There are two styles available. The traditional style in C is the second style slash star followed by a command followed by star slash. This command can expand over multiple lines. The first style shown here is a later style first introduced in C plus plus and also available in C. You put slash slash everywhere within this line and from that point up to the end of line the whole part is taken as command. So, to conclude one must encourage students to write commands and good commands in every program which they write. There is another important notion in the assignment which students find it difficult to understand and therefore it must be emphatically clarified to all students. The notion is that of reassignment. The reassignment notion says that if I have declared an integer variable such as m and I give a value m equal to 5, later on in my program I said something like m equal to 3 into m plus 1, then sometimes the student get confused. It is important to tell them that this is a reassignment statement. You will recall that one of the participants was asking a question about LHS and RHS. Perhaps this is the point where to explain that this is not an equation. In fact, you can state that if the value of m is 5 and if this was to be treated as a equation by our Dumbo or our computer, we will get a mathematically absurd result. For example, this would simplify to 0 equal to 2 star m plus 1 and for no value of m this equation can ever be satisfied. Therefore, this is not an equation at all. Consequently, we must emphasize that the computer interpretation of this instruction is not to treat it as an equation because it is not an equation but to treat it as comprising of two parts. One part on the right half equal to symbol and another part which is the left half equal to symbol. We must emphasize that the right hand part is actually the computational part which contains an expression. That expression is evaluated by the machine using the precedence that we have defined and a single value is obtained and subsequently that value replaces this entire expression. If m is equal to 5, this value will be 5 into 315 plus 1 which is 16. Consequently first, this entire instruction is reduced to m equal to 16. So, there are two parts to this instruction. One is the evaluation of right hand side expression and the second is assignment of that value to the left hand side. It so incidentally happens that left hand side is m. This reassignment is an extremely frequently used operation particularly inside iteration loops and therefore students must be very clear about what this means. Let me now consider repetition or iteration where I said such reassignment is extremely important. This is that I am discussing iteration without explicitly talking about the conditional execution of statements. Curiously enough, people who are non-programmers find it intuitively easier to understand iteration than conditional branching and execution because it involves stepping out of some of the statements which are not required to be executed based on certain condition being made and so on. Whereas iteration is a more intuitive concept which most people understand as repeatedly doing something. The iteration is most cleanly specified in C using the FOR statement if the iteration is for a specific number of times such as for example finding out the factorial of a number or sum of natural numbers and so on. The exact technical specification of a FOR statement is explained through this particular format. Incidentally, this may not be the format that you would like to introduce to your students for the first time. However, whenever you explain the FOR loop or FOR iteration whether after discussing the conditional or independently as I am doing now it will be useful to define this particular syntax as a generic syntax at some place. It says FOR xxx semicolon yyy semicolon zzz semicolon sorry not semicolon back at close and then followed by www. What is important here is to indicate that xxx is a statement or a set of statements. www is a set of statements yyy is a condition and zzz is again a set of statements. I have another slide which explains what exactly it does but it cleanly specifies a number of iterations. For example, if you want to iterate some action from 1 to n or from 0 to n minus 1 you can use a FOR loop and all that you can tell people is as long as the program is executing up one after another instructions here the moment it comes here it suddenly stops going forward till it has iterated around this body www and after the iteration is complete as per the specification which we shall see in a moment then only it goes to the next statement. This is very useful in numerical computing which we can explain through an example. So here is an example to find the sum of n natural numbers I start with sum equal to 0 I define a integer variable i which will change over the summation and the value n where I want to find out the sum of first n natural numbers the way the sum of n natural numbers is defined if n is say 5 then sum is equal to 1 plus 2 plus 3 plus 4 plus 5 if n is 28 it is 1 plus 2 plus 3 plus 4 plus 5 etcetera plus 27 plus 20 here since the value of n is not known I cannot write explicit instructions to carry the calculate this sum because an expression requires specific variables specific constants etcetera this cannot be done because n is unknown so this is where I use an iteration so look at this iteration here I start with sum equal to 0 then I ask the computer to iteratively execute this reassignment statement sum is equal to sum plus I if I have some fixed value say 5 or 7 or 0 or 1 then it will keep adding the same thing to sum however if I change the value of I and this is what this for instruction does it's initiates I to 1 with that it executes sum equal to sum plus I after checking whether I still less than n it goes back again and keeps doing this after incrementing the value of I by 1 as is shown by this reassignment so please note there are two implicit two reassignments in this one is an implicit reassignment as a part of the for specification and the other is the reassignment as a part of the action board you will all agree that this will calculate the sum of n natural numbers I find this slide to be technically quite rigorous accurate and adequate to finally explain exactly what are the rules governing the execution of any for statement so here is the syntax for for statement for xxx semicolon yy by semicolon zzz followed by in process www here are the various steps which are written here as 0 1 2 3 4 to indicate what exactly happens the first thing it says execute xxx when you reach the statement in your program this must be an assignment statement called loop initialization immediately after that it says evaluate condition yyy so it should be a valid condition later on in this session we shall quickly see what are the valid conditions but you already know that comparing a and b b and c or whatever for equality less than greater than here the comparison typically that we saw in the last example was whether I was less than n so this is called a loop test whenever you examine a condition the condition may be true or it may be false if it is true the action statement says continue with step if it is false execution of the for statement ends and you go back to the step after this that is you go to the statement after the for statement somewhere here so to recapitulate when the for statement is encountered first xxx is executed then the condition yyy is evaluated if the condition yyy is true you go to statement level 2 if not you terminate and get out what is that level 2 level 2 says execute www this is the loop body in short if this condition is true you go to loop body if this condition is false you skip the loop body and get out of the for statement after executing this www you do not fall out of for loop but you execute zzz which is called the loop increment notice that we had i equal to i plus 1 in the previous example after executing the loop increment it says continue from one and one is evaluate the condition this is exactly how the iteration is defiled you will notice that www is shown as a group of statements in the process much before that you would have indicated to your students that an opening brass and closing brass can contain any number of statements and all of them are considered as a single equivalent of a single statement by the c compiler so wherever a single statement is permitted you can actually use this in fact in the loop body if you had a single statement you could have simply written that statement here after these bracket and put a semicolon so that single statement would have been executed as loop body for as many times as the iteration requires again conditions can be stipulated in C by using a notion of a operation b where a and b are any values they could be expressions in themselves or they could be constant values or simply variables the op is the comparison operator the standard comparison operators permitted in C are less than greater than less than equal to greater than equal to equal equals stands for equal to and exclamation mark equal to stands for not equal to this is a peculiar notation unfortunately designed by the early developers of C programming language so we can change it it is very very important to tell students to use equal equal whenever they mean a comparison between two values because a single equal is a symbol of assignment and that could lead to many of predicaments and wrong programs so equal equal is equal to exclamation mark equal is not equal to we can of course combine these conditions such that we can sell a less than b or a greater than 0 and a less than equal to 9 so and and or which are the logical conjunction and disjunction respectively are again indicated by using a double e occurring symbol you have to say and and and you have to use a vertical bar twice to denote or this is again a idiosyncrasy of C there is no reason or justification that is it that is how it is and that is how we have to use you could define these condition evaluation mechanism by the way ahead of time indicating very clearly that whenever a comparison operation is done the value is either 0 or non-zero defining a false or true value I will now rush through some of these slides because these slides give you some very good examples with which to describe to your students some nuances of computational processes and computing something useful in different domains you will remember that there was an exercise to calculate logarithm of a number this particular example shows how natural logarithm can be calculated using Riemann integral which is nothing but finding out summation of small rectangles defined under the curve to calculate the area under the curve we state here that we want to solve this problem please note that natural logarithm of a is defined as integral 1 by x dx from 1 to a so since computer cannot integrate it must use arithmetic operations and mathematically this integral is equivalent to the area under the curve fx equal to 1 by x the area from 1 to a if we approximate this area by small rectangles why because it is easier to calculate the area of a rectangle so if I have said 2000 rectangles then by summing up the area of these 2000 rectangles I can correctly reasonably correctly estimate the value of log a here are some examples for example calculating some of areas of all rectangles between 1 and a this diagram shows that I have drawn large number of rectangles starting from 1 up to a so you see this is the first rectangle this is the second rectangle the characteristic of each rectangle is that the height of rectangle is the value of this function 1 by x at the point at which I am drawing this rectangle so at this point 1 the height is 1 at next point depending upon how many rectangles I have so let's have to calculate the area between 1 and a then a minus 1 is this total wealth so if I say 500 or 100 rectangles a minus a by 100 will be this wealth so once I know the wealth and the height the height incidentally is 1 by x since at 1 1 by x is 1 this height will also be 1 at other points it will be different this actually is a good way to show your first year students how to calculate integral of 1 by x fx and thereby how to calculate similar integrals later on by using numerical computations so this goes on to further illustrate that what I want to do is to calculate the area of any one rectangle and then keep doing similar calculations for all the rectangles under this curve so if I take an arbitrary rectangle the width of that rectangle will be same as the width of all other rectangles and that width is a minus 1 by n if there are n rectangles totally in this particular angle so if I divide the interval between 1 to a into n intervals I will have n rectangles each with a wealth a minus 1 by n if I multiply this wealth by the height of this rectangle I will get area of this rectangle and if I have a computation done similarly for all the rectangles then summing them up I will get the value of the area under this curve and therefore the value of the logarithm the x coordinate of the ith rectangle will actually be the value of 1 upon x so x is equal to 1 plus i minus 1 into w observe how this is done this is one that total number of rectangles or m this is ith rectangle the first rectangle x coordinate is one the second rectangle x coordinate is 1 plus w the third rectangle begins at x which is 1 plus 2 w and so on so in general x coordinate of ith rectangle is 1 plus i minus 1 w why are we interested in finding out x coordinate because that will permit us to determine the y coordinate which is nothing but 1 by x notice that we already know the wealth if we know the height which will be 1 upon x for that particular rectangle we know the area so this is what is shown here the height of ith rectangle which is this point is nothing but 1 by x and since x we have determined in the last diagram this is equal to 1 upon this value once we know this height into w is actually the area of this rectangle and now we can use the powerful concept of iteration to start with the first rectangle calculate its area add it to some sum then to go to the next rectangle add its area to the sum go to the third rectangle and so on essentially then how many rectangle should we choose well the answer is more the merrier so suppose we choose thousand rectangles now the total width of rectangles is a minus 1 therefore the width of each rectangle of thousand is a minus 1 by thousand the x coordinate of the left side of the ith rectangle is this and height of the ith rectangle is this we have already seen this in the diagram why am I repeating this for you I would like to humbly suggest that drawing some diagrams to explain this mathematical concept of estimating area of a triangle under a curve goes a long way to easily explain to our students what exactly we are talking about if we just write these expressions they are mathematically correct description of the situation however many students are not able to perceive how exactly this height is calculated or this is calculated and therefore I will repeat we go back to the previous diagrams first we show that these are the rectangles whose sum of areas we want to calculate then we show that for any one particular rectangle the width is constant which is this then we show how to calculate the x coordinate of ith rectangle then we show that the function is 1 by x and therefore height can be calculated using this and now we rewrite all these values that we have so far obtained in mathematical or equation form by first deciding that we have say thousand rectangles then these will be the width and this will be the height etc. please note that if this thousand is replaced by some number n which is a variable then the width will be simply a minus 1 by n given this a program to compute logarithm then becomes very simple notice that this first part of the program is as I had shown earlier are my own definitions of macros which are not really required if you are using scan f and print f in your actual program you can do that but this might simplify the situation the program itself is very simple this is the program I define an integer i I define a floating point value x I start with area equal to 0 I define a variable w and now I get the value of x this program calculates log of x and not log of a to that extent this program does not match the explanation given I am sorry for that you should either change that a to x or you should change this x to a but with this small problem corrected you will notice how elegant and simple the whole program is I first start with the 0th rectangle I calculate the w I am dividing it into thousand rectangles as I said for i equal to 1 to thousand I calculate the area is w into the height which is given by that formula which is again in the terms of w and I so far I rectangle I get this I added to this area as per the rule of the for loop once this is executed I do not get out but I go and execute this particular statement which increases I by 1 and when it increases I by 1 I becomes 2 which is still less than equal to thousand so I come back again and this time this particular equation is evaluated with I equal to 2 and then with I equal to 3 and then with I equal to 4 and so on these are nothing but some kind of tricky equivalent of the print a statement which will print the value properly those of you are interested can try executing this program on your machines and you will get exactly the kind of result that you would expect in fact I do not know whether that assignment is there in your assignment it would be useful to compile this using say thousand rectangles hundred rectangles ten thousand rectangles and find out at what point you stop getting any better accuracy for the computations and that would be that the reason would again be the kind of misbehavior of the floating point representation of numbers these are some notes to represent the same computations in a slightly different way here is a consolidation numerical and string data is represented like this backslash m should be explained to the students in terms of all the backslashes backslash t for tab etc. Constant values are written like this by us see stores these in an internal format numerical values have an associated type into long float double etc. Memory location names are called variables must be predefined and must be associated with data types each instruction of our program can ordinarily convey any one of the three actions assignment input output the assignment is either sum equal to a plus b kind a complex expression or a reassignment increment is a special form of reassignment this question was asked by one of the participants so count equal to count plus one is a reassignment which increases the value of count but this can also be stated as count plus equal to one in fact this is a generic expression I can write a star equal to b which means I have to multiply the existing value of a by b so what is the arithmetic operator here that operates upon the left hand side itself as an operand and the right hand side of course the expression is evaluated and the final result is allocated to count more simply you can write this without any assignment statement as plus plus count or count minus count plus plus both have different interpretation this one first increments count and then uses its value this one you the second one uses the value of count in whatever expression and after uses increments its value these are unfortunately and quite unnecessarily according to me very tricky things and they have been perhaps defined by the original C programming designers C programming language designers as a means of shortcut these also translate more easily into natural machine instructions which have specific instructions to increment and decrement counters registers memory locations and so on however in terms of understanding of our computer programs unless you are very comfortable with whatever is happening I would recommend usage of a standard assignment statement of this type input output instructions in C are not natural there is no harm in agreeing to this to all your students right at the beginning saying that input output operation which is important from a human perspective is not naturally done in C and that is why you need these functions that is because the original designers of C programming language were never interested in providing any input output statement these were implemented as functions later on today you have a large standard input output library and we shall discuss the input output functions in greater details later suffice it to say that these are handled through special functions and depending upon the sequencing of your lectures you would have either introduced printf scanf earlier or you can use such artificially defined macros and differ the discussion of scanf printf functions to a later date I think we have 10 minutes and we may wait for some interaction so what I will do is like a movie show I will go through the remaining slides and I will leave it to you to read them understand them and raise any questions please do that in your spare time before or after your lab and if you have any questions please make sure you send an email to workshop support because it is the image that I will get if you have any questions on this but I hope that most of you will understand the contents of these slides they merely give two examples the second one is more difficult to understand it is not intuitive because of the way the example has been stated in the form of some historical sort of revolutions are done by Indian mathematics first one okay I am sorry I am sorry this is an important point again I will leave this because all of you are familiar with conditional execution so here I am saying that once you examine conditions you can actually execute either one set of statements or another set of statements but not both this is standard stuff you will typically use flowcharts to explain this what is important here is to say that group of statement one and group of statement two could be a single statement in which case it could be written without the process but you should tell your students always to write the process so that later on any modification does not change the nature of execution so this is where you want to use conditional execution for example if a ticket is given to an adult at a different price and half the price for a child then after reading age how do you calculate the value of ticket if you just write these two statements one after another then only last one will have impact you cannot write one because then you will have to write one program to calculate the ticket for a child and another program to calculate the ticket for an adult that is where the conditional statement or conditional execution is required and you can easily state that this problem can be solved if I say if age less than 12 a greater than 12 then ticket is 25 rupees 50 by say as ticket is 12 rupees 75 this simple example can be used to convey the essence of conditional execution however you can use this simple example itself to extend to what is known as a if else ladder in see what is that ladder suppose children are given ticket as 1275 elders are given ticket as 2550 but sorry the the grown ups which is non children are given tickets at 2550 but suppose a bus company has a rule that if you are more than 60 years old such as I am for example I am entitled for a special concession and I am charged only rupees 20 not 25.50 now how do I do this computation further you can introduce the notion of if else if ladder so you can say if age greater than 60 ticket is 20 else if age is greater than 12 ticket is this else ticket is this this kind of else if ladder is used very routinely by most programmers and professional programmers care has to be taken to emphasize that the sequence in which these conditions are checked are written properly otherwise if a condition which is valid always is written at the end then even if some intermediate condition is valid and that statement is done you will miss out on the computations correctly it is also important to indicate because you will be required to teach people the switch statement the switch statement is considered by many as an alternative to if else if ladder however professional programmers who develop real world applications where the application software or c programs have to be read by other programmers do not prefer switch statement they prefer an if else if ladder this is a matter of choice you can use either one but if else and if else if else are the two prominent structures by which to control the flow of execution of the statements in your program. Now as I said I will quickly have a slide show of slides but you can read all of these these will be uploaded on the Moodle and will also be made to the co-ordinators so here is an example of repetitive actions which calculates the factorial there was a query from one of the centers what will be the largest number for which the factorial can be calculated the correct answer is the largest number n for which the factorial can be calculated is such n for which the factorial is larger than the largest number that is representable on the machine since the largest number representable on the machine itself is implementation dependent the value for n for which you can correctly calculate the factorial will depend upon whether your c program has been compiled using turbo c GNUC whether the compiler is working on 16 bit machine 32 bit machine or 64 bit machine it is useful to find out what their dependency is however it is obvious that since the factorial grows very rapidly you cannot find out a factorial for very large number for example calculating factorial 100 factorial 50 etc etc is completely ruled out but I will leave it to you to figure out what is the maximum value of n for which you can calculate factorial and then I will leave it to you to start thinking about how to calculate factorial 100 or how to calculate factorial 50 a problem that shall be posed as an exercise first and the solution will be provided in this workshop itself at a later day this is a very interesting problem Hemchandra problem where he said that if you want to build a wall of 8 feet length and if you have bricks which are either 2 feet or 1 feet then in how many ways I can lay the bricks so that I fill the 8 feet the number of ways in which you can lay the bricks actually these are the possibilities you will observe that all these possibilities will have the last brick as either 2 or 1 and this fact was observed by Hemchandra in a context of a musical problem which led to the discovery of a beautiful series which is known to all of us in a different way the actual problem was designing a poetic meter with 8 beats the meter is made of short syllabus and long syllabus so short syllabus is 1 beat long syllabus is 2 beats now how many ways are there of filling up these 8 bits was the query that here this is an example of a poetic meter by the way the entire set of slides on the Hemchandra problem have been created by my colleague Professor Abhiram the other day when he taught this course last time I found this very interesting and exciting and therefore I have included it here so here is a poetic meter for a fairly well known a shloka kundan dutushar ahara davala you will notice that the beats are long and short the issue mathematical issue is how many ways are there to filling up these 8 bits with long and short syllabus and Hemchandra's answer is very interesting he says by the method of Pingala it is enough to observe that the last beat is long or short and if you observe this you can actually calculate the number of ways in which these 8 bits can be constructed there are two important points to be made here and that is why I am spending time on this slide first of all Pingala was a mathematician poet from 500 AD today we are in AD 2000 so he lived 1500 years ago it is important to observe that Hemchandra is giving credit to someone who lived hundreds of years before him so Pingala was the one who first discovered this method of finding out the solution and then Hemchandra applied it let us go back to the last slide to the actual problem of finding out how many ways in which these beats can fill up the 8 bits the next few slides indicate how Hemchandra evolved that solution using the method of Pingala which Pingala's method simply stated that it is adequate to observe whether last bit is longer short so here is the solution if S is a class of 8 bit patterns with short last bit and L is the class of 8 bit patterns with long last bit then each 8 bit pattern is in class L or classes and we count these classes you can find out the total number of ways it so happens that we have a recursive solution these slides maintain mention that solution this slide explains algebraically how do you represent the number of patterns in a recursive fashion so if you know for example H6 and H7 you can calculate H8 but to calculate H7 you need H6 and H5 and so on in general this is the formula you will all notice that this formula is actually a representative of a fairly well known series this is the algorithm idea of how to calculate first H1 then H2 knowing H1 and H2 how to calculate H3 knowing H3 and H2 how to calculate H4 and so on right up to H8 and in fact beyond but what is important is you would have all realized going back to the previous slide that in general if this is the recursive formula to calculate Hn then this is a well known series the program for that series is as simple as this I read the integer number n I set a previous value to 1 current value to 2 these are the first 2 H1 and H2 and then for 3 onwards up to n I simply add previous and current to get the H next however before going over to the next iteration I reset previous and H current to H next and H previous respectively which is done through these assignment statements and as you would have guessed this is nothing but a Fibonacci series this code is tricky so you put a comment at the beginning how do you prove that you get the right values we this is just a small proof that is given here what is important is that there is a proof by induction why am I showing all of this here is an important point telling your students that this is how you can iterate is one way of explaining how iteration works but telling your students why iteration is important should need a use of good examples if you see the numerical computational examples that we saw calculating of log logarithm of n and another computation which follows these slides that will explain the purpose of using iteration more clearly this particular problem of Fibonacci series explains that recursive problems can also be solved iterating subsequently we shall discuss the notion of iteration recursion and show both the advantages and pitfalls but an additional point that is being scored here through this example why I like this example is to illustrate the fact that the Indian heritage in mathematical discoveries is quite superior and most of us have forgotten about it it is useful for example to remind people that first you are getting mathematics from poetry here second that the series is very interesting and it shows the number of petals in flowers etc or ratio of consecutive terms tend to a limit which is nothing but a series called Fibonacci numbers and professor Ranade observes that him chandra lived long before Fibonacci so this also proves a point a subtle point that while we use maths developed elsewhere in the world it is not that we do not have adequate mathematical heritage perhaps this point is not important from a computer programming point of view but for every Indian student it will touch a chord somewhere if you use this or similar examples in your illustrations another example of illustrating iteration is the Newton-Raphson method as I said I will go very quickly over these slides without reading them this method essentially depends upon your ability to calculate f of x and f dash x the example given here tries to find out the square root of k through an iterative mechanism and how to get a better estimation of the next value once you have an initial estimate so you have an initial estimate x i from which you determine the value of the initial point is x i point x i comma 0 which is point a then you calculate f of x i which gives you the point b then you sort of draw a tangent at that point and you find out the intercept on x axis which will give you the x i plus 1 and you continue doing that till your x i plus 1 approaches the actual 0 of the function which will give you f of x as 0 so since the tangent can be drawn using f dash x i or the derivative that is the reason why you should know why the how the derivative is to be computed anyway I will not go through the details but the subsequent slides tell you how to calculate the square root and this is the code for calculating the square root there is another way where you can put the entire computation inside the for loop itself I personally don't like this code because there is no computation within the for body the body itself is located inside the for state it's a very interestingly and a tricky way of doing things but it is interesting so you can show this to your students however please do not recommend this kind of programming to be done whenever people write proficient program but this is an interesting way there is another way yet so there are multiple ways in which you could code a numerical problem this is what is to be stated here there is also a wild statement I will not go through the details all of you are aware of how wild statement works so while condition followed by loop body there is also a similar statement called repeat while and repeat have standard interpretation which can be very easily explained to the students the for loop is perhaps the most difficult to understand because apart from controlling iteration it does variety of other things as well and the last question that needs to be answered is which one should our students use for or why so the answer is if there is a control variable with initial value update rule and whose value distinctly defines each loop iteration use for but if the loop executes for a fixed number of time I am sorry there is a mistake here if the loop executes a fixed number of times use for but if the loop executes based on a condition which may change during the execution you should use why I think I already exceeded my time I will stop at this stage