 Good morning everybody. We will go ahead with our second lecture in this workshop, where we will discuss some fundamental programming concepts through simple programs. Essentially in this lecture, I will briefly revisit the Dumbo model for a program, then discuss the structure of a C program. Looking at some of the lines, if you recall we said that to the beginner students, those lines may not make much sense because most of them are either compiler directives or some very funny syntax for printf and such statements. What I propose to do is to look at a program listing through the eyes of a student and describe what eye as a student would feel when I read that program. We will then of course go over to discuss the notion of variables and data types. We will discuss the expression evaluation and assignment. Then we will describe the problems which require non-sequential execution of instructions and thereby describe the notion of conditional execution if statements and its variance. In the second lecture after tree that is lecture three, we will discuss problems which require iterative solutions. So, to recapitulate in the Dumbo model of computing, we saw the abilities of a computer through the eyes of a Mr. Budduram or Dumbo, where we said that he had memory locations in which he could keep values, he could add, subtract, multiply or handle or manipulate values through some operations and calculate results and he could of course carry out input and output operations on those values. More importantly, we said that Dumbo is capable of executing a program which contains instructions and these instructions are executed sequentially. An instruction may accept value as input and store these in designated memory locations or a value may be computed by the computer as per our instruction and the resultant value stored in another location. Alternately, one or more values can be copied from memory locations where they have been put earlier through either of these two operations and these values can be given to us equivalent to an output operation. We specifically mentioned that the memory comprises of locations each of which can contain a single value. More importantly, we associated a name or a tag with every location. Such name is called a variable as the value in that location may vary depending upon the operations. A location is never empty and we took some time to describe how this model is different from a drawer model because there could be an empty drawer in a cupboard. However, we said that a memory location is never empty. It will always contain some value. The value would be there either because we have put it in through an input operation or the computer has computed something and put it there as a result of an assignment operation or there could be a value coming out of some previous computation which the computer might have carried out earlier. What we will do now is look at a computer program and look at it from a student's point of view. So I have added the word student's thoughts. I believe it is important for us to imagine what students would be thinking when they read a program for the first time or when they are being introduced about the computing environment and so on. So when we discuss all our sample programs for example prog1.c and prog2.c, we notice presence of several lines which really did not make much sense to us immediately. What we said is that we may understand these at a later time and we become more familiar with the programs. But what I propose to do now is to just identify these and take a note of the nuances which these lines represent in so far as the student's thinking process is concerned. So basically I am looking at the problem of evaluating an arbitrary formula which I have written as v equal to n1 by n2 plus x. And I wish to evaluate this formula for specific values of x, n1 and n2 where x is 3, n1 is 2 and n2 is 5. What I have written in blue color are the thoughts of a student. Please note that a student has no clue about integer division or integer and floating point values etc. etc. at this juncture. A student merely sees as a formula as he or she will understand it in the context of 11th and 12th standard mathematics. In the mathematics there is no notion of precedence. All operations are executed from left to right unless overrided by parenthesis. In this particular case any student will think that n1 is to be divided by n2 and x has to be added to it which incidentally would be the correct execution. However when we say n1 is 2 and n2 is 5 a student will automatically calculate this as 2 by 5 amounting to 0.4. Then he will add x to it which is 3 and therefore the student will expect value of v to be 3.4. This is the mindset when I tell a student that I am writing a program to evaluate this formula for this value. Let us look at the program now. Include stdio.h. Include stdlib.h. We do not know what these two things are. Slash slash proc3.c slash slash arithmetic operations. Well as a student I have seen something like this earlier and somebody said these are comments. So perhaps I understand these. Then I have int main opening bracket closing bracket opening brass. This definitely looks funny to me. Subsequent lines appear familiar int n1 n2 float x v. Still further lines do appear very familiar because these are instructions that I had seen our Mr. Dumbo model or computer executing them. So n1 equal to 2, n2 equal to 5, x equal to 3 are all simple assignment statements which I recognize v is equal to x plus n1 by n2 appears to be my formula. Print f is another funny statement I notice. Return 0 I have no clue as a student on what return 0 could mean and then there is a closing brass. So this is the program. Let me very quickly jot down what I as a student may feel. So for example when I look at the first line I would say this line does not seem to make any sense. Perhaps it is asking the computer to include some pre-return instructions. Please note that every student at this stage is speculating dreaming imagining because there is no concrete knowledge available to the student at this juncture. There are two different ways of handling this situation by the way. One is to bombard the student with exact information, the significance of which may be completely lost because the concepts are not clear. The other and the one which I prefer is to actually encourage students to dream, imagine, contemplate, think, speculate. Why? Because essentially when they look at things and speculate they are thinking and later on their understanding will be that much clearer. So what I have written here is what the student would speculate. Does not make any sense. Perhaps asking the computer to include some pre-return instruction is the meaning which I understand from this. As a student I look at the next two lines. Well these appear to be commencement for women readers. Thankfully this was told to me when my teacher described the earlier programs. I look at the next line now. Int main seems to be something important but cannot understand the meaning. This is the true feeling of me as a student. Perhaps the line indicates the beginning of our main program. How do I speculate this as a student because I see the word main? Let us go further. Int N1 N2 float XV appears to define variables for the formula. However as a student I will say I do not understand the words int and float. Int perhaps means integer in which case float should mean a real number. As teachers it is important for us to understand the psychology of a student who has come to us after his first standard examination. He or she understands numbers as real numbers or integer numbers. There is no notion of floating point representation in the school mathematics and therefore the word float is completely Greek and Latin to the student. But you see a student might be correctly able to speculate that if int may mean integer values in which case the float should mean a real number without perhaps fully understanding. Next three lines as I said make eminent sense because I have seen similar instructions earlier in a program which you as a teacher discussed in my class and now I will speculate that these are clearly the instructions written to solve the problem. However I have a problem with the formula. The way the formula is written the computer may add N1 to X and the result will be 1.0. Why? Because the formula says V is equal to X plus N1 slash N2. Please note that our notion of division and multiplication having a precedence over addition is not at all clear or understood or known to our students. For them all formulae are to be evaluated from left to right that is the mathematics that I am taught and therefore a student may rightly speculate that the computer will take the value of X as 3 add it to N1 which is 2 making it 5 divided by N2 which is 5 and therefore come up with a result which is 1.0 and rightly the student will say this is wrong in the context of the formula that was originally given as the statement of the problem. Next I look at as a student I look at the next line printf value is %f backslash N all in double apostrophis comma V bracket closed semicolon. Well looks like an output instruction similar to C out. Remember the program that you showed me as a teacher earlier had an instruction C out where you explained C out means output. Why I speculate that this is similar because I am guessing this is the word print appears in this. However why is it written as printf well I do not know also what about the funny symbols inside the parenthesis what do they mean I go further there is still one line left I look at this line what is my impression as a student this is very confusing perhaps the line indicates returning value 0 to someone I am speculating but to whom and why was 0 why do I spend so much time describing this it is my considered opinion that if you have 100 students in the class each one of those 100 students would be speculating differently about the meaning of different lines in our program because at that stage remember we have not explained the details on the other hand unless we write a complete program the student will not be able to execute that problem which is our earnest desire to take the student to a lab and make him run the program to see that the results come out properly. So is this speculation good or bad in my opinion opinion this speculation is very good indeed this particular selection of comments that I have made let me quickly go over these once again please note that most of these speculations are tending towards correct interpretation however many students will miss all of this completely what is the purpose of writing these here then the purpose is actually to guide the students to speculate in the right direction and also to tell the student that no problem if you do not understand these things at this juncture it does not matter things will work out well and you will understand these later I would submit that using such a slide where you actually deliberately fuel the speculation or thinking by the student on the interpretation of these lines could be a good beginner to open up the mind and heart of the student after all what is our objective the student must think about everything that he or she sees and then applies his or her mind to learn problem solving I believe this purpose is eminently achieved by using this approach. So these are the comments which I have written some of you might like to write similar comments based on your impression of what your students would be speculating it would be a very interesting thing to see in which manner different students think anyway having said this we will have to go ahead and tell the students some real facts about the programming language syntax and semantics which is implemented by the computer. So in a nutshell when a student looks at this program as a student I will say I execute the programs in the lab from your files prog1.c and prog2.c those work correctly they collected input value from me and return the correct computed value as output so all those funny lines must have some purpose which I do not understand now however even at this stage I must understand the way my variables are defined and the way the computations are carried out so that I can write instructions to correctly implement the desired calculation. So look at it the student has already figured out that there are a few things that I do not understand I have also figured out what I must minimally understand even at this stage to write my programs correctly all this then forms a very nice precursor to discuss the basics of the programming language elements first we will describe briefly the computing environment when you teach this course you might delegate this description and in fact a larger description to a laboratory class when you take your students to the lab just as you yourself are attending lab and for some of you the ubuntu environment may be a new environment but most of you know computers well that will not be the case with the students and therefore some description about the computing environment is well in order but please understand from my previous deliberations that everything that you talk about will be more or less greek and latin and the student at this juncture would be attempting to understand things mechanically and would like to learn those things quickly which makes sense to him or her from the perspective of writing programs and running those programs so therefore we can for example talk about a compiler which can translate instructions into the machine language it can do input output with us it can take values in decimal numbers and fractions and convert these to and from an internal representation which if we wish we can talk about the binary representation at this stage we may briefly talk about an operating system merely as a collection of program which controls the behavior of the computer we then may mention that we interact with the computer through the operating system commands using a keyboard and a monitor a monitor displays values and strings and graphics to us and a keyboard collects values as input from us we interact with the operating system through what we call a command prompt at which we give some commands to execute pre-written programs and then the fact that we shall be using Ubuntu for this course which is a variant of a Linux operating system we may want to describe in a class typical interaction with the computing environment this may be preceded by the login screen where we explain the purpose of logging in etc and then we may say some of the commands most of you would have executed these in the lab yesterday so pwd for example will print the working directory currently then cdcpp will change directory to a directory called cpp when I say ccproc3.c it will compile my program so when I ask the operating system to list my files it will list may be prog1.cproc2.cproc3.c and a new file called a.out please note that as a student I do not understand the significance of a.out so you will have to explain to me that I must blindly believe that this is the result of the compilation process and this in fact contains the executable version of my instructions given in my program there is only one a.out file I have to explain that whatever is the program I have most recently compiled this executable will correspond to that particular file and finally when I say dot slash a.out the output will come value is 3.000000 in fact a student may be astonished because as per his or her calculations the program should have resulted in the value 1 and this is the time where we now explain the notion of data types the basic operations precedences etc. etc. we begin our description by first describing handling of numerical values it is the numerical values that the students understand best and it is therefore most useful to describe the handling of numerical values first so we talk about the notion of assigning a memory location to a name which is our variable name and we also state emphatically that it we define that type of value that can come or can be associated with that team clearly then a particular name which is associated with the type cannot contain a value of any other type we are also indicating that there are different types of values that we can deal with int n for example results one location in memory for n to store integer values we can at this stage tell the student that there is a definite range of values that can be accommodated and typically on a 32 bit machine the maximum positive number will be plus 2 to the power 31 minus 1 and minimum negative number will be minus 2 to the power 31 minus 1 as I said I am against describing in details the binary and hexadecimal number systems at this juncture for a general programming course however if it is the practice in your own institution and university that binary number system and the number representation in terms of signed and unsigned integers is considered to be covered before everything else this is the right point where you can perhaps use the opportunity to describe some details of binary number representation etcetera a question may arise in the student's mind that what happens if I want to handle a value which is larger than this well the answer is we may use the notion of long integers for example when we say long m we describe to the student that this is a variant of integer where the range of numbers representable is larger unfortunately in real life most of the machines put both int and long as 32 bits unless of course you have much larger machines such as the yesterday's main frame where the long would be a 64 bit number with a maximum magnitude of 2 to the power 63 minus 1 as I said earlier we can mention that the computer actually uses a binary or hexadecimal representation and therefore the limits on the values are expressed in powers of 2 or of 16 and not in powers of 10 however for the time being we will continue to believe that computer uses decimal values why because as far as integers are concerned there is a one to one mapping between a binary integer and a decimal integer and there is absolutely no difference in numerical evaluation of expression involving integer numbers our computer does arithmetic with constraints because the limited capacity so for example I may have two values which individually are well within the range of representable values and therefore can be accommodated in any storage locations however when I perform an operation such as multiplying two large number or even adding two very large positive numbers the resultant number may go out of the range and what happens then is a matter of speculation at the moment for the student so we have to tell the student what kind of error we might see resulting from computations which require results to be larger than the capability of a particular location to handle that value representing point numbers and explaining this representation to the students is not an easy task many of us would realize it from our own experiences what I try to say very simply is that I have to represent real numbers or fractions these real numbers or fractions are stored differently inside a computer using what is known as a floating point I describe the floating point to have two components one which is called a mantissa and the other which is called an exponent somehow I have to convince the student that the entire number is still stored in a single location as identified by my variable name but I say that this location itself is now compartmentalized so one compartment holds mantissa and the other compartment holds exponent and that the identity of mantissa and the identity of exponent is independently known to the computer and can be handled independently so we represent mantissa by m and exponent by e and we tell the student that the number value is taken as m into b to the power e where b is the base since computer uses a base 2 or 16 that is how the number will be represented in terms of bits or extra decimal digits however we will take it to be our usual 10 base because that is how we write values in our program it is for the compiler to convert these constant values that we write to appropriate representation internal so in the program we write such values as say 2.53 which is a decimal fraction or minus 7.2 each 15 which is a scientific notation with which our students are already familiar we now use the keyword float to describe float y which reserves one location for a variable y to store the number in this particular format some more explanation is in order I try to do it using this slide I say that no decimal point is actually stored in a floating point representation but it is assumed to be at the beginning of mantissa so for example if this is the representation containing two parts mantissa and exponent I might have minus 4127 in the mantissa part and 11 in the exponent part it is exact meaning as far as the computer and I are concerned would be that the value will be taken to be minus 0.4127 into 10 to the power 11 so observe that I am saying that a decimal point is implicitly assumed at the beginning of the mantissa similarly if I have a value stored here as plus 49462 and minus 21 this is the mantissa and this is the exponent I explain that this will be interpreted as a value 0.19462 multiplied by 10 to the power minus 21 please note that we are also conveying to our student through this simple diagram notation that mantissa can be individually positive or negative and so can exponent be positive or negative. However we will also indicate to the student that in my program just as in real life the first value for example need not be written always minus 0.4127 into 10 to the power 11 I can write it as minus 0.4127 e11 if I want I can even write as minus 41.27 e9 student will understand that both are equivalent values all that we need to tell the student is that in our program if we write it either this way or that way internally the computer will always convert these to an equivalent representation which is this. We further explain that the mantissa can store about 7 decimal digits and the exponent can be roughly between minus 100 and plus 100 again like the larger magnitude integer values we say I can have a floating point representation which can accommodate much larger values than this including a larger procedure please note that eventually we have to tell the student that the game in floating point arithmetic is not so much about the exponent but is about the limited precision of mantissa and this is where if I use double set then it stores a location which is in principle double the size of original location but it gives me about 14 decimal digits of precision for mantissa. It is a good idea to explain right at the beginning that there could be problems predominantly arising out of the fact that there are limitations on number representation. So since computers offer limited precision the computations which are carried out on given values may lead to inaccuracies specially with floating point numbers and special care needs to be taken in order to handle such round off errors. There are by the way many textbooks describing numerical analysis the best I have seen are numerical recipes in C there are now corresponding books called numerical recipes in C plus plus and numerical recipes in Java. One of the best series that I have seen describing extremely complex computational algorithms including solution of simultaneous algebraic equations or even differential equations partial differential equations finding out roots etc. Excellent book as a matter of fact most of our students from mechanical engineering civil engineering, aerotical engineering etc when they study structures and finite element analysis later in their career they would actually be using these methods extensively. Indeed the purpose of teaching programming at the first year level to students of all branches is actually to equip them with adequate programming knowledge so that they can tackle these problems. It is for this purpose that in our course here I use many examples from numerical computations and I would urge our colleague teachers also to do the same. Here is one example of problems with floating point arithmetic for example we know Avogadro's number is roughly 6.023 to 10 to the power 23. Imagine that I have a memory location Avogadro to which I have assigned a value 6.023 E 23 after all in many of my computations this Avogadro number may come into prominently used. Suppose I have another variable Y whose value is 1.5 I may innocuously calculate something as a value of W which is Avogadro plus Y I would expect actually the correct reflection of this addition however what is the value of W well we know now because of the representation that we have seen that the Avogadro number is a very large number 10 to the power 23 and therefore when I add 1.5 the final addition or the added value will differ from the Avogadro number only in the 23rd digit since there is no way a 23rd digit is ever going to be stored in my computer's memory location because float stores only 7 digits of mantissa digits beyond that are ignored this is what is the root of error consequently the value of W is same as Avogadro. It is useful to emphatically state this fact very early that unless we are careful and we will have examples later in this course and I am sure you would be having in your course which will exemplify this and also indicate how such things could be handled. Given the problems with floating point numbers I have jokingly coined this phrase the first one I picked up from Leopold Kronecker who said God made natural numbers the rest is the work of man he was referring of course to fractions and decimal numbers etc. However, I have added humans made fractions and decimal numbers the rest is the work of numbers implying that the floating point representation in arithmetic causes a whole lot of heartburns. However, what is important to emphasize to our students is whatever may be the problems there is no other way except a modern computer through which extremely complicated and large calculations and computations can be performed quickly in order to get results for our mathematical modeling of many real world situations. We may use this opportunity to indicate to our students the notion of mixed arithmetic for example we will say that the computer can convert from float to integer and vice versa as needed. So in a program if a number is written without decimal point it is taken as integer otherwise as float. So if I say int n equal to 20 float y equal to 15.8 n equal to y 15.8 will be rounded down and n will be 15. Similarly, 360 by n integer result is calculated because 360 is taken to be an integer value and n is integer. However, 360.0 is taken as a floating point value consequently when it is divided by n n is converted to a floating point representation and then the floating point result is computed. Some remarks on arithmetic usually it is not difficult to convey to our students the meaning of simple arithmetic operators like multiplication division plus minus. The percent operator which is a modulo operator may take some time and it might be well worthwhile to explain through a couple of examples. For example we say percent means modulo remainder and therefore if m is 10 and n is 4 then m percent n will be modulo remainder of 10 divided by 4 which is equal to 2 and that will be the value allocated to x. We describe the precedence the left to right execution of operations at the same precedence and of course unary operators if you wish could be explained at this juncture without any problem. I had already mentioned yesterday that we should empathically state the importance of commands. This is another small slide to introduce the notion that commands can be written in two styles. In any instruction whenever I finish the writing of instruction I can write slash slash and write a command up to the end of the line. Of course this can happen at the beginning of the line also as you have seen in some of the programs which I have shown here. The other style is to enclose a command between slash star and star slash and here the commands can encompass several lines. So whenever you have to write some kind of a documentary description of what is happening you might prefer this style of writing commands. The important notion of reassignment needs to be emphasized. Students must be told that they must not take statements such as m equal to 3 m plus 1 as a equation. In fact this is a good example to say that if this were to be an equation then it would be mathematically absurd because by simply subtracting m from both sides of this equation if it were an equation I will get 0 is equal to 2 m plus 1 which is indeed an absurd. So in fact we should again tell that we can never write equations because the computer is incapable of understanding equations. What the computer understands is expression evaluation. So the meaning of such a statement or instruction when we give is that the computer does not look at the left hand side at all but looks at the right hand side. The computer interpretation is calculate the value of the expression on the right hand side come up with a single value and then this value is to be assigned to be stored in the variable which appears on the left hand side. RHS and LHS are perfectly valid and well understood notations by most of our students so this is okay. While we describe the expression evaluation we may emphasize the special nature of increment and increment happens very often in real life also. For example we increment a counter or the clock is incremented by a time ticking in one second or one minute interval. So the reassignment is in a special form applies to this increment such as count equal to count plus 1 means whatever is the current value of count incremented by one and then we say that C provides some very nice and simple looking abbreviations for such increments. So for example I can say count plus equal to 1 indeed we can explain that I can also say cloud plus equal to 5 which will be the equivalent of saying count equal to count plus 5. So the increment or decrement for that matter need not be limited to 1 but if we want an increment by one either or decrement by one a more simply I can write plus plus count or count plus plus. It is worthwhile to explain to the student through examples the difference between plus plus count and count plus plus. So basically the usage of a variable value in an expression happening before the increment or after the increment is an important attribute that students must understand so that they can use these things appropriately in their programs. Finally I would like to commend to my students that input and output instructions in C are not natural and these are handled through special functions. Indeed function has multiple meanings in English. Functionality is the meaning of the function. What we mean is functions in the computer programming sense which is true. C programming depends upon use of library functions for handling input and output. As I mentioned yesterday this is the reason why I prefer the use of C in and C out which significantly simplifies things. With this now we move over to describe the conditional execution of instructions. We may say that program instructions are executed sequentially normally. However we can examine conditions and based on the result of such comparison or the condition evaluation we can execute instructions out of sequence. What do we mean by out of sequence? If I write a statement in one group followed by a statement in another group normal execution will always execute the statements in first group immediately followed by statements in another group. That means instructions in both the groups will be executed. But if I have a situation where I want either instructions in group 1 to be executed or instructions in group 2 to be executed based on certain condition then I need a special instruction which is provided by the if statement. And we say thus way the if statement is written is we say if in bracket condition followed by a group of statements first group else group of statement second group followed by a semi group. So if and else is the what we call syntax of the if statement or the conditional execution statement that we have in C program. I have chosen a simple example to illustrate the need for conditional execution. This example is a real life scenario where if I travel by bus and I have to purchase a bus ticket then the bus service in my city might give concession to younger children whereas adults might have to pay larger fare. This is often the case and therefore is a quite realistic example. So here I assume that the adult bus cost ticket ticket cost is rupees 2550. I say whereas for a child less than 12 years the ticket cost is 12.75. Now if I have to write a program to find out the cost based on the age I say in age float ticket cost. But and I might read age in by using a scene statement. However if I say ticket cost equal to 12.75 and ticket cost equal to 25.50 because these are the two assignments I might have to make and I output the ticket cost I will always get cost as 25.50. So this is obviously very stupid way of writing a program because independent of the age this program will always print the ticket cost as 25.50. Even if I were to reverse these two statements I will then get 12.75 always as the cost. So I immediately realize that this is no way to give instructions to the machine to calculate the correct cost based on age. In fact I will this is where I will have to use the if statement because I want to assign either of these two values to the ticket cost based on the value of the age and not both. So if I read the value of age and then say if age greater than 12 ticket cost is equal to 25.50 else ticket cost is equal to 12.75 I will actually get the correct result at the end because when I come out of this if instruction at the end of this closing brass here whenever I output the value of ticket cost the correct cost will be output. It is possible that there could be a series of conditions that need to be applied for example imagine that to very old people the bus service charges 20 rupees not 25.50 as for normal adults and not as little as children somewhere in between. Now depending upon the age I have to calculate the correct cost I might be able to write my instructions to the computer in some fashion like this. So I have taken the value of age as input and now I say if age is greater than 60 ticket cost is 20 if else if age is greater than 12 ticket cost is 25.50 else ticket cost is 12.75 this incidentally is called the if else if ladder because I can have as many else if statements as I wish to evaluate a series of such conditions. What is important by the way is to ensure that our students understand the importance of correctly sequencing this ladder for example what will happen if I check age greater than 12 before checking age greater than 60. You can actually ask the students to run this simple variants of this program and give different values of age to find out what is the ticket cost that is printed. So that the students will quickly understand that the meaning of this ladder is first this condition is evaluated if this condition is not true then only this condition is evaluated if this condition is not true then next condition is evaluated etc and if all of these conditions are not true then and only then the last statement is executed. So this understanding of this kind of sequencing of the ladder is very very crucial here is an example to find the maximum of given numbers. So I have proc 4.cpp which finds maximum of 3 given numbers notice that this is slightly different from a typical example that we find in most of the text books if we have 3 numbers whose maximum is to be found out then what most books says first compare a and b if a is greater than b then compare a with c if b is greater than a then compare b with c and this is how a fairly complex flow chart is drawn which is then implemented to fight the maximum as teachers will all remember that whenever we have to find out a maximum numbers more than 3 we never use this approach which results in a large decision making tree. In fact what we follow in case of larger numbers is in my opinion the best way to describe the situation when even when you have 3 numbers. So I am saying that the way I will calculate is I have a b and c as the 3 numbers I have max as the maximum value which is to be computed I will input 3 values a b and c I will assign a to max and if b is greater than max I will say max is equal to b if c is greater than max I will say max equal to c in any sequence I execute these statements because these are these are not a if then as ladder these are all independent if statements each one of which will work at the end the maximum will be whatever is the latest value of max please note that this logic is not only simple but is clearly extensible as I have shown in the next example if I want to find maximum out of say 5 numbers then I could write my program as integer a b c d e and max and then input the 5 values a b c d and e and then like before I would say max equal to a if b is greater than max max equal to b if c is greater than max max equal to c if d is greater than max max equal to d if e is greater than max max equal to e and then I output whatever is the maximum value the point being made here is that whether I have 3 values 5 values 7 values the logic that I use to find the maximum is best I pick up the first number given to me assume that it is maximum and then keep collecting the next number and keep looking at the next number comparing with whatever maximum I hold you can even describe to the student that as if I pick up a number find out what is I assume that it is the maximum and keep it let us say in my pocket whenever another number comes I look at my pocket if the outside number is greater than what I have in pocket I throw the number from my pocket and put that number in my pocket so at any point in time what I will have in my pocket is the largest number amongst all which I have considered this is the purpose of showing the first algorithm of finding out maximum of 3 numbers in that fashion as I said this is easily extensive there is another objective in showing this example and that objective is to indicate to our students that something is still going to be difficult if for example if I have to find a maximum of 200 numbers now if I have to find maximum of 200 numbers of course it is possible for me to elaborately write down variable names as a b c d e f g h up to z a a a b b b c d d whatever whatever whatever I can casual of 200 names but I will have to write 200 instructions so I will have to give 200 numbers as input to the computer that I will have to give in any which way if I want computer find out the maximum but the amount of time and effort I will spend in writing these instructions is going to be enormous there is another problem once I write a program I would like a program to be used by different people for calculating maximum for the kind of numbers that they may have a friend of mine who wants to find out the maximum of let us say marks of all students in his or her class may have 58 students in the class another teacher might have 72 students in the class so the number of numbers whose maximum is to be found out itself may be different in which case it is clearly impossible to write a program in this fashion which will cattle to the occasion when the number of numbers itself varies and even if the number of numbers is known such as 200 as this number becomes large even 200 is pretty large and if someone says okay I will write 200 variable names and write all these statements 200 times I will say suppose the number of numbers is 10,000 so all this is uncanny so what is the objective of discussing this particular problem the objective is actually lead to the notion of iteration observe that we have achieved two things by looking at this example let me go back to the previous slide here in this slide I put up a logic to compare three numbers to find out the maximum and the comparison logic was like this in the next slide I am extending it to say I want to find out the maximum of five numbers and all my students will easily notice that the logic used in the previous version and this version is same in fact is the extended version and that is the reason why I say that even if I have to find out maximum of 20 numbers I can actually follow the same logic which is simple and appears repeat it here we shall use this fact later to indicate to our students how to solve problems using instructions which permit repeated execution of our instructions we may incidentally wish to specify in greater details the comparison conditions for example less than greater than less than equal to greater than equal to etc in this particular case it is important to a state that equal to is not a comparison operator in C but it is equal equal or use this symbol twice similarly not equal is a exclamation mark followed by equal to so we explain here equal equal is equal to and exclamation mark equal is not equal to within state that the conditions can be combined using and and or and not operations by the way almost all of our students coming out of the 12th standard are perfectly clear about the logical operations and or not so it will not be very difficult to for them to understand except that they will have to become familiar with different kind of symbols for example use of two ampersands to represent an and operation which is called a conjunction by many students of the school similarly two vertical bars or pipe symbols as we call them represent disjunction or or so we have and and or and in the programming language see evaluation of any condition results in a value one if the condition is true and zero if it is false as a matter of fact later on we shall see that whenever a positive value comes across during the evaluation of any condition it is taken to be true a value zero is always taken to be false with this as I promised I am leaving aside about half an hour for interaction so I will now look at the questions that might have been raised from remote centers please feel free to ask questions there is a question from NIT Varangal NIT Durga poor and Nirmah amdabad let me go over to NIT Varangal first over to you NIT Varangal good morning Pata sir I am Dr. Jede from Varangal NIT Varangal my question is usually for a number you have negatives and positives why there is unsigned character over to you the question is when we are positive and negative numbers what is the purpose of an unsigned care or an unsigned it well if we look at a basic unit in the binary number system of a 8 bit number for example typically 8 bits can represent values between 0 to 255 these are the maximum values 256 possible values and these values are possible if I consider only the positive range of numbers in order to represent characters I need all the possible symbols I can also use only 7 bits but if I need to use 8 bits I will typically call it an unsigned number when we say unsigned all that we are doing is we are shifting the representable values notice that for a 32 bit number for signed numbers we had said the range varies between minus 2 to the power 31 minus 1 to plus 2 to power 31 minus 1 ok in case the numbers are unsigned the smallest number will be 0 and the largest number will be 2 to the power 31 minus the purpose of unsigned is slightly different it is actually to be able to handle characters converted as integers and simple unsigned in has been a standard form of number representation in C fortunately in my opinion in the early part of the programming language teaching we need not emphasize the unsigned notion too much later on maybe in this workshop itself I will actually find out a couple of appropriate examples which indicate where the unsigned word is most useful let me go over to a night in Durgapur now what in instead of integer ABCD which we declare float ABCDE and we want to find the maximum of this type number so what procedure will follow because each condition cannot allow because they are little condition floating point condition cannot be compared floating point number ok the correct answer is that floating can indeed be compared there is absolutely no problem you can compare two floating point numbers even if you can declare float a b c d e and float max and read the values of a b c and d it is perfectly alright to compare if b is greater than max so it is not that the floating point numbers cannot be compared they are to be treated exactly as any other numbers and the comparison will be made correctly based on the actual value of the number which is stored inside so exactly the same logic will work and there is no problem in implementing law as a matter of fact even if you want to find out maximum between some numbers which are integer and some numbers which are floating point you can still do that because you see maximum is a quality of the range of numbers that we are actually comparing the floating point and fixed point is merely a specific representation so in a nutshell as far as your question is concerned floating point comparisons can be made absolutely in the same way as fixed point comparison and exactly the same logic will work to solve the problem man i t bhopal has a question let me go over to man i t bhopal hello I can hear you bhopal I can't see you but I can hear you please ask your question over to you what is the use of using name space std over thank you the question is why do we use the word name space std using name space std so actually the nomenclature that is used in c++ describes various name spaces the standard name space is represented by the word using name space std it is difficult to explain this notion without going to the details of c++ semantics and syntax however we can crudely take it to be equivalent to our include std i o dot h as we put in our c programs I am now looking at n i t kurukshetra and there was a question from n i t kurukshetra we said why do we use printf instead of print as I as I said earlier the input and output statements in c programming language are not native input output statements these functions are carried out by using standard library functions for input output so scanf is the standard library function for collecting formatted input and printf is the standard library function for putting out formatted output that is the reason why they are called printf and scanf there is no other particular reason they could have been named any which way but the people who define these functions in the standard library chose to define these functions in this particular fashion and that is how as a legacy we are all required to continue to use them. I have a question on the chat session from J. E. C. Kukas he says hello why don't we use bitwise operator with floating point data type the objective of floating point data is completely different it is to represent numerical values which are either very large or very small on the other hand the bitwise operators are used typically to shift bits inside a location or inside a world and you would ordinarily not be shifting bits in a floating point number because it won't achieve any purpose please remember that the floating point number consists of two distinct parts the mantissa and exponent however the compiler writers or those of us who want to actually deal with the different components of a floating point number stored either in a register and a memory location can definitely use these bitwise operators when applied independently to either mantissa or to exponent bitwise operator makes sense but as a floating point number as a complete value bitwise operators do not make sense because there is no meaningful operation that can be carried out by shifting bits arbitrarily that is the reason why bitwise operators are typically restricted for use with integer numbers. I will temporarily suspend our interaction on a view and go over to the remote centers on Ajusat my friends in Vijay Tiai please wait I will come back to you in due course of time. So which center we are going to we are going to triple IIT Allahabad okay. I have heard you very clearly and correctly let me go back to this slide can this slide be shown once again yes if you look at this slide for example I mentioned that the no decimal point is actually stored and it is assumed to be at the beginning of the mantissa so please understand that by stating that the assumption of the decimal point at the beginning of the mantissa I am actually saying that this is in the normalized form you are very right in saying in fact I have explained it here that the same value can be written as minus 0.4127e11 or as minus 41.27e9 and indeed both of these represent the same value. However when we said that no decimal point is actually stored but one is assumed at the beginning of the mantissa then this number is actually in the normalized form. So you are very right I have not used the word normalized but when you teach the students you can tell them that this is the normalized form you can also explain why I assume the decimal point or the binary point or the extra decimal point to the left of this number because if I did not and if I had say several zeros here I will be eating into the limited precision that I have so normalization is implicit in the storage because there is no point which is stored anywhere indeed I can actually make an assumption of putting that assuming that point to be anywhere it is assumed at the beginning to maximize the precision that is available to me. So thank you for raising this issue of normalization indeed we should be talking about this representation as the normalized representation. Thank you very much let us go over to the next center well or has a question over to you well or thank you very much for bringing out a very good point although we are not discussed this issue but it is very pertinent because on one hand we do talk of machine language instructions and higher level programming languages and students are always confused as to what do we mean by a program being translated or compiled into machine instructions. The example or the explanation that I typically give is more from the human domain so I describe the computer as a person speaking some other language than what I speak. So since let us say I speak English and I expect the instructions to be given in English I imagine that computer is a person who understands only German. Now if I give instructions in English the computer cannot understand so for me compiler or translator is actually another person who will read my instructions which I have written in English rewrite them by translating them in German and will give that list of instructions in German to the computer which then the computer can execute. So essentially we have to caricature a human being as a translator who actually takes instructions written by us in a particular language rewrite those instructions retaining the same meaning in the form of instructions that are understood by the computer in this case the example is that the computer is a German speaking individual and therefore he can understand only instructions given in German. I think you are very right in saying that hexadecimal and the machine language instruction format and other things are rather complex and difficult to explain to the students at this junction but what I have found invariably is that the human comparison and a human simile works very well. Of course it presumes that the translator is another human being who is reading instructions in my language and actually translating them. Later on we will tell them that this translator is not a human being but is another program which automatically translates my instructions. This I believe is probably the easiest way of explaining not the most satisfactory but adequate because students do understand the concept at this stage. Later on of course they will know more about machine language and translation and other things. Let us go over to another remote center. NIT Suryatka over to you. Thank you. I understand what you are saying. I have seen such funny questions in some university exams and also in some books. I will actually compile such funny computations and try to explain them. However I have a much simpler question to ask. Have you ever come across a real life situation in which in order to properly instruct the computer you actually have to say a plus plus a plus equal to minus minus plus plus plus minus etcetera etcetera etcetera. Are we not actually trying to tease our students or are we not trying to give them a poser in terms of a problem which is completely unreal. Of course the student should be smart enough to understand and unravel what is the meaning of use of such composite operators put one after another. But I would submit that in the initial lectures of our programming course we must concentrate on directing the students to the use of simple and correct syntax for solving problems. But I appreciate the point that you make. I will ask my colleagues to compile such questions. One suggestion is in the laboratory in the afternoon you have the compiler at your disposal and the computer. Just put in this expression on the machine and see what the machine does for different values of the concern variable and in fact if you find out something interesting please mail that solution to workshop support. I would like to share that with all other participants in the course just out of interest and curiosity. But thank you for raising this question. Let us go over to another remote center. We are going over to SV NIT Surat. The moment I am able to see them I will hand over the control. Yes I can see them now over to you Surat. So actually normally I write a C program usually I use wide name and I do not return anything. In our example we were saying that you have written into the name and we have written as we have written 0. So what is the advantage of using the int name instead of wide name that I wanted to know? Good question. Actually you can compile a program correctly without writing either int or wide. The objective of writing int and the objective of writing return 0 is to subsequently relate these two statements to the fact that even our main program is actually a function which is called by the operating system. As a result whenever our program terminates execution it return controls to the operating system. So later on tomorrow and later when we study the functions we will understand that whenever we call a function, function usually returns a value. Of course you can have wide functions which do not return a value explicitly because the return value is of no consequence. However it is customary to have a return value if for nothing else but to indicate whether the execution of that function was satisfactorily done or was there any error. It is not uncommon for example that several functions are written to give you back a value 0 if everything was 5 and to give a value minus 1, minus 2, minus 3, etcetera representing different types of errors. So our return 0 actually means that whenever control goes back to the operating system if the operating system wishes to analyze what has happened when my program was executed it would know that my program was executed correctly. Later on when we look at files and other examples and larger programs we will have occasion that our int function main will return something other than 0 but yes use of wide main is perfectly valid in which case of course since the word wide says that the function will not return any value saying return 0 or return anything will be wrong. In a nutshell then both the usages are correct wide main followed by no return and end of the program or int main followed by return of either 0 or any other value depending upon how you want the operating system to handle this. But this is more of a legacy from the old times currently most people say wide main there is another reason why I do not prefer wide main you look at a first year student as it is int main is quite confusing but wide main when the word wide is not at all understood in the context is not known sits as a very heavy weight on the student's mind. I therefore avoid the use of wide at least in the initial lectures later on when the students do their team projects they invariably learn the meaning of wide and then try to use the wide appropriate. I hope this explanation is satisfactory. Let us go over to DOE ACC Srinagar I am very happy that you are all assembled there over to you Srinagar please raise your question. My question is if we proceed long double with a variable for example we are declaring a variable as long double. Do we expect that the precision of mentisa part is increased or the exponent part is increased? Actually the precision increases on both sides slightly so the precision of mentisa also increases and the precision of exponent also increases slightly but the mentisa increases significantly. The C language standard defines what should be the minimum mentisa precision and the minimum value of exponent that must be implemented whenever you say double. However unfortunately the C programming language standard also permits individual implementations to provide larger precision than what is prescribed in the standard and that is the reason why while we can tell our students what is described in the C programming standard we cannot definitely tell them that on every computer they will have exactly that much precision for mantisa and exponent they will have to find out there are standard functions by the way which return the size of a variable of a different type and you will be able to figure out based on the documentation of an individual compiler as to how much precision for mantina and exponent you have. But the C programming standard is a elaborate document it is available on the net there is a resource on your Moodle by the way it is a textbook on C and it does discuss these issues fairly elaborately but you are very right it is not either mantisa or exponent whenever you use double the typical increase in the precision of both the components let us go over to Yes sir one more question Okay over to you Jammu Kashmir there is one more question Good morning sir in one of the program in C++ you wrote the statement using namespace STD so what actually is this namespace STD because I am extensively using this namespace concept in one of the objector in the programming language that is C hash and there in C sharp namespace is nothing but it is just the collection of classes or grouping of classes so I know my question is the concept of using namespace STD same here in C++ and if so what are the classes which are inside this STD namespace over to you sir Thank you very much there is a difference between the understanding of the notion of namespace in C sharp and C++ slightly however fundamentally both the notions are same to put it very briefly namespace there could be multiple namespaces and these are nothing but the named classes belonging to those spaces the namespace STD actually represents the collection of standard classes very much like the collection of standard library functions that we have in C at this center it is best to say that namespace STD is something similar to the inclusion of a statement in a C program which says hash include STD I O dot H as far as we are concerned the standard library functions that we use in C are the ones which become accessible through the class implementations when we say namespace STD so for the purposes of the first course this comparison and this equivalence is adequate but again as you said the standard C++ library functions describe the various namespaces their usage and their implicit classes and you will find a lot of similarity between C sharp and C++ in this respect thank you out and over