 ཀ གྷ ༃ ཭ༀ ཅ ག ༂ ຌ ༅,ཅ ༁ ༅ ཛྷ༏ ༅ ༊ ཊ ༊ ༊ ༊ ༊ ༊ altogether གྷ ་ ༀ ་ ༎ ༉ tastes ༊ ་ ༄ ༝ ༝ ༀ ༏ ༊ ༇ ༎ ༉ 是 ༊ ༊ ༌�̂ ༇ ༆ ༇ ༊ ༊ ༊ ༊ must ༇ � memories సై amplified theppers మ Kivolowitzట౔udible పై retro నొరసెర్వోగం ఺ాక్సఆల్ క gehabt�మeun స౉ఔీబ్సిక్దాధమెలెరా హిసూ఻ Mailదోక్పరతై Appro�ి సమాసేరినెలా్ న్సఌశలurity�ela depende thing are important particularly for you. these concepts are not very critical and that is because like our Dumbo the computer handles internal representation the computer is able to do internal arithmetic conversion from external representation in characters digits and so on into the internal form etcetera you don't have to generally worry about it from now onwards will concentrate on learning how to write C++ programmes but I would suggest that you do have a relook at all the lectures and slides of the previous sessions which talk about internal representation so Pratik is going to put up some additional practice problems which will be posted on the same IIT Bombay XI there is also a home page of CS101 which will contain some information of common interest to all of you so read that page later when you go home so this is you are all you have seen the Dumbo model of computing which was a very simplified abstract version of how the computer could work we had seen the notion of instructions but those notion of instructions was with respect to a caricature not to the actual computer we also understood that there is a memory in which values can be written and values can be read shop values can be copied or seen in particular we saw the notion of input and output where we said an input will destroy the old contents of that memory location whereas an output will merely mean copying of that value and giving it out to you so input is a destructive operation, output is not more important we also saw the concept of evaluating some expression and making an assignment to the left hand side name which means that the calculated value goes into that location it is almost like an input operation except that you are not getting the value from outside but you are computing it somewhere and putting it in so assignment operation is also a destructive operation and input operation also a destructive operation, destructive not in bad sense but it merely means that the old value in a location is replaced by new value and we had intuitively seen the idea of a program by looking at the procedures and so on now what we wish to do today is to understand the structure of a C++ program we will look at a program and we will look at various components of that program trying to understand the significance of those features we will then have an example program and then we will prepare a groundwork here to study more of the actual C++ program the point is from now onwards from the next week in the labs you will actually be writing, testing and running C++ program so here is a C++ program any guess on what the first red line is about? slash slash file add to numbers dot cpp well some of you already know it but those who are seeing a C++ program for the first time please remember that ordinarily just like for Dumbo for C++ we will be writing instructions which the computer is supposed to execute there are some statements in our program which will be declarative statement just like use location so and so but in addition since the computer programs are read not only by computers but also by people like us we also read those computer programs I write a program you will read it you would like to understand what is written and therefore to make matters simple clean English comments are inserted in our program now we do not want to confuse the computer with those comments because those comments are not meant for the computer C++ cannot understand what you are writing as comments so how do you distinguish? so there are two ways of distinguishing comments from rest of the instructions in your program one simple way is to start a comment with slash slash so whenever there is a line in which you put slash slash it means the entire line is ignored by the computer simply ignore it is as if the line does not exist but for us such lines are important because they tell us what the whole thing is all about in this case for example you have add underscore to underscore numbers dot cpp so this is the name of a file which contains this particular proof now a program is a sequence of directives, declarations and instructions written according to some rules and these rules stipulate how the instructions are to be written in a particular language called the computer programming language and like our natural languages programming languages also have grammars and grammatical rules and like in our natural languages if we do not use the grammar properly we may end up conveying a wrong meaning in exactly the same way the meaning of the instruction will change if we do not follow the grammar for the computer language the entire program is stored in one or more files for the time being we will assume that the program is stored in one file a file is an entity which typically resides on the disk and which contains either the text of a program or a translated program in bits and such things these are different types of files that we have so you have seen this program is a sequence of directives, declarations and instructions it is written according to some rules and it is stored in one or more files as a matter of fact when I mentioned this is a command this is a command which indicates in which file in the disk this whole program is there otherwise you will be able to locate it then the next two statements hash include less than sign io stream greater than sign looks very cryptic to us at this juncture next line says using namespace std for the time being we will merely take these as mandatory declaratives to be given in every C++ program at the very beginning without these the C++ compiler will not be able to understand or execute your program because it won't be able to compile it so these are basically compiler directives that means instructions to the compiler phase of Mr. Dhambo to understand what the program is all about later in the course we will of course know more about what hash include means or what using namespace std means but these we take them as compiler directives for the time being so these are instructions to the compiler when translating your program into the machine line and these are mandatory in fact these two lines will occur in every program practically without any exception there may be more such directives as we advance our knowledge in programming these are mandatory so while typing etc. you should never make a mistake these two lines should always be there the first two lines so include io stream what does this mean it actually means that I have a program file apart from my program instructions which I want C++ to compile and translate in order to properly translate my program instructions I want C++ to also have by his side another file which is called the io stream header file although we don't understand much about it but let's just try to analyze the name io will mean what input output obviously it has to do with something stream all understand stream like water flowing so in fact input output in C++ is handled as a stream and you can have multiple streams just like your multiple rivers so there are multiple streams which of those streams is going to be used by your program is indicated so you say using io stream header file as I just mentioned input output is handled as streams of bytes now the input stream will contain characters that you type on the keyboard a, b, d, z 1, 2 when you type 2 on your keyboard what is going inside you know you saw the dumbo taking the value 2 and taking it in is it taking the binary value of 2 no it is not when you type on the keyboard what you type is a character the ASCII code of the character will go in suppose you have to type a value 25 you will type 2, 5 return what will go in actually into the C++ compiler translated program will be ASCII code of 2, ASCII code of 5 there is some other code which is actually embedded into your io stream which will convert these two characters into an internal representation of 25 and that internal representation will be stored so input stream is converted to internal representation of computer also when you give an output instruction the internal binary representation that 2's complement floating point whatever whatever is converted into a stream of characters minus 37.142 would be the value which does not exist as we read it in a printed form not in a decimal number there is a floating point number inside the io stream there is already some program written to take this internal value convert it into printable characters and display them on your screen so you realize the importance of io stream without including io stream you will not be able to handle input output so you have seen the structure of a program here is a computer program C++ program I claim that it is correct but when I try to run it C++ does not like me and my program so what are the problems with the following program and why there are problems as usual spend next 2 or 3 minutes in writing down a proper list of problems 1 2 3 4 5 6 7 8 whatever so what are the problems with this program that you write first and then you write what should be the correction in those mistakes so you might want to write almost half a page listing the possible problems and the remaining half page writing the correct program it may take a while but do that and then let me know when you are done alright so now we do our usual thing I think some of you already started that swap your answer books discuss with your friends if you are odd number of people rotate the copies but I do not want a single individual whose notebook is being read by the same person alright I think this is enough time so how many of you jointly with your neighbors have been able to convince each other convince each other that your list was the perfect list and b together have discovered some more errors so let us look at this the first error is in which line in a comma first b so what is the mistake a variable name which is like a name that dumbo tags on a drawer there are very clear cut rules in c++ no name can start alright next error the second statement does not have any error c in so now I understand why you are getting more errors what our friend is saying is that there should be a statement c out is that right well this is a good programming practice but unfortunately this is not an error a c++ program will run even without an output message coming to you the only thing is as we saw in that diagram dumbo is waiting with a card on that side and you are waiting here and that may result but that does not mean the program is wrong it is not behaving in an ideal fashion but that is because we did not write a particular list it is desirable but not mandatory therefore it is not an error in c in greater greater a greater greater first b again the same error that I cannot use this here of course this is related to the first error so once you correct the first error hopefully you will write the correct name here next error float a so what is wrong float is correct the variable name a does not start with a number so that should also be right right please remember in float are declarative go back to dumbo in the response to our first instruction in a dumbo has meticulously labeled a into one drawer please remember dumbo is not executing any instruction at that time just labeling when it compiles the program he finds another name float a now there is a single name a float a onto another drawer also later on he will get confused if you say c in a which a this a or this a effectively it means that you cannot must not use the same name to mean two different things so therefore this declaration is incorrect if you wanted a floating point variable for use in your program you could have said float a1 a2 a3 5a right no 5a starts with 5 but what if i meant f i v e a that would be correct ok next error first b of course is wrong so let me say i correct that as a plus b why this is a very important thing but this is an assignment statement in an assignment statement there is a left hand side there is a right hand side any expression which has to be evaluated can only be written on the right hand side it cannot be written on the left hand side the left hand side of the equal to symbol is reserved for a single name which has to be the target location for collecting the value that is calculated from that expression so you cannot have an expression on this side whether it is a plus first b or whatever it is not permanent alright next error c out a plus c so dumbo will have to calculate a plus c it goes to the drawer chess a it picks out a value and then it keeps looking for c it does not exist in fact it precisely to avoid such a situation dumbo while compiling the program itself will warn you a person you have not defined all the symbols that you are using who is this c? error error error throw up alright so everybody got the errors now yeah yes his question is can i say a plus c in the out string as we shall see later there are other methods for example i could have said somewhere int d then i could have calculated d equal to a plus c and then i could have said c out in fact if you ask me i will always consider this to be a better programming practice however if you want a simple short calculation and the calculated value is not meaningful for rest of the program you can directly write the expression c out is a very powerful statement c out can compute it can evaluate the expression except that instead of assigning it to some variable it will directly put it in the output stream the output stream will convert it into your form and give it this is the assignment which calculates a value and assigns it to a location an insertion operator is deals only with the input and output streams it has nothing to do with computations and putting it in the memory ok so roughly less less or greater greater symbols are associated with the cart moving in and out an assignment statement is associated with the dumbo doing something on the workbench and putting things in the memory if i say c out a plus c for example then it does deal with the workbench dumbo will take the value of a and c go to the workbench calculate the result but instead of putting it in a memory location which would have been done by an assignment statement he will simply put the value in the cart and bring it out because you have said insert it in the output stream understood this question is suppose i did not write a plus b equal to a but i wrote a equal to a plus b will that be error or will that be correct some people say error some people say correct i will tell you it is correct and i will also tell you why although this will be discussed later please understand we have not yet discussed the assignment operation but assignment operation consists of two sub tasks one task is to evaluate the expression on the right hand side of the equal to symbol another sub task is to get the final result value and put it inside the location on the left and while doing these two sub tasks c plus plus is not confused at all so c plus plus whenever it comes across this statement it will first look at the right hand side the right hand side says a plus b so it will take the value of a take the value of b and calculate a plus b it gets a single result now and only now it will look at the left hand side the left hand side could be any variable zeta,beta,delta whatever it so happens that the left hand side variable is a it does not matter so c plus plus will take the value whatever it has calculated and put it in a in the process it will destroy the whole value of a this in fact is equivalent of changing the value of a consider a very good application of this concept when you want to say count equal to count plus one don't you do some count cricket score how the cricket there is a score 249 and some batsman takes two runs what you do existing value 249 too added to it new score 251 so you want to change a summation counter this is the best way of doing so although we digress we will probably look at it again but please understand I appreciate this question this statement is not wrong at all the variable which happens on the left hand side can occur on the right hand side expression in a number of times because these two have nothing to do with each other but the right hand side is first evaluated single result comes out that result goes into whatever you have kept on the left alright the character string representation that we saw last time you will recall that we said that individual characters are represented by an ASCII code and if you wanted a sequence of characters as a string there is a notion of an array in which there are multiple locations and you can keep characters one after another which format string however in order to make C++ understand where your string has ended you insert a special character called backslash 0 which is actually full 0s so that is all 8 bits of a byte contain 0 0 0 0 0 it's called a null character you insert it and then C++ knows that that is a string which ends here that permits you to have a long array in which we may have a 5 character string 20 character string, 35 character string wherever the string ends either you or C++ should insert a backslash 0 and BN that was the understood so here is a quiz what is the string represented by the following sequence of characters so you are given representation in some memory locations consecutive memory locations of an array and you have to write down in plain English what the string is you may write double code string double code to indicate that is how the string you will recall so here is a question now this should not take more than a minute let me do that wherever there is a blank it means the blank character what is the ASCII code for a blank character 32 very good you don't see 32 here it appears as blank but the ASCII code for that blank character is 30 so internally of course all the ASCII codes will be stored directly whenever you talk about character strings right now very quickly check with your neighbor whether both of you have got the same string alright so what is the string so the PUT those characters with the blank etc colon so give input is what I wanted to say but somewhere I inserted a backslash 0 here now this array might have 200 other locations it might have n number of other characters but C++ keeps looking at the string from one end the moment it finds backslash 0 it says string is completed it's terminated so it will ignore anything else that falls is that very clear now when you give an input string or something and the C++ stores that C++ will never make that mistake always put the backslash 0 only at the end of the string but later on you can see that by program writing program you can construct string you can actually take an array push this character there push this here take few characters and push them there when you do such jugglery then it is your responsibility to put the backslash 0 at the proper play if you make a mistake this kind of nonsense will happen clear? alright no no no the string has ended as far as C++ is concerned this part does not exist at all the backslash 0 is not an indication to go to new line or something like that that is part of the output instruction we are looking at how a string is represented internally so this whole which is not concerned with input and output at all this concern with how a string is represented inside and in that representation the problem is that we should not have suppose instead of this backslash 0 I had this as blank now what would be the string now gave blank will it say input as a single word so this will be the string alright next quiz here is a full program so what is printed by the following C++ program within one minute you will be able to do that bool true value false value these are two variable names true value equal to true false value equal to false c out true value is colon less less true value c out and false value is less less false value written simple program it assigns a value to a bool variable bool type variable and prints that value so certain things I can always say first c out statement this c out statement will produce a string which will say followed by something and the next one will say and something what will come on the first line true or false sure true means the characters t r u e false first one will say false one says true value other says false value true value is true false value is false alright so most of you have forgotten the discussion of the bool variable the boolian value internally is interpreted as follows any value which is zero is treated as false any value which is non zero is treated as true however when you ask a boolian variable value to be printed then if it is false it is always printed as zero if it is true it is always printed as one always independent of what value internally it might have assigned so this is you see very curiously c++ language does not describe what value the c++ compiler should store in the location for a boolian type variable it only says the following the value zero will always reprint false and only value zero will reprint false any non zero value will be treated as true however c++ compiler says independent of how you decide to store boolian values internally while printing you will always print two value as one and false value as zero this is only related to print that care here is a question I would like to say will these lines come in two different lines as I have written them no and this will sort of wrap around but notice that the word a and d will not be immediately following one because there is a blank here if you notice and therefore there will be one blank before this you will get a single line which will say true value is c++ blank and false value is zero also one advantage of recorded videos is that you can rewind either suprathik or phatak anytime that you want whereas in a classroom it is very difficult to replay a professor now that is an advantage of course the disadvantage is that if you ask the professor a question hopefully he or she will answer it slightly differently than what the original speech was here you will get exactly the same words same sentences again but this is an advantage in case there is a problem in comprehending some part you can actually replay that that is the reason why we are suggesting that we should have all the videos accessible to you yeah you have couple of questions related to this particular one generally the function declarations have to come at the top of the program but we are not discussing functions in details the purpose of this entire was to tell you that every object name that you use needs to be declared before its use so we will be discussing functions in much greater details here was merely a mechanism to introduce you to the notion of a function because the word int main appears right at the top in your program that's all yeah okay so our friend has the question we are saying there is a statement called return 0 so since you have just seen what a function is a function is sub-kind of a sub-task you go to that function with some values and the function does some calculation and function sends you back the return value which is typically a result typically every function returns one value then your main program can take that value go on further with calculations if it again has to perform some sub-task for which a function has been written it will go to that function with some parameters calculate the value and come back that's the normal way the function of now your main program itself is a function int main in bracket something that function must get some values must do certain tasks and must return something who is executing that program that program is being executed your program which is the function is actually being executed by another superior system called operating system so for the operating system your entire program is a function when you actually execute a c++ program you are telling operating system to execute that program or the code blocks environment you are asking it to execute them code blocks your entire program is a function it starts executing when it finishes execution it has to return something what will it return the function will not return anything arbitrarily but it will return what you have stipulated in your main program when you write return 0 actually the 0 value is returned to whosoever calls that program the operating system or something and 0 stands for no error so that is interpreted by the operating system saying your program has run well but in your programs as we shall see there will be if something do this as do this iteration variety of thing and suppose suddenly you come to a program branch where you notice that the program has not executed correctly may be because somebody has given a false value now instead of just returning if you say return 1 then the program will return a value 1 any non-zero value is interpreted by the operating system as an error status so the return statement is merely a status statement as far as the main program is concerned later on we shall see in case of functions return is actually it returns a value which is used by the calling we will discuss these in greater details when we talk about function thank you