 So, welcome to this special tutorial as I had mentioned in the class, this is in preparation of the makeup quiz exam which we will be conducting tomorrow morning from 10.30 to 12.30. The portion for the quiz tomorrow is same as the portion for the original quiz. So, some of you will remember that portion was the basic concepts in programming that we had discussed. These basic concepts include the innate data types which are internal data types of C plus plus. So, what are those data types? Int, float and care, these are the three data types. There is a Boolean data type which we have not discussed, we will not bother about it right now. But, how are these numbers represented? What are the operations on these numbers? How do we define variables? How locations are get allocated and so on? And more importantly, if there are expressions involving these variables, so x plus y minus z divided by something etcetera, etcetera, how are those expressions evaluated? If there is a mixture of integer and floating point, how does the conversion happen? If there is a care variable, then can it be converted into something else? These are some of the issues that we need to understand. The assignment operation is the one which allocates a value to a variable. We will see the assignment statement very briefly. These are of course, very preliminary things. The more important thing is as we write instructions in our program, they are executed. Normally, they are executed sequentially, one after another and that is what we call sequential execution. But, we may want to conditionally execute some statements. We want to say, if something is greater than this, then do this, otherwise do this. For that, there are instructions in C plus plus which are if, if else, if else if and a special instruction called switch. So, these are the instructions which permit us to specify in our program that execute this, only if this condition is true, otherwise execute something else. Equally important is an extension of this decision making flow control, where not only we want to conditionally do this or do that, but we want to say iteratively do this. Keep on doing this again and again and again and again and again. And for example, if we want to add terms of a series. Now, if there are n terms, we are not going to write n instructions. We want to execute a simple body which will iterate n times and find out some of n terms. Such iterative control is available in C plus plus through three specific instructions. One is called the while loop, the other is called the for loop and the last one is called the do while. These are the three instructions for iterative execution. Having understood these and seen some of the examples which we will illustrate here, we then move on to a concept of handling elements of the same type. So, suppose I have 2000 integer values, I have 500 floating point values say marks and I want to do some computations using those. Then how do I store those values? For that, you have the notion of an array. Array is a collection of elements. So, we will see how arrays are defined, how they are used. In particular, the iterative structures that we would have discussed are most important in handling arrays because you do not handle arrays element by element. You handle arrays through a loop structure. More importantly, an array of characters is regarded in C plus plus as a string like a name or whatever symbols that you want to give. So, we will study how such strings are handled to store let us say names of people, to find out which character occurs here, to find out what is the composition of string, to copy one string from one array to another, to read names, etcetera, etcetera. These are the typical activities that you do with these strings. And finally, we have multidimensional arrays. We have taken one example of an image processing in the class. I may or may not discuss this multidimensional arrays depending upon how much time it takes and if I get to discuss it, then I will include a question on that in the quiz. If I do not get to discuss it, then I will not have a question. So, you need not worry from a perspective of the examination. So, we start with the discussion on basic data types. So, can you tell me what are the basic data types which C plus plus supports? Sorry, int. So, int stands for what? Integer values. How much of storage is occupied by one integer value? 4 bytes. Sometimes normally 4 bytes, but sometimes it could be either 2 or even 8. The integers that you handle on Ubuntu using C plus plus are all 4 byte integers. If you write a short int, then it may become 2 bytes and long int could be 8 bytes. And there is also a unsigned care which is 1 byte which is also treated as an int. So, strictly speaking, 1, 2 or 8 is the correct description. Ordinarily, however, it is 4 bytes. And the internal representation, again with which we do not have to do much, but we note that it is represented as a binary number and it is usually in 2's complement form internally or a signed form. And the range is from minus 2 to the power 31 to plus 2 to the power 31 minus 1. That is the range in a 4 byte integer. Then there is a float data type. It has typically fractional values and it also has large or small values. How many bytes does a float occupy? 4 bytes. Although optionally it can have 8. If you just say double, so double is 8 bytes. But we have been mostly using 4 byte floating body. Please note that these values are represented as a mantissa and exponent. So, there are a few bits which go for mantissa and a few bits which go for exponent. Typically, although it is not necessarily standard, exponent occupies 8 bits and mantissa occupies 24 bits. I am saying typically this is an internal representation. The point is that internal representation of floating point is completely different from integer. And that is why integer numbers and floating point numbers cannot be mixed or added as we would have ordinary numbers. Then there is a basic data type called CARE which handles what? One symbol, one ASCII symbol. And this is always, this is also equivalent and end because ASCII codes are between 0 to 255. So, therefore any character is treated internally by C++ as if it was an integer number. So, you can actually assign a character value to an integer variable and the equivalent ASCII code value will get assigned there. So, number it is treated almost as a number. But when you use it as a character, it will display for example as a character output as a character, etcetera. There is another data type called BOOL which we will not bother about. The Boolean, it search for Boolean, it has only two possible values, true or false and that is used to store any decision that you might have made in your program earlier. But we are not bothering with that for the time being. Double is a version of float. So, just as int has short or long. Long int means 8 bytes, short int means two bytes. Similarly, double is a version of float. Double means it is doubly wide float. So, that float is 8 bytes. 8 bytes is how many bytes? 64 bytes. So, what they do is in 64 bytes, they extend the exponent instead of 8 bytes to 12 or 16 bytes and all the remaining bits are used for mantissa. So, the precision is much larger. If you have a 4 byte float, the precision is usually 6 to 7 decimal digits only, nothing more than that. Since most of the data types are arithmetic data types, the operations will also be arithmetic type. First we consider the definition of variables. So, how do we define integer variables? For example, we may say int m, n, p, this is one way of defining. So, you write the keyword int followed by the names of the variables that you have. Each one gets allocated memory 4 bytes. Similarly, you can define floating point variables. How do I do that? What do I write? And I could have let us say k, x, y, etc. Similarly, I can define character variables. So, I can say k, a, b, whatever. Int and float variables are given 4 bytes each. Care variable is given 1 byte each. The operations on these variables are typically of the type of arithmetic operations. So, first we consider expression. So, suppose these are the definitions which I have made earlier and suppose I say p is equal to 5 plus 3 star m minus 16 star n. So, can you tell me how this expression will be evaluated? This is an expression you all understood. What will be done first? First will be which multiplication? There are two multiplications. So, you know the precedence order. What is higher precedence in arithmetic operations? Multiplication and division. Next order addition subtraction. And if there are operations at the same level, then those operations are carried out from left to right. So, ordinarily this will be done first. Some result will be obtained. Then the expression changes. It now becomes 5 plus result minus 16 star n. Now, 16 star n is higher priority. So, this will be done next. Now, the expression becomes 5 plus result 1 minus result 2. And then you add or subtract, you will get the final. What is important to note is that in the expression, when the variables are of the same type, then the result is also of the same type. For example, if I were to say m is equal to 7 by 3, what is the value of m? Why 2 and not 2 point something? Because m is integer. No, that is not the result. It is because 7 is integer and 3 is integer. There is a difference between the two. Please note that on the left hand side, what is written, it does not matter. For example, suppose I had said x is equal to 7 by 3. So, according to your logic, since x is floating point, 7 by 3 should translate into 2 point something something. That is not correct. When the expression is evaluated, c plus plus does not look at the left hand side at all of the assignment operation. Expression is first evaluated, independent of what it is assigned to. It may be assigned to anything in the world, but it does not matter. C plus plus looks only like a blinker horse, which looks only at the street. It looks at expression. So, what is this expression? x is equal to 7 by 3. Expression is 7 by 3. 7 is integer, 3 is integer. So, the division that will be done will be integer division. The result will be not 2 point something something, but just 2. And then 2 will be assigned to the left hand side. In the case of m equal to 7 by 3, it will assign m equal to 2. In the case of x equal to 7 by 3, it will assign x equal to equivalent of 2.0. That means it will convert 2 into floating point and assign, but the expression evaluation is supreme. So, again remove this from your mind that the calculations depend upon what is the left hand side type. It has nothing to do with it. Expression is the most important. Understood? Okay. If you have a mixed mode operation, for example, suppose I say y is equal to 5.26 multiplied by m divided by 7 plus 3.28 multiplied by n divided by n divided by n divided by n divided by 2.5. So, this is an expression m and n are integer y is floating point. So, can you tell me what will be done? How will this expression be evaluated? First it will do what? First it will do division m by 7. Why? How many division multiplication operators are there? Two multiplications and two divisions. When the operators are at the same level of precedence, how is the expression evaluated? From left to right. On the left, what is the expression? 5.26 into m. So, this will be evaluated first because this is left to right. Now, notice that when you evaluate this, m is integer, 5.26 is floating point. So, what will happen? m will be converted to floating point, will be multiplied by 5.26 and the result will be floating point. After that, the floating point result will be divided by 7. Now, what is to be divided by is floating point. What dividend is integer? So, what will happen? 7 will get converted first to floating point and the result will be floating point. That is how this expression will work correctly. Suppose you have written m by 7 first and then said multiply by this, then what will happen? m is integer, 7 is integer, integer division would have resulted, you would have lost the fraction, the result would be integer. Now, that when it is multiplied by 5.26, that integer will be converted to floating point but already you have lost some fraction. So, it is very very important to remember this, that when you have a mixed mode operation, then expression first decides what is to be calculated. First, what is to be calculated next? Then when it decides it has to calculate this, this meaning there are two terms and some operation division star plus minus and there are two operands. So, then it will examine whether operands are of the same type. If operands are of the same type, result will be of that. If operands are of different type, integer will be converted to floating point and then the result will be floating point and this is applied piece by piece to the entire expression. After you have calculated the value of the entire expression, then you get one result value. It could be integer or floating point, you do not know what it is. That value is now assigned to the left hand side variable. So, that is the time when the C plus plus compiler looks at y, not before that. It does not therefore matter what is y, integer, character, float does not matter. It does not look at y at all. Till it has calculated the entire expression, it has got one value now. Now, it will say okay, I got this value, I have this variable. Now, if this variable is integer, value is integer, I assign. If variable is float, value is int, I convert into floating point. Say 13, 13.0, I will say if the variable is integer and the value is float, say I say n is equal to some value which is 7.35. So, what will get assigned to n? If n is integer, integer. So, what it will do? It will not give any error. n is equal to 7.5 is a valid statement or 7.35. What it will do is, it will remove all the fractional part. It does not matter what is the fraction. 7.99, it will make it 7 only. So, n will have a value 7. So, please make sure that you understand these basic things. That whenever assignment is done, at that time, C plus plus looks at the type of the left hand side variable, not till then. Till then, it is evaluating expression. And any long expression, even if it has 35 terms, so many additions, subtractions, brackets, this, that, ultimately it will give you one value and that value will be either integer or float. Whatever is that value, that is now assigned to the left hand side. Is that clear? There is an operation called modulo. This is applied to integers only. So, for example, 173 modulo 10. So, what is the value? 173 modulo 10. Sorry, 3. So, this will result in a value 3. So, what it does essentially? It divides 173 by 10 and finds out the remainder. What will be 173 divided modulo 100? 173. What is 173 modulo 1000? So, you see this is how you can extract things. Now, this modulo operator is extremely useful if you want to determine individual digits of a number. For example, if I have a program wherein I have defined int n and let us say I want to find out the first digit and the fourth digit of a four digit number. So, suppose it is a four digit number. Four digit number will be what? Not 00001 is not a four digit number. 00001 is 1. So, 1 digit number. So, what is the smallest four digit number? 1000. What is the largest four digit number? 99. So, suppose four digit number is given and now you are asked, find the sum of first and the last digit. So, if you have given n as 5174 and you want to find out the sum of the first and the last digit, then how much is that sum? First digit is 5, last digit is 4, sum should be 9. How will you find out in a program? Now, finding out last digit is simple, modulo 10. So, you say d1 is equal to n modulo 10. We will write a command, find last digit. Now, how will you find the first digit? Which I have designated by some variable, say d2. Sorry, divide by 1000. What will happen when you divide by 1000? 5174 divided by 1000, ordinarily would have been 5.174, but since both are integer, it will be only 5. So, find first digit and now suppose you want to calculate say sum, I will define an additional variable called sum here and say sum is equal to d1 plus. So, understood? This is very simple. So, let me give you a problem. Sorry, 1000 digit and 10th digit. See, the digits in any number are counted as unit digit, 10th digit, 100th place, 1000th place, 10th thousand place, etcetera. So, suppose the number given is 1, 2, 3, 4, 5, 1000 digit is 2 and 10th digit is 4. So, sum should be 6. If the number given is 6, 7, 8, 9, 1, 2, 3, 1000 digit is 9 and 10 digit is 2. So, the sum should be 9 plus 2 equal to 11. Suppose, somebody gives you a number 3, 4, 5. So, what is the 1000 digit is 0, which is here non-existent plus 4, which is this digit is equal to. Suppose, somebody gives you only a single digit, 7, then 0, 0. Get the problem? It is important to first understand such problems. If you make a mistake in understanding, you will have a problem. Now, in the exam, sometimes I try to give an example like this, so that you will understand. But if example is not given, you should write an example. And you should write 2, 3 examples. So, this will happen. If this is so, this will happen. This is so, this will happen. And tell, write your program. So, how will you write your program? We will write our program in this space. So, we will define int n, that is required. Anything else is required? Int, let us say, d 1, d 2. And also, I will require int sum, because I want to find out the sum. Now, I start by saying, see in n. Of course, normally you will say, see out, please give the value of n, etcetera, etcetera. So, this is the number given. Now, the tens digit is very simple. How will you find? d 1 is equal to n modulo 10. But now, you do not know how many digits are there. So, how will you find the thousands digit? Why? The earlier trick will not work, n divided by thousand which digits? So, suppose you say, 1, 2, 3, 4, 5 divided by thousand. What will happen? How much? So, you see, if the number is large, dividing by thousand will not help, because you will get all the punchal law of all the earlier digits there as division by thousand. So, what should you do? Yes. So, divide by ten thousand, you will get 12. Suppose, the value was 6, 7, 8, 9, 1, 2, 3, you would have got 6, 7, 8, 9. Now, whatever is that number? That number ends with the thousand digit. So, for that new number, if you get the last digit, you got the thousand digit. So, the correct thing will be n divided by thousand modulo 10. So, it should be what? So, this is not correct. It should be n divided by 100, because you see, I am not getting the units digit. I want the tens digit. Same logic will apply. Same logic will apply if ten digit, for example, 1, 2, 3, 4, 5 is the number. Then, if I say simply n modulo 10, n modulo 100, let us say, or n divided by 100, I will not get the tens digit. n divided by 10, I will not get the tens digit. I will get all the digits before that. So, I first find out n divided by 100, and then take modulo 10. But what number will I get? n divided by 10 modulo 10. Apply this logic to 1, 2, 3, 4, 5. 1, 2, 3, 4, 5 is the number. What is n divided by 100? 1, 2, 3. The last digit is 100th place digit, not the tens place digit. What we want is 4, not 3. So, it should be n divided by 100. It should be n divided by 10. 1, 2, 3, 4, 5 divided by 10 will give you what? 1, 2, 3, 4. So, 4 is the last digit of that n divided by 10. And when you take modulo of that, you will get the last digit. Is this clear to everybody? So, these are simple tricks, but where we can make a mistake? Now, how did we correct ourselves? It was a mistake first, but somebody thought, and how did somebody think that somebody actually calculated? Okay, if this is the value 1, 2, 3, 4, 5, what will happen? If I divide by 100, what will happen? If I divide by 10, what will happen? If I take modulo 10, what will happen? You should never answer a question without trying out one such value. And whether example is given or not, it doesn't matter. You should write an example. You should write an example of a larger number, smaller number, and see whether your program works correctly. Got it? So, while it is simple, but you can make a very simple mistake, and you may get a zero. Whereas you actually understand what it is, but you are not able to correctly judge what should be right. And this mistake may happen any time. Is that clear? So, always construct a small example quickly. It will take 1 minute more, half a minute more, nothing more than that. Can I consider that you have understood expression evaluation, use of modulo, conversion from integer to floating point, floating point to integer, etcetera, etcetera. Okay, no confusion there. Particularly, two things to remember. Integer divided by integer gets you an integer, and you lose fractional path. But floating point divided by integer, integer is converted to floating. Similarly, in assignment, floating point assigned to a floating point value, no problem. Floating point assigned to an integer value, no problem. X is equal to 10. 10 will become 10.0. But integer assigned to a floating point value, you lose fraction. N is equal to 17.53. N will remain only 70 if the N is defined as integer. Fine? So, no issue on that.