 In this lecture, we are primarily going to discuss multidimensional arrays. How do you do array manipulations? When an array is defined to have multiple dimensions. Matrices in fact are two dimensional arrays. So, it is specifically discussed matrices and their manipulation because in all your subsequent science and engineering curricula, large amount of numerical computation that you would do would involve matrices. We will specifically look at matrix multiplication today. At the end, we will discuss another interesting notion of random number generator. This is a precursor to a simulation problem that we shall be attempting to solve in the subsequent lectures. So far, we have seen one dimensional arrays of integers. You will recall we had roll 100, marks 100 and so on. Instead of defining an array as an integer, I could define it as a float or double in which case each element can hold a float value or a double value etcetera. I can also define two dimensional arrays by merely adding one more dimension after the first square brackets. For example, int a 10 10. Now, this represents a two dimensional array which has 10 rows and 10 columns. So, 10 rows and 10 columns you will agree that resembles a matrix which is a 10 by 10 of course, when we say int each element must be an integer. It is not possible for an array to hold one integer value in one element, a floating point value in another element etcetera. All elements must be exactly of the identical type which is what is declared. An element in the array is referenced by simply writing the name of the array followed by an index for the first dimension and an index for the second dimension. So, a 4 3 as is written here will represent which roll number and column number in our conventional sense, third row and second column or fifth row and fourth column. Please remember that the array indices move from 0 to max value, 0 to max minus 1 value. So, if there are 100 elements, the last element is 99 and the first element is 0. Consequently, a reference to an index of 4 will actually mean the fifth row in a natural number calculation, first, second, third, fourth, fifth and this would mean fourth column. Arrays indeed occur naturally for example, the computer screens in front of which you sit in the labs actually consists of small points which are called picture elements or pixels. It has a number of pixels in rows and columns and you get a feeling of continuous picture because all the columns simultaneously exhibit a property of a color and intensity. So, each pixel essentially holds a value which is color or intensity value. If instead of a two-dimensional picture, if I had a mechanism to project a 3D picture or 3D image, then an element of that three-dimensional image would be called a voxel and it would be determined by three different values each representing one of the dimensions. Take for example, marks of students in different evaluations. In this course itself, we have quizzes, assignments, mid-same, and same. We have seen how roll numbers of the students could be stored in a single-dimensional integer array. But I could, for example, declare a two-dimensional array. Suppose we have roll number and marks for quizzes, assignments, mid-same and n-same. So, here is a representation that I have shown here, where on the left hand side, the roll numbers are 1501, 1502, etcetera as you already seen. So, that is let us say one array that I define as a single-dimensional array. But the subsequent marks which are written against each roll number, remember we saw this as a record for a particular student. Now, all those marks together could be stored in an array called all marks. It would have 100 rows corresponding to maximum 100 students, let us say, and it will have as many columns as we have the different evaluations. Here, I have shown four columns, quiz, assignment, mid-same and n-same. So, I could define an array called int roll 100 and another array float all marks 104. This would mean the second array is a two-dimensional array which has 100 rows and four columns. Each of the columns can hold a floating point value, but it is our interpretation that the first column shall contain the quiz marks, the second column shall contain the assignment marks, etcetera. Obviously, at the end of the course, one would like to total up all the marks obtained by every student. I could then define another array to hold those. However, it is important before that to consider how will you access different elements of this array. I have shown two elements. The first element is the fifth element of the array which is referenced as roll four. It is vital for you to remember that c plus plus starts counting the elements of array from zero and not from one. It is a very tragic thing to occur in fact because in all our computations for arrays and their indices, we always start with natural numbers 1, 2, 3, 4, 5. When Carnegie and Ritchie defined the c programming language in early seventies, they somehow started counting from zero. Now, that is alright in most circumstances, but when it comes to array indices, we have to be extremely careful because in a mathematical notation, whenever we write a i j, we automatically mean i th row and j th column. However, a i j in c plus plus would mean i plus one th row and j plus one th column. As long as we remember that and provide for it in our computation of indices, we shall be okay. So similarly, you take the larger array, all marks 100 by 4. The fifth row second column element is all marks 4, 1. Here, I have tried to show another array, one dimensional array called total marks 100. Total marks 100 resumably is supposed to store all some of all marks scored by every student. So, there are as many rows or as many elements in this as many students are there. You will notice that I have shown fifth element of total marks 100, which is referred to as total marks 4 because the index will be 4 for the fifth element. And we would like to ensure that that element contains a value, which is the sum total of all the marks in the fifth row of the all marks. Is this clear? How will you store value? How will you represent values inside at the index computation column? Of course, there is one inadequacy. Here, the marks which are shown here resumably represented in our course do not contain marks for an important component of evaluation, which is that the project. We have quizzes, assignments, mid-same, and same, but we do not have projects. If you want to include projects, you will need to add one more column. Consequently, an array appropriate for storing marks for this course students would have to have five columns and not four columns. So, here is what I have defined, row 100, end students, i, j as integer, and all marks 100 by 5 and total marks 100 as float. You are already familiar with this simple level of array manipulation, where I first read in the number of students and then for i equal to 0 to i equal to end students minus 1. That is why the condition i less than end students in increment of 1 indicated by i plus plus. For each student I have to read the five marks associated with that student. So, I start another iteration for j equal to 0 to j less than 5 j plus plus. What does iteration within iteration mean? The execution will start with i equal to 0 in the outer loop. You will come to the for statement that for loop will be executed for j equal to 0 1 2 3 4, which means that the statement C in all marks i j will be executed for all marks i 0, all marks i 1, all marks i 2, all marks i 3 and all marks i 4. Then the inner loop will end. You will go back and i will be incremented by 1 and then you will read the next student marks, the next student marks etcetera. So, whenever you have a two dimensional array, you can actually access all elements of a particular row by setting up an outer iteration which goes over the row numbers and inside that iteration for each value of i which represents a particular row number, you can have another iteration which goes over all the columns of that row. This is a standard practice in handling two dimensional arrays. So, how do you access array elements? For example, we want to solve the problem of finding and printing total marks for each student. We have read all the marks. So, we set up that iteration again for i equal to 0 i less than n students i plus plus. For each student, I start with a total of 0 marks. Just like you initialized sum to 0 before adding to that sum incrementally over any iteration. So, I start with sum equal to 0 which is total marks i equal to 0 for i th student. Then in that total marks i, I add all marks for that student for which I set up an iteration over columns varying j from 0 to less than 5. So, 0, 1, 2, 3, 4 and I say total marks i is equal to total marks i plus all marks i j. Remember the marks are now represented by an element of a two dimensional array. So, I will have to refer to that particular element as all marks i j. The value of the j comes from the inner loop, but the value of the i is static for all execution of the inner loop which is the outer loop value. So, you will agree that at the end of inner loop, I would have compiled the total marks for i th student. So, at the end of it, before going back for the next iteration of i which means before going back to look at the next student, I will print the value of total marks for that student and that is why you have the C out statement which prints the i th roll number and the total marks i. You will agree that beyond this point, there is nothing going to be added to total marks i because i is now going to change once and for all. This is a standard method of setting up a nested iteration to access all elements of a two dimensional array. In this particular case, we have access them first in the row order and for a given row, going over all the columns. So, it is the row column order. I could have easily done the other way round. I could have done column row order also if I wanted. However, that will not make much sense here particularly in terms of printing the marks of the total marks of the i th student. If I do the j th column first and the i th row later, we now turn our attention to matrix. Matrices are two dimensional array. We call arrays as compatible arrays if they are the second dimension of the first array or first matrix and the first dimension of the second matrix are same. You all agree that such matrices can be multiplied with each other. So, consequently if I have a array m by n which is a matrix A and another matrix B which is n by P, then A B which is matrix multiplication of A and B which will be the resultant matrix C will be m by P matrix. All of you agree with this? This simple stuff all of you know matrices. More specifically if C is A by B, then every element of C is defined by the summation as indicated in the formula. You all agree with this then? C i j will be equal to sigma of A i k multiplied by B k j. So, for a given value of i and j, you go over all values of k which values k will take in terms of matrix the middle dimension is n. So, k will vary from 1 to n. So, when I sum up all the matrix elements all the all the multiplication elements going over the middle dimension from 1 to that maximum value which in this case is n. I would have found out the i comma j th element of the resultant matrix. How do I implement it in a C plus plus program? Simple, I can use exactly the same formulation, but I have to remember that whenever matrix A for example is m by n, then the number of rows and columns in that matrix will be indexed by 0 to m minus 1 and 0 to m minus 1. Consequently, when I say sum over all k in conventional mathematical formulation k will vary from 1 to n which is the common dimension, but inside C plus plus I must vary k from 0 to n minus 1. Simple stuff once you understand it, it is slightly difficult to remember because that is not how we naturally index matrices, but this is how C plus plus does and we are stuck with that convention. So, the matrix multiplication program could be written by first reading all the matrices. Next, we compute C as per the formula that we just now saw namely every element of C i comma j th element of C is actually summation over k of a i k multiplied by b k j. We already know how to set up iterations, we know how to set up iteration within iteration. So, we can easily set up a nested iteration where the outer iteration let us say varies i, inner iteration varies j and inside the inner loop I could add up the, I could now have another summation of iteration, which varies k. So, I will have three nested loops, you have to watch out for indices and the input output that you do because the matrix input output in this particular case when you calculate C i j this will be calculated for a specific value of i and a specific value of j, but calculation of C i j itself will require an additional iteration. So, we will have to correctly remember where to put our output statement and so on. C i j in our program will be output as soon as we compute it, we could hold till end and have a separate double iteration to print values of C i j that is a matter of choice. So, here is the matrix multiplication algorithm, I have not written this in the conventional program include i o stream etcetera etcetera. I hope by now you all know that that is the standard stuff. So, hence forth I will try and keep showing you only the relevant portion of the algorithm. It is to be assumed that this does not make the complete program, you will have to write the appropriate paraphernalia and in fact return 0 close etcetera etcetera. Sometimes if space permits I will do that, but otherwise this shall be the model that we will use. So, when I come to this point I presume that arrays a and b have been defined and have been read in, they have already been you have done the input function. So, you got a you got b inside the computers memory. Now, I set up an iteration for i, i varies from 0 to less than m in steps of 1, inside that I set up another iteration for j, where j varies from 0 up to p minus 1 in steps of 1. Consequently, when I come inside the inner loop I have some fixed value for i and some fixed value for j and I am guaranteed that any time I come here I will come here with different value of i and different value of j and all values of i and j will be covered. So, I know that at this point if I set up another iteration to compute the corresponding multiplication value for i comma j th element of c I am safe. Look at how that summation is done the sigma over k as we talked of here I start with setting up c i j to 0 and then now I run a third iteration varies k from 0 to n minus 1 recall that n is the middle dimension. So, this in fact represents sigma k over 0 to n minus 1 in steps of 1 and what am I doing inside that iteration for every value of k I pick up a i k and multiplied by b k j and whatever is the total multiplication that I get I added to the existing value of c i j which is the element I am computing. So, notice that c i j that element the final element of the product matrix will start with value 0 which is set just outside the iteration for k and within this iteration I will go over all the values of k such that I will do that sigma correctly when I come out of the inner iteration I would have computed c i j you agree with that I would have correctly computed c i j I can therefore output that c i j element which is what is done by the word c out c i j when I come over the next brass effectively what iteration I have finished when I come out here see when I come out of here I have finished the iteration for k when I come out here I would have finished what iteration iteration for j that means all columns I would have covered consequently this c out statement would actually print the i th row first element second element third element etcetera etcetera for all the p elements 0 to p minus 1 and then I am just putting a new line here. So, that the second row of the c matrix start from the next line and so on. So, is this clear how you can do matrix multiplication what is important here is to remember that you keep in mind the multiple iterations that you execute and you keep in mind the correct index reference and index computation knowing that indices vary from 0 to something minus 1 where something is the size of that dimension. In this context we consider magic squares all of your familiar with magic squares those of you have not heard of them a magic square is an n by n square matrix where the sum of elements of every row and every column is same. Does magic square exist for n equal to 2 anybody is there a 2 by 2 matrix where you can put in integer numbers such that the sum of rows sum of columns you cannot have it right, but for 3 you can have it for 4 5 6 etcetera you can have it. Here is an example of a 3 by 3 matrix 8 1 6 3 5 7 4 9 2 you add numbers in any row or any column you get the value 15 and you get the value additionally if you add the numbers in any one of the main diagonals that will also add to 15 4 plus 5 plus 6 8 plus 5 plus 2. This is an interesting mathematical entity there has been known for very many centuries to mankind the first reference appears in a Chinese reference where these were called low shoe square they were actually attempting to solve a different complex problem and they figured out that the solution depended on the layout of certain things according to such magic numbers. A 3 by 3 magic square is also was also known to Indians because during Vedic rituals references are seen more recently in the 10th century AD there is a magic square 4 by 4 magic square in a gen temple at Khajurao. The Europeans learnt about magic squares through Arabs who actually supposedly picked up this from the east and spread mathematics everywhere does not matter what the source is magic squares are interesting. There are algorithms to create magic squares of any given n a fairly complex mathematical structure has evolved around magic squares those of you are interested can go and look up Wikipedia there is huge amount of useful information on magic squares. Why are we bringing it here? Here we will try to solve a simple problem if somebody gives you a square matrix n by n square matrix you have to determine whether the contents in that square matrix add up to a fixed value or not. So essentially what we are going to do is we are going to look at the concept of matrix manipulation accessing various elements and find out how to calculate some of rows columns and diagonals and if all of those sums turn out to be same a fixed value which incidentally is known for any n through a formula we shall see that shortly. So if the sum amounts to the same value then we know it is a magic square. So determine if a matrix is a magic square I define a matrix as square 2020 n is the n by n matrix for which I will read the value of n from outside i j and sum i j are indices which I will use to go over the rows and columns of the matrix sum is just a value which should be the value of all rows and all columns sum. Additionally I define things like r sum c sum d 1 sum d 2 sum can you guess what these would represent r sum will be sum of a row c sum will be sum of column d 1 sum is diagonal 1 and d 2 is diagonal 2 we can take any one of the diagonal as 1 and treat this back. I set sum equal to n into n square plus 1 by 2 incidentally that is the formula for the constant sum of either row or column or diagonals of any n by n magic square. So I need to ensure that every sum that I compute should be equal to this sum here is how I calculate all the sums for i equal to 0 to n minus 1 and for each value of i for j equal to 0 to n minus 1 I do the following r sum is r sum plus square i j c sum is equal to c sum plus square j i. So what am I doing here I am calculating the sum of i a throw by adding all j elements and putting them together in r sum. What am I doing here yes what does c sum mean any idea well c sum supposedly denotes column sum but what is c sum doing inside the inner iteration. So what will be the value of c sum at the end of this particular iteration first iteration let us say i is equal to 0 what will be the value of various what will be the value accumulated in c sum first column will be added. At the end when I go out of this I am calculating d 1 sum plus equal to square i i d 2 sum plus equal to what is the question mark that I have put what will this do d 1 sum plus equal to this will take the main diagonal. Please note that the sum so calculated have to be checked against this sum I am not included those if statements here if anyone of the sums that I so calculate does not tally with sum then I will have a problem actually this algorithm has major problems I am surprised that you are not indicating that problem will it be alright to calculate c sum like this is there a single r sum and single c sum how many rows and columns does a magic square have n if it is n by n magic square it has n rows and n columns note that the sum of all elements of every row and of every column must be equal to the original sum consequently I must accumulate the sums for each row and sums for each column will my declaration r sum and c sum be adequate there because it represents only one value if I go through all these iterations r sum and c sum will have only one value r sum I am initializing before the j loop starts am I am not so if I complete all the iterations what will happen at the end I will get the sum total of all elements in both r sum and c sum which will be meaningless what is the correct solution ok he is saying that you initialize r sum and c sum within the I loop and check for them it will work for r sum because I is going over rows so for every row I can initialize r sum sum over all the j elements for that row and then compare that but I will have I cannot initialize c sum that is column sum at the end of I unless I compute things differently c sum plus square j I will calculate c sum correctly for which column because inner loop is varying j for a given value of I so for first row second column first row third column first row fourth column what does it do is second column 0th row second column first row 0th column second row so c sum will calculate correctly the sums for which column I th column so r sum and c sum as written here would correctly reflect the sum of I th row and j th column I th row and j th column when I come out of this iteration for just j consequently the solution which is correct is to set r sum equal to 0 and j sum equal to 0 just before the for loop those of you are not able to figure this out should actually have to work out in this specific example I am just going to discuss how do you calculate the diagonals sum so you will agree that I am adding something to d 1 sum is the first diagonal at the end of j loop because the diagonal really does not require two indices varying independent one diagonal is 0 comma 0 1 comma 1 2 comma 2 3 comma 3 4 comma 4 so whenever I have an iteration for I I can calculate the sum of that particular diagonal by just adding up I comma I th element of the I th this is what is being done in d 1 sum please note that instead of saying d 1 sum equal to d 1 sum plus square I I I I am using d 1 sum plus equal to square I which is the increment operator the question mark is what should be d 2 sum plus equal so forget the adding up of rows and columns let us just concentrate on diagonals you agree that d 1 sum will correctly calculate the sum of all elements of the diagonal represented by 0 0 1 1 2 2 3 3 n n n minus 1 n minus 1 right so what should be d 2 sum b what should accumulate here how are terms of a row added r sum is r sum plus square I j so for I equal to 1 j equal to 0 1 and 2 for a 3 by 3 example the I j this is 1 comma j because I is equal to 1 and j will vary over 0 1 and 2 so the square 1 0 which is 3 square 1 1 which is 5 and square 1 3 which is 7 will all be added here to get you 50 this is how you calculate the sum of a row so notice this is the outer iteration which fixes a value for I which is 1 in this case and this is the inner iteration which varies j from 0 to 2 for n equal to 3 and ultimately I will get the sum 50 similarly you can work out how the column sum is calculated in exactly similar fashion but it is important to remember what our friend said that r sum and c sum must be initialized just before the j loop to get to the correct value and they must be compared with the correct sum before you go over and go to the next row or next column however we come back to d 2 sum once again d 1 sum is calculated as square I comma I if that is so from the previous slide then how are diagonal terms sum here is that statement which he said d 1 sum plus is equal to square I I this will add what square 0 0 which is 8 square 1 1 which is 5 and square 3 3 which is 2 here is the quiz so while summing the main diagonal elements square I I sum of the other diagonal d 2 sum can be incremented by the term which I start with d 2 sum also equal to 0 initially square n minus I plus 1 I square n minus I square I n minus I minus 1 and none of these this must be done inside the earlier loop for j so let's see how many people think a is the right answer 1 2 3 4 5 6 7 how many people think b is the right answer a few people there how many people think c is the right answer very large number how many people think d is the right answer oh they are one or two people who think d is the right answer here is the analysis of the answers that you have figured out actually I will try what my colleague Dr. Sahana Murthy once mentioned I do not mind spending two minutes just on this experiment those of you who gave different answers if you are sitting next to each other use two minutes to convince your neighbor how your answer is correct so just talk to your neighbor and find out whether your answer in the neighbors answer was same if it was same keep quiet if not discuss argue and convince your neighbor that your answer was right of course there is a danger if both you and your neighbor had the same answer and that answer was wrong then you are nothing to convince so please argue with your neighbor in any case even if both of you have given the same answer try to find out what was the reason for giving that answer from your neighbor and convince yourself that that argument was right now not enough discussion taking place or you have no neighbors we are not discussing with anybody come on come on come on convince him of your answer even though it is same why is it same why is it why is it correct why do you think it is correct oh this is not a joke by the way it is a very serious business we are going to have such quizzes after the mid-sem remember that time you will not be raising your hands but you will be pressing a button on a clicker device so your neighbor won't even know what answer you gave and occasionally I will ask you to do this convince each other and then again answer the quiz and I will keep the second answer as your legitimate answer for marking so there is an advantage in discussing yeah no discuss with your neighbor so raise hand for both the cases when I ask okay so at least some discussion has happened if not much so now let's go over the quiz again when I sum the main diagonal element square ii the d2 sum what should I add to it square n minus i plus 1 i so how many people think the answer is a please raise your hands two people how many people think answer is b please raise your hands one person how can there be one person because everybody is supposed to talk to a neighbor so either you convince the neighbor or neighbor convinces you there cannot be only one one person giving anyone of the answer it has to be no pairs so this time is okay but next time you keep arguing till either you get convinced or you convince the other don't fight political battles but mathematical battles how many people think the answer is c and how many people think the answer is d nobody there are some people who genuinely believe that there are two correct answers to this place let's analyze exactly what happens by looking at how exactly what exactly do these values mean so here is an analysis of the quiz problem for n equal to 4 so I have taken n equal to 4 ii when I am putting up the main diagonal sum ii will take value 00112233 that is very clear I will get the sum of all elements across the main diagonal if my answer is a then for n equal to 4 n minus i plus 1 when i is 0 will be 4 minus 0 plus 1 and i is 0 so the first element added will be 5 comma 0 is that correct no that's not correct 5 comma 0 4 comma 1 3 comma 2 2 comma 3 will be the answers that I will get will that be the right answer no in fact I am not even referencing a valid element of the array by putting that index let us look at the next one n minus i i for n equal to 4 and i equal to 0 the first element picked up by the summation will be 4 comma 0 the next one 3 comma 1 next one 2 comma 2 last one 1 comma 3 is that correct is there any element called 4 comma 0 in a 4 by 4 matrix in c plus plus no elements are start with 0 1 2 3 0 1 2 the reason this is wrong is that the first element reference is not a valid element and that is why this choice is also incorrect the next one says square i n minus i minus 1 this when you put i equal to 0 1 2 3 you will get the indices as 0 3 1 2 2 1 3 0 isn't that correctly the second diagonal that's the reason why that is the correct answer the last answer says that none of these this must be done inside the earlier for j this is meaningless because i is varying j is also varying so if you put a diagonal summation inside you will get some extremely funny results there we call it meaningless by the way whenever you write any algorithm involving matrix multiplication you should be doing this kind of exercise just to confirm looking at the extreme value whether your indices are moving correctly or they are not moving correctly before we go further to look at so is this clear how you handle matrices and matrix indexes particularly so the right expression is d 2 sum is equal to d 2 sum plus square i n minus i minus 1 this is what we had written in the quiz however notice that I can use another symmetric expression so I can say d 2 sum is equal to d 2 sum plus square of n minus i minus 1 and i this will calculate the sum of the same diagonal elements but going the other way round is that clear so either of these 2 will work for n equal to 4 this symmetric equivalent will add terms in a different sequence with index values 3 0 2 1 1 2 0 3 and this will still get you the correct sum of that diagram the reason for going so elaborately over the seemingly simple problem is to ensure that we understand the intricacies of index value computation and index value variation in c plus plus all of this came from the unfortunate fact that the first row is called 0th row first column is called 0th column and nth row is called n minus 1th row in c plus this is tragic but we have to live with it large programs have been successfully written to do matrix computations involving these kind of indexing whether it is c plus plus or java this is the standard way of doing it and it is better that you your mind gets accustomed to this almost natural as I said before going further to the random numbers I would like to just draw your attention to the fact that matrices are extremely important later on in your subsequent courses in engineering and science when you do numerical analysis in the numerical analysis you will find matrices to be extremely useful it is not merely for conventional matrix manipulation there are complex computations done in engineering calculations for example solving partial differential equations a system of partial differential equations it is not always easy to find an analytical solution many of such systems are solved through complex numerical analysis method one of the methods for example is finite element method where for the structure on which you have these elements you actually consider that a plane is consisting of large number of finite elements and just like you did the summation of areas under the curve you do something for solving those partial differential equations I am over simplifying it but suffice it to say that very major design issues in the modern engineering are solved through such complex computations done in finite element methodology where you solve systems of partial differential equations so you do structural analysis using this people who are going to do civil engineering mechanical engineering aerospace engineering even chemical engineering structural analysis heavily depends upon these kind of computations here is an example that I want to show you when you design a car and build a car you have to ensure that if the car crashes against something either another car or a wall or a tree then it will not hurt the passengers inside so consequently the cars are actually crashed after manufacturing to test how do they break obviously they will break differently every time you actually crash them but initially itself you would like to confirm that they will break in a certain pattern the car crash pattern is often analyzed using finite element techniques where you use these matrices heavily here is a picture of the model of a car crash so it is a 3D model this is not the picture of a normal car what you see here are shown as elements as they will behave when the car crashes using this kind of methodology you can simulate car crashes you can simulate car crashes without actually crashing the car of course you do not know exactly but you know to quite a good extent how the car will behave when you crash there is a case of an Indian car manufacturer where each car actually has to undergo physical crash test and it was not uncommon for such car crashes to be conducted 10 to 12 times taking over a period of year because after every crash the manufacturer has to go back redesign the faulty elements and come back with the car this particular car which was designed in India they used heavy simulation for the first time and they could complete the certification exercises with only half the number of average physical crashes that are required and they could complete that exercise in three and half months instead of one year so you can see the enormous amount of cost saving and time saving that such beautiful maths can give you and to do that beautiful maths you need to master handling matrices this incidentally was a record time in the world at that time we will very quickly discuss the notion of random numbers we are not going to use it today so the examples will follow subsequently but just to see where random numbers are you are all familiar with the randomness that happens in the world in variety of human activities for example if you play any games of chance you are familiar with card games so when you deal the cards you effectively get some card randomly out of the deck of 52 when you throw a dice in a game of snakes and ladders you will get a number 1 2 3 4 5 or 6 but that is a random number every time you throw a dice another number may come and equally probably any of the numbers can come such random numbers are called uniform random numbers because the number is randomly chosen from the same set and each number has same probability of occurrence can we use computers to simulate any game of chance yes but if we do we need to generate random numbers now anything that we generate using a computer can be done only using a specified algorithm that means we write a program and that program when executed will generate whatever sequence of numbers that you want unfortunately for a fixed algorithm if a starting point is fixed the same sequence will be generated it may appear random when you execute it first time but next time you run that algorithm it will give the random so how would you like in a snakes and ladder you always get 1 2 1 2 1 2 because that was the sequence that was generated by a particular algorithm you do not want that to happen therefore such algorithms by the way are called pseudo random number generating algorithm that means the sequence is not strictly random in the random sense but it is pseudo random because it is generated by fixed algorithm to simulate true randomness what you do is you run the same algorithm but with a different starting point and you start with a different starting point then you may get a completely different sequence if the sequence is large enough say multiple million values then obviously the sequence will not repeat and effectively you will get random number this is the crux of using some initial seed value you can generate different sequences C++ specifically provides for generation of random numbers for example if you want to represent generation of toss of a coin head or tail I need a random number generator which will keep generating randomly either 0 or 1 or you could call it 1 or 2 or you could call it 5 or 6 or 0 or 10 whatever 2 number similarly if you want to simulate the throw of a dice you need a uniform random number in the range of 1 to 6 whenever you throw a dice you get either 1 or 2 or 3 or 4 or 5 or 6 so how do you generate these C++ standard library which is called CST lib C standard library this standard library include all the computational functions that we had once mentioned additionally it includes a function called RAND RAND open bracket close bracket it does not take any parameter so every time you call a RAND function a new random number is generated and given to you obviously it is a pseudo random number and as we already know the number sequence that is generated depends upon a seed value so if you run a computer program 10 times ordinarily you will get the same sequence 10 time to avoid that every time you can set up a new seed value for which there is another function which is called SRAND so this is seed for the random number generator so initially in your program you take one seed set it through SRAND and then if you keep executing RAND again and again and again you will get sequence corresponding to that seed consequently for different program executions you can actually start with a different seed and get different sequences of random number here is an example of a program to generate random numbers so include I O stream CST lib using namespace STD etcetera int main for int equal to int I equal to 1 2 4 sorry 1 2 5 plus plus I C out RAND NL what will this generate this will generate a random number how many random numbers it will generate yes it will generate 1 2 3 4 5 random number please note that random number generated is an integer number the full integer is 2 to the power 31 minus 1 so this will generate very large numbers we can't do much with it whenever you throw a dice you can't get any one of those numbers normal so you want to restrict the random number so generated within some range this can be done by using this RAND but doing something different so I am simulating dice throw what is difference between the dice throw and the normal random number the dice must get me value between 1 and 6 so here is how do I do I generate the random number and find out modulo 6 what will modulo 6 of any integer give me 0 1 2 3 4 or 5 if I add 1 to it I get this normal dice throw so that is how I said dice is equal to this and I can output the value of dice however every time when I execute this program I want to start with a different seed and to set up the seed I read a value of seed and I say SRAND seed this function execution will set up a different seed value I have shown some executions here with seed equal to 1 2 3 another execution with seed different seed you will get different value notice that you are getting 2 6 6 1 4 this is what actually will happen if you throw it only 4 or 5 times so how do you know it is uniformly generating random number that is the distribution is uniform well you should generate thousand times 6 thousand times 10 thousand times and find out how many times 1 is generated how many times 2 is generated etcetera this is done by the next program what it does is it defines a sum array of 6 elements every time I die dice is thrown it will add the corresponding element of that sum it will increment it by 1 so I do the exactly the same thing that I was doing in the earlier program inside this loop which is run from 1 to very large number of trials it could be 10 thousand 50 thousand whatever each time I generate a random number I calculate the value of dice and I add 1 to that element I increment that sum what will happen now if I run this trial 10 thousand times the first element will contain as many times dice throughout 1 second will contain as many times dice throughout 2 of course the element number will be 0 1 2 3 4 5 and if all these numbers are roughly same then I know that the random number generator is uniform so here is the last slide which says if I run this uniformity check program for 6 lakh times so if I run it for 6 lakh times generally each number should approximately come up 1 lakh time and this is the what is indicated by the generated numbers 1 2 3 4 5 6 are thrown 99 thousand times 1 lakh 83 times 1 lakh 90 times etcetera you will agree that these numbers are roughly similar and therefore the number generated are indeed uniformly generated random numbers will stop here now and will continue looking at some problems using all this stuff in the next Thursday lecture thank you.