 So here is the problem and the way the program has been written for this. So this is the sample answer that a colleague of mine, Feroza Aibara has prepared. I'll be putting up this on the moodle soon. But I wanted before putting it up to go through this thinking process. We appreciate that this thinking process is important. If you only look at the final answer, you will understand that the answer is correct. But will you be able to create an answer? That is why this thinking process is important. So let us look at what it is doing. Now we are saying that at first the string is accepted from the user. Program requires to find the number of occurrence of a particular alphabet in the string. The character ch is accepted from the user. The first if condition checks whether the character entered by the user was an alphabet or not. Please note this program is not written during examination constraint. It is written later on. That means when you write it for your lab or when you write it at your home, you will think of additional things. For example, you are looking for a character and some sentence is given. But instead of an alphabet, somebody gives you less than sign and says please check whether it is there in the sentence. Now in any English sentence, will you find a less than sign? You will not find. So what she is doing is she is checking first whether the given character is alphabet or not. If it is not alphabet, she says I will tell you that there is no alphabet given. So I can't look for it. That is her style of programming which is okay. Now then she goes and argues if yes, then the uppercase alphabets have ASCII value from 65 to 90 and lowercase alphabets have ASCII values from 97 to 122. Now please note that she is writing this program in a comfort of her room or in the lab. You have access to ASCII code. So she is able to get that ASCII code. This is what you are supposed to do when you write programs for your project or when you write programs for your labs because you have access to other information. If it was an open book examination, I would have expected you to open the book, read the ASCII code, find out the difference and include it. If it is not an open book, then I will expect that you will make some assumption like what I said, any assumption. But as long as you are clear in your mind or I am clear as an evaluator that you have understood that small and capital letter would be different. It is good enough. But in an examination you cannot run the program. That's the problem. We are discussing here how will you answer the problem in an exam. What you are saying is that in a lab, even if I don't have ASCII table, I can actually run the program, ask it to print an integer value. Let's say for one small letter, one capital letter and I can find that out. Yes, that is correct. That is absolutely correct. So that is what if ASCII table is not given, that's what you will do. In fact, he is suggesting that you can actually write a test program and get these ASCII values. And that is the approach you will have to take in the lab or while doing the project when you are doing something more complex. Why ASCII code? You should not do it because ASCII code is such a standard thing that it is available and accessible everywhere. In fact, in less time than what you will take to write this test program on the same computer you can go to internet and ask for ASCII code table. We will get the entire code table. We could calculate the error and integer B and over there only we could calculate the error. How will you calculate that? By, I mean you can store it say one as A and one as capital A and then take the ASCII... Difference, correct. So in fact what he is saying is that I need not do all of this. I can actually calculate the correct difference between two values and get whether the calculated value is correct representation or not. He is complicating the answer but he is very right. Let me spend some time in telling you what exactly he is trying to say then we will come back and look at this answer. So let me say what he is trying to suggest. He is figured out that I do not know whether small A to Z and capital A to Z how they are different, I do not know. So I will start an experiment. I will start an experiment. This experiment says we do not know what small letter capital letter is. So you see what he has done is in a very small time he has thought of another examination problem which he says we should solve as a part of this question only. We are trying to attempt to do that. Nothing is given. How will I discover ASCII code, table and how will I find this difference is the issue. So let me define what am I trying to do. I am saying that I will declare four variables small A, small Z, capital A, capital Z and I will declare ranges small begin, small end, capital begin, capital A. I will now say small begin. Small letters begin with what? Character small A. So I will simply say small begin is from A. Where does small end ends? Capital A begins where? Sorry capital begin and capital end where? Agreed? So whatever is the ASCII code it will come and sit in these integer variables. Now I know the range of integer variables and I also know the difference. The difference is difference between any corresponding characters. So if I say now D is equal to SB minus CB and I also know that what is the range of small and big characters. Now D is SB minus CB that is small beginning and capital beginning or AC minus C. It does not matter. It is the same. Now given a character here is the question given CH find CH2. Meaning if given CH is small P find capital P. And if given CH is let us say capital T find small T. That is what we want to do. CH is given as something small. Find what is the capital? CH is given as something big. Find what is the small words? What can I do? I will find out first whether the CH is in small range or big range. If CH is in small range then the CH2 should be the capital thing difference added. If CH is in big range CH2 is in the small range reverse. So he has given you a excellent logic or excellent approach of even if ASCII table is not known to correctly solve the problem. Let us now look at the solution. So it is saying using the ASCII values it is detected whether the user entered upper or lower case alphabet. Thereafter the upper case is converted to lower case by adding integer value 32 to the ASCII value of the character and is stored in CH2 or vice versa. Now a while loop then reads the string character by character and the value is checked with CH and CH2. If the character is found the variable count is incremented and finally the count is displayed. So let us look at the way the solution is made here. These are the variables used in the program. So I have STR100 which is the string for taking input. CH stores the character to be searched. CH2 stores the lower or upper case character. I is a counter variable. I had used K in that rough draft which is okay. Count stores the count of characters CH or CH2 in this string. So let us look at the program now. Include IO stream. Include STDIO. This is it should be what? Not STDIO.H. In our case what do we say? C A STDIO. Anyway. Int main. I declare these two characters. Int i 0 count is 0. C out enter a string. Get a string. It will get you a string. Enter the alphabet that is to be searched. C in CH. So I got everything. Now see what she is doing. She is saying if CH is greater than equal to 65 and CH is less than 90. Or CH is greater than equal to 97 and CH is less than 122. She is checking whether the character is alphabet. If the character is not alphabet she will not search. Remember I mentioned this. If the given character is not alphabet I don't search. If it is a character then she comes down. Now she checks what is the range. If this is the range the character is uppercase alphabet so CH2 is CH plus 32. If the range is other one else CH2 is CH minus 32. So exactly what you are saying she is doing except that she knows now because she has access to ASCII table that the values are different exactly by 32 and what is the range of small case and what is the range of uppercase. In your case if you really want to write the program and find that out you will have to do that work which I said. Find out small a small z capital A capital Z you get the range and the same range you can check here. So instead of 65 to 90 you will check from AC to SB to AC and CB to C capital beginning to capital N small beginning to small a and appropriately adjust the D what you want. In any case you have got now the character here and the search is now very simple. Having got this CH you are saying she is not even finding out the string length although that would have been easier. We had used a loop for I equal to 0 to N minus 1. What she is doing is she is doing a loop while the ith element of string is not backslash 0 which is end of the string anyway. So she is explicitly checking for that and she says search till end of the string what I have to search if it is not backslash 0 then I have to find whether the ith character is either CH or it is CH too. If the character is found I increment the count. Otherwise I can complete this if statement but because it is while the I will not be automatically incremented so I am incrementing it here. Next character, the comment is not correct. I am not reading next character. I am comparing next character. So I compare next character and go back and do that. When I come out the character CH has appeared count times in my program. There is an else. Why there is an else? She started with an if. If the given character is not an alphabet then if the given character is an alphabet do all of this. Otherwise she says the character entered is not an alphabet. Now some of you would have written the program if you were to do the same check you would have said if the given character is not an alphabet give a message and return minus 1 or return 1. End the program. And then you could have written the rest of the problem. You got how this particular problem is solved? You can what he is suggesting let me elaborate. What he is saying is that suppose the given character is not present there at all then this program will produce an output given alphabet occurs 0 times in the sentence. Logically it is correct but that is not how you answer the English question. How many times character B appears in the sentence? Will you say it appears 0 times? Generally say it does not occur at all. So what he is saying is he would like to put an if statement. If count is greater than 0 then you say it appears so many times. Else you say given character does not appear at all. That is a better way of doing things. Have you done it like that? Or you did not get your answers here? So how many marks do you expect? You expect full marks? Somewhere else you might have made a mistake. You don't know. But you have understood now how this problem is to be solved? Let us go to the next problem. Question 5. So what does the question 5 say? Let us read that question. A value of x is given such that x is greater than 1. We need to find the sum of n terms of the following series where value of n is given. So our thinking starts now. What do we write? Value of x is given such that x is greater than 1. So let me write given x is greater than 1. Then I have to find the sum of n terms of the following series where n is also given. Write a program to read x and n. Verify that x is within given limits and find the desired sum. Suppose without reading any further I write draft. My draft program int x, n. I don't know. At this stage I do not know. It just says x should be greater than n. But it does not explicitly say x can be 2.5 or x can be 3.7. So I may make that mistake. I may assume this. How? When I continue to think, my correction will come automatically. It says find the desired sum. The program should also print the difference of last two terms added. Now that's an additional point. If I have to add n terms, then the last term, last two terms will be what? nth term and n minus 1th term. Now I have to find out, find sum of n terms, find tn minus tn minus 1. Mathematically this is what it is. The difference between nth term and n minus 1th term I have to find out. Now I read the series. What does it say? 1 plus 1 upon x plus 1 upon x square plus so on. This is the series. I am writing it deliberately in my mathematical form which I know. But now do you realize that if I have to calculate 1 upon x, and x is greater than 1, then 1 upon x will be fractional. It means that x also may be fractional. So I will correct this and I will say no, not this. But int n is alright, but I will say float x. Now I have to calculate sum, so I will write sum. This is my style of writing programs. While drafting, I write int and float, in float care in fact. And whenever I come across a variable, I put that variable there and leave it there. If another variable I require, I will put a comma and put it. And at the end I will put a semicolon. So I don't miss any variables in the declaring. So I have said int n, I have not put a semicolon yet. If I require something else I will do that. I know that I require sum, I know that I require x. Now my formula calculation is not going to be very difficult. Because if I look at the sum, I have to calculate a term and I have to add it to sum. But the term depends upon the previous term. So I need previous term and next term or previous term and current term. So I need two variables. I will call it old term and new term. And I know the formula, new term is equal to old term by what? You look at the formula that is given, the series. Old term by x, old term into old term. He is saying n term is equal to old term. He is saying by looking at the fourth term. Fourth term is 1 upon x square. The second term is 1 upon, third term is 1 upon x square. Fourth term is 1 upon x raise to 4. x raise to 4 is 1 upon x square plus into 1 upon x square. Therefore he is saying old term into old term. The old term is 1. The next term is 1 by x. So the first formula applies. Here is the other alternate interpretations. The other internet interpretation is that one, there is no unique answer to the next term in this given series. I can actually come across, come out with different formulation for the next term which will fit this present series. As it is you find that these two equations which you have given do not fit these. If you have to take one as the first term, one by x as the second term, one by x square as the third term and one by x raise to 4 as the fourth term, what is the fifth term? No, so what is the nth term? Here is a possible interpretation in a programming course. If this was a course in maths and in series, you would be expected to work out what is the nth term of the series and you would have to think mathematically. In a programming question paper, particularly when the question has only 3 or 4 or 5 marks, would it be expected that you break your brains on finding out what is the nth term? Maybe yes, maybe not, you do not know. You have classified this as a simple problem. You have allocated some 8, 10 minutes to solve this problem. Is it worthwhile for you to spend 5 minutes in thinking what should be the right term? If I am thinking like you, I will think as follows. Look, if I do not get the correct nth term mathematically, I may lose some marks. But let me make some assumptions here. Let me make some assumptions here and write that assumption. I believe as a student of programming that as long as my solution is correct as per that assumption, I should get marks for programming. The fact is that if I can figure out what the correct nth term is, then I will still be implementing an iteration. I will be still calculating nth term from previous term and I will be finding out the sum. That logic is correct. So my inability to find the nth term, I may lose maybe half mark or one mark, but for my program I will get marks. Now I have to decide whether I should struggle mathematically to find the correct nth term which will satisfy all these terms or I should spend time in programming. My logic would be that I know there is some confusion. I will take some assumption as long as I state it. Suppose I state it like this. As long as I state it and then implement it, it is correct. But if I do not write this assumption, then whatever you write in your program is wrong because I do not know what you are assuming. Do you understand the importance of comments? This is one question in which the comment is mandatory. Without that comment, people will not understand what you are trying to do. Do you agree? If you do not write a comment, you have something in mind and you implement it. Now whatever you write, whether you have implemented as per your own assumption or not I cannot check. Either your writing should be such that absolutely correct nth term is calculated which is not very easy. We have spent some time. We have not been able to come up with that. Either we do that or we make an assumption, write that assumption. But once you write an assumption, let me say this is my assumption. Now what I have to do now if this is my assumption? What is the program simply? Yes, he is suggesting define an array and define a fault loop, calculate each term, store it in every array element and once n is over, then you take the nth term and the n minus 1th term find the difference and otherwise you sum all of them. But here is a point. Then I have to sum an array and I require only two terms to find the difference. Is an array justified? I cannot keep circling old term, new term, old term, new term and get the final sum. Every time I calculate the term, I add it to the sum. So let me write both the versions. Let us write one version that he is suggesting, one version that I am suggesting. But let us see how both of them can be written in a reasonable time very quickly once this thinking is clear. So what is our thinking? n term is old term by x. That is our thinking. With this assumption, we write as per these two approaches. First we write his approach. I do not know how many terms will be there, n may be 50, 100, anything. Again I will have to write here an assumption. N is less than 1000. N is the number of terms. I am going to set up an iteration. So I will need an iteration variable. Let us say i. I want to find some. So I will find out some value of some. To begin with, I need to set up at least the first term, initial term. I will call it t. Now, to save time, I am not writing c out, etc., etc. I am just reading these two things. Is that okay? I take the first term and assign it to si and I divide t by t by x so that I get the next term. But I have to add this t to the sum. So I will say and I have to find out the difference d. So I will set the difference d as this term here and I will say d is equal to s n minus 1 minus s n minus 2. Is that okay? 0 to n minus 1 are the n terms. My nth term is in s n minus 1. n minus 1 that term is s n minus 2. I will have this here. So I have calculated it correctly. Let me write a program in a slightly different fashion because what I notice here is that the array s is not paying any big role. In fact, even if I remove si equal to t statement, my sum will still be correctly calculated. The only thing I seem to require that array is to get the nth element and n minus 1th element. Otherwise I don't require it. If I can capture an old term and current term, old term and new term or old t and t and keep calculating these and whatever are the last two values, I find the difference between t and old t. I should be okay with it. With that approach, if I write the program, I will say here again int n comma i float x sum t old t and d for difference. I will read c in x and n. I will start with sum is equal to 0. I will start with old t equal to 1. That's the first term of the series. Now I will say for i is equal to, note the way I am thinking. I don't have to handle array elements. So I don't have to run my index from 0 to something which is artificial. I have to count n. So I will say 1, 2, 3, 4, 5, 6, 7, 8, 9, up to and including n. So my loop says for i equal to 1, 2 i equal to less than equal to n. This same as for i equal to 0 and i less than n. Doesn't matter. First time when I come in, i is equal to 1. So what should I add to my sum? The first term which is old t. So I will say sum is equal to sum plus old t. Now I have to prepare for the next iteration. The next iteration will have a new term which is what? I will have to calculate t is equal to, because we have said that new term is equal to old term by x. Our assumption, if somebody had said x square, I would have said x square. If you had a more complicated phenomena for finding out the next term, suppose like Fibonacci series or Hemchandra series, it depended on two previous terms. I would have defined old term, old old term. If it depended on 10 terms, I would have defined 10 terms. I would have got that. But all that I do now here is that's it. My formula is done. Do you agree that when I come out, the sum would have correctly calculated for n terms? When nth term is added, what will be the value of t? Suppose only one term was to be added. Then sum would have become one and t would have become one upon x. When I go back, I will become two and I will come out. Suppose five terms were to be added. You will find that the terms are added one plus one by x, plus one by x square, plus one by x cube, plus one by x four, whatever. Here by the way, my assumption means that the question is wrongly printed. Somebody has missed out one upon x cube. That is what my assumption means. So I may want to write that assumption in details, that somebody has missed that one upon x cube. I am assuming that term is there, whatever. Anyway, but here I would like to suggest that I will end up calculating one extra term in the t. The term which is calculated in t will not get added because my i equal to n will be reached first. Imagine when i is equal to n. What will happen when i is equal to n? When i is equal to n, I will come in here. I will add old term to this. That old term is actually n-th term. But now t is equal to old t by x. I will calculate n plus one-th term in t. And when I come out here, if I find the difference d as equal to t minus old t, it will not be correct. It will be the difference between n plus one-th term and n-th term. Not n-th term and n minus one-th. So what could I do? So there are multiple ways of solving it. He is suggesting that you write an if statement here that if i is equal to n, then before calculating n minus one, you calculate the diff at the end. Another way is saying that you don't calculate t equal to old t here, but you calculate, you start with i equal to two. And first calculate this t and then add it to some. That means in every iteration, you calculate the n-th term. There is another problem here. I am calculating this t here, but I am calculating it from same old t. Old t is not changing. Originally old t was one. So you know what this will do? t is equal to one by x. Then again t is equal to one by x. So it doesn't matter how many times I add. I am only adding one by x plus one by x plus one by x plus one by x. If I have to add n terms like this, I don't require a program. Sigma one by x i equal to one to n is n by x. So I have to modify that old term. I am now therefore, you see, this is what will happen when I mentally execute my program and say this is how the program is going to run. So I will write it once again. Now I will say okay, I have understood what is happening. A, I am going for the next term n-th, n plus one. I don't like that. So let me readjust my formulation. I will say let us see if this logic works. Tell me whether this logic works. I start with sum equal to zero, whole t equal to one. Then I am saying sum is equal to whole t plus sum. What does that mean? I have already added the first term. When I do that, my sum is equal to one already. So I have already added first term. Now I have to keep adding second term, third term, fourth term like that and keep calculating second term, fourth term, fifth term like that. So see what I am doing. I am saying i is equal to two. I first calculate t. t is equal to whole t divided by x. So that is correct. Whole t was one. It will be one upon x. Then next time it will be one upon x square, one upon x cube. I add that. And now I set whole t equal to t. So when i is equal to two, at that time, t will become one upon x and whole t will become what? One upon x at the end. Is that correct? What will happen when I have reached nth term? I will get the nth term calculated as t. It will be added. But whole t will become t. This is... See, please remember, we all thought this was a simple problem. We have allocated how much time for this originally. I am now coming back to examination strategy. We had allocated some ten minutes for this. We have already tried three different tricks. We seem to get the sum correctly, but we are not getting the difference correctly. What he is saying is that, look, instead of getting into all this jamella, I want nth term and n minus one. I will explicitly say if i is equal to n minus one, then some value p1 is equal to this term. If i is equal to n, p2 is equal to this term. And now I will come out and say p2 minus p1 is your difference. You can do that in order to solve that problem. But I will just leave a thought to you. If instead of this, I did the following. If instead of this, I did the following. I start with some t initially. I first preserve it as whole t and then I calculate the new t for this iteration. So if I start with first term, then I preserve that first term as whole t, calculate second term. Next time when I come, I will preserve second term as whole t, calculate the third term. This will work provided I had set the initial value for that term and not for the whole term. All that I need to do is, instead of saying whole t, I will remove whole, I will say t equal to one and I will say sum is equal to t plus. See if I did this, initially sum is zero, term is one and sum is equal to what? t plus sum. That is first term added. Now for t equal to 2 to n, I am doing the following. First time preserving t as whole t. Remember t was one. So when I is two, I have t as first term. I preserve it in whole term. Now I calculate the new term as old t by x, add it. So now I got t and old t as second term and first term. When I go back, I will preserve the last calculated t as whole t and again calculate the new term for third iteration. So at the end, when I come out here and if I simply say d is equal to t minus old t, I would have the correct result. This is where you need to practice writing solutions to such problems at home for different series, different terms, how many terms you have to add, what difference to calculate, et cetera, et cetera. Run these programs on your machine once in the lab and be sure, ah, if I use this logic, it means this. If I use this logic, it means this. And that is the advantage of an open notes exam. If you have written such program and not only the programs you have print out but you have written comments in your notebook that if this has to be the calculation, this is the logic. If this has to be the calculation, this is the logic. Then in an exam time, you will not spend too much time, you know exactly what it is. In fact, it will be in your mind. Once you have written it down somewhere, once you have tried it, you will never forget it. Okay. So do you agree that this kind of logic will work? But more important thing, if I put a semicolon, the iteration will terminate there itself. You don't have to because when you say i is equal to 2, i less than equal to n, that i less than equal to n will terminate the iteration. Remember the condition is always checked at the beginning of a for loop. So initially itself, if i is not less than or equal to n, if n is 1, the iteration will terminate without doing anything at all. That is guaranteed by the for loop. So I don't have to test it. Okay. So this is one way of solving. Let us very quickly look at, so this is the problem. Let us look at the sample solution. She has solved the problem that you are mentioning by assuming n is greater than 3. And by the way, that is not an illogical assumption because anybody who was wanting to add only one term of the series would not have come to a great programmer like you to write a program. Okay. So this is a reasonable assumption. However, if you make an assumption, you should test it. Anyway. So here she has flowtext, sum, term, term old and difference in k,n, give values of x and n and n. She is reading x and n here. She is setting term old as 1. Sum as equal to term old. And she is calculating new term as 1 upon x square. According to me, she should have written here a comment. It is assumed that the successive terms are 1 upon x square times the old term. If you write that assumption in an exam situation, that is correct. Sum is equal to sum plus term. So she is adding two terms independently. First term old and next the new term. So she has now the two consecutive terms as the current term and old term. And now she is running a iteration for k equal to 3, k less than equal to n. All that she does is she takes the current term, makes this old term, calculates the new term and finds out the sum. Almost the same logic that I had indicated what you would do. Capture the old term, keep it there, calculate the new term and add it. And at the end, difference is term old minus term. Value of sum is this. You see, if the logic in your mind is already formulated, so quickly you can write the answer. The answer does not take time. But the thinking could take time. And if this thinking is done a priori before the exam, by solving similar problems and by making sure that in your mind you have clearly the logic how to set the iteration, how to set the initial term, old term, if you have to find the difference what kind of variables you'll use. You'll be able to, you don't need open notes, open book. You'll be able to write the program automatically. That is how it should be. But in an exam, some of you may be spending time, oh, this is the question. Let me see a similar question that I have solved. And if there is a small change in that question, you will tend to write that answer and you'll get a big zero or whatever. You'll lose marks there. Don't do that. First of all, tell me whether the approach that I have suggested makes sense to you. Do you agree that if you experiment like this, now what I suggest is as follows. This is one of the reasons why I have not put up the makeup test solutions on the model. Because if I did that and if I was a student like you, you know what I'll do? I'll wait, shanti se. The model answers come. I'll take a print out. Oh, this question. Okay, this question. So much gaya. And then next exam, nahi samda. Now this happens because I am not applying my mind, I am not experimenting, I am not trying and I am not timing myself. What I would now suggest is these are difficult questions I have already tagged them as difficult questions. They are not very easy questions. So you'll have to think harder. Suppose I say that the remaining four questions, six, seven, eight and nine, you require not 10 minutes, not 15 minutes, not 20 minutes, but 45 minutes per question. It's a one hour per question. Suppose, one hour per question. The point is in the examination either you will attempt or not attempt, but afterwards you will never spend one hour for one question. Because there is no need. Marks to nahi hain kahin bhi. Marks nahi hai toh kaam nahi karne. Abhi yeh mera request hai. Abhi yeh paper hai aap ke pass copy hai. Abhi ek ek ganta ek ek sowal pe lagaye. Spend one hour per question and do what? Do exactly what I said. First read, then think, then write alternatives. And don't write one program. Write two programs. Write three alternate programs or modify some portions. And then finally make a fair copy. And then hand execute that program. Hand execute karun. Find out whether it works or not. You can do all that if you spend one hour per question. You can't do that if you spend 15 minutes. In 15 minutes you can only read an answer and understand it. But you will take one hour to construct it. Here is my request to you. Spend one hour on each of these questions. Each of these questions. And understand the principle. Within this week I will be putting on the moodle not only the answers which Feroza has written but I will be putting up additional explanatory notes saying that if I think like this my solution will work like this. If I think like that my solution will work like that. You will see alternate answers. You may find that what you have thought is a third way. That is perfectly possible. But you have to make sure that the answer is correct.