 you have studied the conditional execution of statements at your home this is one problem that you would have solved on how to find and print the largest of two given numbers so if two numbers are given this is one program which could solve that problem the numbers are a and b, I declare int a and b, I want to find out the largest of the two so I declare max in which I will store the largest value I have the standard message output saying give me two numbers I input two numbers a and b and I have a simple conditional execution if a is greater than b then I allocate a to max else I allocate b to max I know at the end of this execution max will contain the larger of the two what will happen if both numbers are same his answer is anyone would go to max anyone can go to max in such a situation is the correct answer but this particular logic that is implemented so if a is greater than b it will not be true if a is equal to b therefore this statement will not be executed and the second statement will be else statement will be so this is a state implementation now you can also write this in a slightly better fashion in this way said to be an indented program where you indent the if part and the else part so that it is clearly visible to people as to what action you take during the stage when the condition is true or what action you take when the condition is false in this particular instance it does not really matter because there is a single statement that you are executing in either case but suppose you have four or five statements on this side and four or five statements on the other side then they would not be very clearly decipherable unless you put this kind of in all right now this is a problem I would like you to solve find maximum largest of three numbers a b and c the declaration is given int a,b,c,max I have taken the plain input a,b and c at the end of this program I expect that your program would have found out the right value of max putting this c out largest number is max and then return 0 incidentally you should become familiar with this style of programming where you do not end up writing each and every statement for every program for example hash include statement or using statement int main statement these are going to be standard statements in any program it is useful to know how to correctly write the spellings and semicolons etc but it is stupid to keep on doing this for every program so that is the reason why you would have skeleton of programs if not anybody else is your own program so you can copy your existing program remove the unwanted things and start writing a new program so that you save on typing type because your time is important so assume that these statements are already written incidentally in many of our exams and quizzes the questions would be of this type where the structure of a program would be given and you would be expected to write a portion in between using the declarations that have been made and producing the result that is going to be print so it is a simple extension of the previous problem except that I do not want people to do only mental calculation I want each notebook to have exactly all the statements which are required to compute max given the three numbers whose largest value should be assigned to max so write down all the statements that you would within this region come on everybody has to write a program and then I will tell you what next to be done have you discussed with each other and convinced who is right who is wrong so now raise hands where there was a disagreement amongst people that one of the programs was not correct disagreement raise your hands none all had an agreement alright now I have two answers from two of your friends they could not discuss this because I commandeered their copies before they could but I would like you to see these programs alright this is one program there is an obvious error our friend was over enthusiastic and he started writing the complete program the instructions were please do not write in ABCC in etc etc please do not write C out you write only that structure nothing else I will tell you why this is important suppose in a quiz you are asked to input a text which will be the portion like this which you have to write and suppose you write in ABCC max now the way we will automatically test your submission will be we will take our structure insert your text in between and let the compiler run that program what will happen in a,b,c max in my structure again in a,b then compiler will go crazy of course you will get zero so the first thing follow instructions very carefully alright but barring that would you agree that this program will find out max correctly no somebody saying no alright so first let us examine this program as is it says if a is greater than b and a is greater than c then max is equal to a so if a is indeed the larger of all the three so a is not equal to b a is not equal to c is very obvious if this is true a is truly the largest so a will come out to be the maximum if it is not so that means a is either equal to b or less than b or a is either less than or equal to c in such conditions you come here and you examine whether b is greater than c if b is greater than c is saying max is equal to b else is saying max is equal to c so now you can raise your objection what is the input for which this program will not work alright let us take his suggestion he says all are equal say seven seven seven now if all are equal does it matter which one of them gets assigned to does it matter which one of them gets assigned to max they are all same values is one seven better than the second seven or is the third seven so useless that it cannot be assigned they are all seven so it doesn't matter what was the other case that you were trying to point out look at what is observed suppose they are all seven if a greater than b and a greater than c is this condition true no so this will be false so I will come out to the else part right in the else part I say if b is greater than c max is equal to b else max is equal to c so max is equal to c is being executed b will not be greater than c b will be equal to c remember this else will be executed once you come here to this else he was poor fellow under the impression that if all three are equal max is not getting assigned max is not getting assigned any value at all actually I don't think our friend had really thought that deep but he is lucky his program is working fine syntax error here is another effort if a greater than b within that he is checking if a is greater than c max is equal to a else max is equal to c if a is not greater than b then else if b is greater than c max is equal to b else max is equal to c will this work anyway we have not discussed this but generally you believe that this program will also work alright now here is a point that I am trying to make almost all of you there might be some exceptions we have not seen the copies almost all of you are actually trying to follow this flow chart checking a is greater than b if this is true you are checking if b is greater than c etc etc right this is the general approach now try to solve this problem numbers are a, b, c, d, e int a, b, c, d, e semicolon no max semicolon and now find out the largest of a, b, c, d, e so first thing for 2 minutes because you will now have to say if a greater than b then if b greater than c then if c greater than d and else c less than b a whatever whatever alright the point being made is can you not without even writing the program can you not guess that this approach is even to take quite some time to write a program and this approach is therefore likely to take about half an hour of your time after which there is a 99.9% probability that the program will not work correctly now suppose I ask you something else I am writing a series of 5 numbers let us see how quickly you as human beings find out the largest what is the largest this took much less than half an hour right what did you do did you compare each number with every other number no what you were doing I will tell you what you were doing in your mind you were sort of separately remembering the largest number encountered so far as I was writing the number so you had this kind of location in your mind where the moment I wrote 5 you said 5 is the largest the moment second number came 7 oh 5 it is larger than 5 so you sort of erased it and wrote 7 years then when I wrote the third number you said 14 is larger than 7 so you wrote 14 when I wrote the fourth number 2 said ignore when I wrote the fifth number 12 ignore every time you were comparing the number that I was writing with what you had in mind so in fact the moment I finished writing 12 your answer was ready now when you can do things with such simple elegant logic why is it that you think that to write a C++ program you have to think in such a complex to eat it like this and so on and so forth that is the point the point I am making is you were all guided by the fact that there was a two-way decision making box so your premise was that somehow you have to start by comparing two numbers first now once you do that you are doomed because you have to compare whatever was the larger amongst the two with something else and then like a what would you call it like a big tree it will keep growing and if you are not convinced that you can do it in less than half an hour or more than half an hour imagine I give you 24 numbers rest of the semester you will be writing that program not worth it now how do you implement your algorithm you knew it before coming to IIT that is how you solved all such problems in life imagine now that this is max what did you do you read all the numbers you arbitrarily started with max equal to a you put that number there now you compared the second number with the current rally of max is that number larger than max then you said max equal to b is c larger than max max equal to c if d larger than max max equal to d if e larger than max max equal to e and whatever comes out is max now you see even if I give 23 number you will say there is bar like that but it won't be a thousand line program so please remember to apply your common sense with which you have been solving such problems earlier in life here is a solution you may wish to note just for your record you agree that this will solve the problem and you agree that this has no complex logic and that this can be extended for any number of numbers now let me ask you another question I am trying to extend this logic to see whether if instead of 23 numbers I had 15 numbers I can extend the same logic suppose for instance that my job is just to find out the maximum value and print it I don't care what are the individual values of a, b, c, d, e, f, g, h, etc in that case is it really necessary for me to use 7 variable locations if I want to find out the maximum of 7 given numbers so what is the minimum number of locations that I must have to find out the maximum given numbers has everybody got it no, so let me describe that method I say int n and max, n is any number I have read the first number I am remembering at max this is the trick I am reading the second number and putting it in the same location for n that is because whatever I had to do with the previous number I have already done so I can afford to forget it I am reusing that location but now to find max what is that you agree that this will find out the larger of given numbers now can I repeat this 5 times can I not repeat it 25 times please note that the advantage of saving memory locations is incidental there is a far greater advantage in writing my program in this fashion because now I know that this thing seems to be repeating again and again and again now suppose I have to find out the largest of 500 numbers if I had a mechanism to say a boss c++ do this 100 times or 500 times will you not get the maximum that instruction should look something like repeat the following 500 times fortunately c++ has such instructions and you will see them in the next week now you will notice that a very large computational problem can actually be addressed by writing program and trick in those iterative solutions will be how quickly you can identify program instructions which can be repeatedly executed you cannot repeatedly execute instructions if a greater than b another says c third one says d there is nothing to be repeated now solve this problem write a program to input 5 numbers as before a, b, c, d, e or a, a whatever you want but I would like you to write a program which reads 5 numbers a, b, c, d, e and find out the second largest number so please ignore the word output the second largest number you have to find out you give it another name call it max2 so there is a max and there is max2 you may incidentally also have to find max but what we want your program to do is find out max2 this is not a difficult question but it is not as simple as it looks only hint I will give you and start writing program please remember the declarations are given let me be very specific you already have this declaration you have read all the numbers and what I need for you to find out max2 you already got a hint you need not make obscure comparisons between various numbers just like you kept a location called max and kept stuffing it with whatever was the latest maximum you should obviously now keep two numbers one max and the other is max2 and keep running down the list comparing appropriately with the current max till you find out correct max2 he is of course not using all ABCDE numbers he is reading one number assigning it to max then he is reading another number if A is greater than max then he puts the previous value of max in max2 and assigns the new value to max so you would agree that so far he has read two numbers and they are put properly in max and max2 now he reads oh there is an else clause if A is not greater than max that means he has to assume that max is greater and the value of A must go into max2 so it is these statements which complete the initial assignment of max and max2 next he reads in another number and to this number he applies exactly the same logic if this number is greater than max then the old max becomes max2 and new number becomes max if that is not so then the new number becomes max2 is that correct? if A is greater than max then this assignment is correct but if A is greater than not greater than max does it imply that A is greater than max2 so this part is something like an initialization and after that when he reads the next number he checks if it is greater than max if so then max and A are shifted to max2 and max respectively else he checks if A is greater than max2 if so then A is put into max2 otherwise A is ignored so he is using the same logic except that he is using two locations now he has to write this how many times I will call this r1 I will call this r2 that is repeat second time and then I will call this r3 unfortunately he is outputting A I don't know why so my problem is with this last statement see out less less A whose notebook is very good effort but you see a small mistake luckily for you in the structure that I have written see out max and max2 would have occurred anyway so you don't lose out on anything but remember to do it correctly that is another effort if A greater than B max is equal to A max2 is equal to B else max is equal to B max2 is equal to A simple thing two numbers are read depending upon which one is greater one is assigned here the next one C is read if C is greater than max then max2 is made into max and max is assigned the value C else if C is less than max and C is greater than max2 then max2 is said to C can you see the difference any difference between this condition take checking and the one which we saw previously anyway but you follow this is the general logic here is another effort I have so if B is greater than max max is equal to B else max2 is equal to B if C is greater than max max is equal to C else max2 equal to C do you think this will work correctly many people feel there is a problem now the correct thing to do is to execute your program with some input values and at least two or three distinct sets and at least in one of them you would have duplicate values same values which are largest etc so you don't blindly believe that your program is correct although it might be correct here is another attempt so our friend has done what any professional would actually do he has written down a series of values 10, 20, 40, 20, 10 and actually executed the algorithm found out what would be the max and max2 at different stages therefore he believes that this algorithm will work this is the minimal that is expected when you are doing programming practice many people believe that you write a program submit it end of the matter if it compiles correctly that is fine it does not work that way and you have to learn to hand execute a program and creating a sample data is itself an art you must create multiple sets of sample data which will actually exercise different parts of the program max equal to a if b greater than max max2 equal to max max equal to b will this work a, b, c, d have been read max is assigned the value a if b is greater than max then max2 is max and max is equal to b so the values are shifted appropriate what it means is like all of us he would by this point would assign a value to max2 however if b is not greater than max then what happens so if b is not greater than max it will come here and it will assign max equal to b if b is greater than max then it will assign max to max2 and b to max but if this is not correct so this is the classic case where somebody wants to execute two instructions if a condition is correct and if you want to execute two instructions you must put them like this otherwise they will not be executed however if this condition is not correct then nothing is assigned to max2 although you already handled two numbers so there is some problem here that should be correct here is the last attempt we will look at so this is the program if a greater than b max is equal to a max2 equal to b else max is equal to b max2 equal to a is a very clean way of saying that I will examine the first two numbers and put them appropriately in max and max2 having done that he now looks at c if c is greater than max max2 is equal to max max is equal to c obviously because the max is being replaced the previous max must become max2 else he examines if c is atleast greater than max2 if so max2 is reset to c max is anyway decided notice that this is something equivalent to initial work and this is something which is equivalent to repeatable task you agree now whatever I have done for c I will do for d whatever I do for d I will do for e for f for g whatever you had an example where a friend had used only one number one variable and was doing everything using one variable that is also doable everybody understood how these things are to be done they are not complex things but you have to be very focused when you think about doing this if you have to find out the fifth largest number amongst 500 numbers given now that program would become complex if you write it like this but as I mentioned you have the possibility of forcing c++ to repeatedly execute some instructions and you would be able to find out the fifth largest by using what is known as the nested iteration we will discuss these problems later alright so this is an exercise write a program to input a 3 digit number and display the sum of its digits so please understand I made a mistake in framing these questions my questions will in future never show such thing that there is some input and you print something you actually store that input somewhere you find out different components and then you find the answer and then you print it so let me rephrase this problem so I am writing in a symbolic notation that all of you are familiar with however you know that you cannot declare memory locations in c++ with d subscript 3 d subscript 2 etc etc so you will have to give proper names I will tell you the names for this so this is the part of the program which is written already now I am being very clear in describing that you have to read a number n you have to read a number n and then from that number n you have to find individual values of d3 d2 d1 please note that these values individually must be between 0 and 9 these are digits so here is one solution by aditya he seems to have written his d1 d2 d3 in a very peculiar fashion because it appears to be q1 q2 q3 to me but doesn't matter I would also like to remind my friend aditya that when you type on a keyboard if you type q it will come like this but if you type 1 it will not come like this it will come like this only so please remember not to write super strips and subscripts because they don't appear in the machine it reads in the value n q1 is equal to n by 100 that will give you what the first digit the most significant q2 will get you what n by 10 minus 10 into n by 100 q3 is equal to n by 10 minus 100 into n by 100 minus 10 into n by 10 will you get the answers aditya people don't agree with your answer please go back and examine but he has executed something seems to have found out the correct answer for the sample data that he has taken all right here is another effort by shivam he reads in n calculates d3 as equal to n by 100 I would also like to remind aditya and others that if you are given a problem which says d1 d2 d3 have specific interpretations which one is the least significant digit which one is the most significant digit is given in the specification then your program must use the same terminology otherwise it will not be readable and understandable by others even though it might find the correct solution so here d3 is found by dividing n by 100 d2 is found by subtracting from n d3 into 100 and then dividing the whole thing by 10 and d1 is found by taking a modulo of 10 of the number 10 itself and then these 3 are added will this program work correctly anybody who says it will not work it will not work because there is no semicolon there is a full stop here now will it work will it work will it work everybody says it will work I have a minor typographical problem how will I create brackets of different sizes for example problem is d3 into 100 can you spot an error in what he has written x is not correct he has to use the however the next one n minus this now the bracket has become larger divided by 10 10 has gone down then there is a bracket which is still larger shivam I have a small question if I have given you a 10 digit number how large would be the outermost bracket so I this is of course it doesn't matter because you know that when you type you will get a bracket of exactly the same size but it is prudent and useful to remember that and practice writing it like that fine here is another effort so pranjali has solved this problem in this fashion d1 is equal to n percent 10 so n module of 10 will give her the digit d1 is that correct that is because the representation was d3 d2 d1 and d1 was the units digit so this is found correctly then she finds she reduces n she divides n by 10 so the last digit actually vanishes so now she has a 2 digit number d2 is equal to n module of 10 now you will get the last digit of that 2 digit number which is actually the 10 digit she further mutilates n and divides it by 10 so now she has a single digit number and then she finds out d3 equal to n and finds out s is equal to d1 d2 d3 I have one question pranjali why are you mutilating my n suppose after this time I wanted to do some computation using n the value of n will be the 100th digit only so please leave my n alone right but you can see it is a very clean program it will work like this it is not uncommon when you want to write a program like this where you want to successively reduce a value and you wish to use the reduced value for computations it is not uncommon to say something like int n temp etc and then say temp equal to n and do whatever you wish to do with temp because temp is your own personal variable here is the last one we will see this is from yogesh so yogesh declares these numbers inputs n calculates d3 as n by 100 he also reduces n by saying n is equal to n minus d3 into 100 calculates d2 as n by 10 calculates d1 as n minus d2 by 10 and finds out the sum this program will also work there is one good point here and that is because s is going to be output he starts with s equal to 0 he knows that d1, d3, d2 are computed from n n is going to be an input value but s is going to be computed in general it is a good idea to initialize your variables alright we will stop at this juncture