 Good morning everybody. We will continue our discussion on arrays and today we will look at how matrices are handled in C programming language. Specifically, it is multi-dimensional arrays that we use. Multi-dimensional arrays is the nomenclature used in the C programming language. A specific subset of this, namely two-dimensional arrays, will be equivalent of our matrices. Since a large number of computational problems involve use of matrices, I will discuss matrix computations with the example of Gauss-Siedel reduction. First some preliminaries. To declare multi-dimensional arrays in C, we note that our arrays can have more than one dimension. As a matter of fact, there is no strict limit on the number of dimensions that you can have. But if you have an array with, say, ten dimensions, it is not clear where you would require it, but you could have it. But the product of the sizes of all the dimensions will be the amount of memory that will have to be allocated. So, that is something that you would like to keep in mind. A typical declaration would be of this form. So, when I say A 50 comma 40, it effectively allocates memory for what we may conventionally call a matrix. The two-dimensional array will have 50 rows and 40 columns. Each element of this two-dimensional array will be accessed by a reference, which now requires two index expressions to be specified. So, in a single-dimensional array, we said, say, roll i or roll j. For this two-dimensional array, we will have to say a i j. For example, we may have an assignment statement which says a i j equal to 37, in which case what the computer will do is it will calculate this index expression. In this example, it is simple i, but it could be i star 3 minus 4, whatever, as we saw yesterday. This index expression is evaluated and the final value becomes the element index for the row number. Similarly, the second index is evaluated and the final value becomes the index for the column number. And then, once a row number column number identifies a specific element, that gets assigned the value 37. So, please note that the name A does represent 50 into 40, that is 2000 elements. However, a i j or any reference of this kind will refer to exactly one element, exactly one element, no more, no less. And that element will be identified based on the values of the row index and column index. We call it row index and column index because it is a two-dimensional array. If it was indeed a five-dimensional array, for example, we would have said the value of the expression for index 1, value of the expression for index 2, value of the expression for index 3, etcetera. Whatever will be the number of indices, ultimately such a reference will refer to exactly one location. We note the usual constraints which are same as those which apply for a single dimension, namely that each index, whether it is a row index or column index, must be within the limits prescribed by the size declaration. So, in this case, the value of i can range from 0 to 49 and the value of j can range from 0 to 39. And we note that all rules for index expression apply to index for each dimension. With this, we consider an important computational problem that arises in many applications which involves the use of matrix. We will specifically talk about representing a system of equations in multiple variables. This material is based on information provided in numerical recipes for C, C plus plus etcetera. I will show you the reference at a later stage. As I mentioned earlier, one of the best books I have seen which describes algorithms in details along with a very detailed analysis of how those algorithms have been arrived at. So, we continue our discussion with a simple example. We consider two equations in two variables, 2 y plus 4 z equal to 8. We call it equation 1. 4 y plus 3 z equal to 1. We call it equation 2. Actually, all of you will recognize that these are very elementary set of two equations and generally all our students who come to engineering college would have solved these kind of equations, simultaneous equations very many times. Here the objective is to demonstrate the use of matrices and the method of Gauss elimination by working out that particular algorithm on this simple system of equations first and then generalizing it for a system of n simultaneous equations. We note to begin with that this system of equations can be represented as a coefficient matrix 2, 4, 4, 3, a matrix of the variables y and z and the right hand matrix which consists of 8 n val. So, we note that if there are two unknown variables and we have an equation system of two equations in two unknown variables, then we get a 2 by 2 coefficient matrix. Please note that this matrix representation actually denotes 2 multiplied by y plus 4 multiplied by z equal to 8. Essentially, the dot product of the first row and the only column of unknown variables equal to the corresponding element of the right hand side forms equation 1. In exactly the same fashion, 4 multiplied by y plus 3 multiplied by z equal to 1 formulates the second equation. While our students may know matrices and this representation, we cannot make that assumption. So, it will be useful perhaps to extend this by describing what is a dot product. Even if the term dot product is not known, they will know how matrices are multiplied. So, 2 into y plus 4 into z equal to 8 is what amounts to the first equation should be clarified. Having achieved this, we proceed further to look at how this system of equation can be solved. In particular, we describe the Gaussian elimination. This method we observe depends upon certain standard facts. There are actually two facts which we will use which the Gaussian elimination uses. The first fact, any system of equations is not affected if any one equation is multiplied by a constant. That means the equation continues to represent the same factual position. To illustrate, we will say, look at this equation 2 y plus 4 z equal to 8. We observe that it is same as 1 into y plus 2 into z plus 4 which we obtain by multiplying the equation by 0.5. If we did that, we can now say that we have two equations 1 y plus 2 z equal to 4 which is the one which is derived from this first equation by multiplying it by 0.5. We call it equation 1 dash and we have retained the second equation 4 y plus 3 z equal to 1. This is equation 2. At this time, some students may naturally ask, why are we multiplying by 0.5? Why not by 0.8? Why not by 7.3? Why not by any other number? While it will become clear why we are doing this, but we can very minutely, very, very, very subtly suggest that by multiplying this equation by 0.5, what we seem to have achieved is that the coefficient of the first variable has become 1 now. This one has significance as we shall see later. We then continue with equation 1 dash which is 1 y plus 2 z equal to 4 and 4 y plus 3 z equal to 1 being equation 2. Representing these equations in the form of matrices, we get this representation. So the same equations, we now say 1 2 4 3 y z equal to 4 y. Notice again that people if they are not understood the notion of dot product, we can remind them again that this matrix representation really means that the first row is multiplied by first column and is equal to first element of the right-hand side. Similarly, the second row multiplied by first column, that summation is equal to the second element. So 1 into y plus 2 into z is equal to 4 is our first equation and likewise the second equation. We note in passing that the original coefficient matrix had we represented the coefficients in a matrix form would be like this. Why do I repeat this here? This is to show a comparison between what was the original coefficient matrix that we have and what is the modified coefficient matrix that we have now derived. We can again subtly emphasize to our students that ultimately when we apply the Gaussian elimination method to a larger set of equations, we would essentially be modifying the coefficient matrix and because of that modification we will also be modifying the right-hand side matrix. y z of course represent unknown which is what we need to determine. Clearly the objective is to find out what is the value of y and what is the value of z which together satisfy the system of equations. So we got this equation 1, equation 2 originally in matrix form and this is equation 1 dash and equation 2 again in matrix form. We now notice the second important fact that the Gaussian elimination technique uses namely if an equation is replaced by a linear combination of itself and any other row, the system of equations remains the same. The word linear combination may not be very easily understood by our students. So we say that we can add or subtract from a row a multiple of any other row. So that is what is meant by a linear combination of itself and any other row. In general actually linear combination could mean say k 1 into 1 equation plus k 2 into another equation. The form that we take is that we replace the second equation by subtracting from it 4 times the first equation. Again at this stage the number 4 might appear to be an arbitrary number but we will soon proceed to tell our students how we derive this number 4. In short if these were our equations equation 1 dash 1 y plus 2 z equal to 4 or now we can speak in matrix terms 1 2 4 3 was our matrix and 4 1 was the right hand side. I multiply the first equation by 4 and subtract from equation 2 the resultant combination of equation 1 dash multiplied by 4 and replace the second equation by that value. So what do we get? The first equation stands as it is because we are not doing anything with it 1 y plus 2 z equal to 4 remains as it is. What happens to the second equation? It was 4 y plus 3 z equal to 1. Now it becomes 0 y minus 5 z equal to minus 15. Now people can easily see what was the reason for the choice of multiplying the first equation by 4. In fact we will remind our students that when they apply these principles normally while solving a simultaneous set of equations they would like to eliminate one of the variables. Here we are talking about eliminating the variable y. Since y has a coefficient of 4 if I multiply the first equation by 4 and then I subtract that equation from this I will get 0 1. So when I multiply this equation by 4 this term becomes 4 into 2 8 z. When I subtract 8 z from 3 z I will get minus 5 z. Similarly when I multiply this by 4 I will get 4 into 4 16 and when I subtract it from 1 I will get minus 50. So it is a simple linear combination evaluation. We are using this fact which is a general fact and we are using this fact to reduce the coefficient of the first variable in the coefficient matrix to 0. In terms of matrices this was our matrix. When I multiply equation 1 by 4 basically I multiply the first row by 4. When I replace the second row by subtracting 4 times the first row from itself I will get the second row as 0 and minus 5 which is nothing but the coefficients of the modified equation as we see on the left hand side. And of course the right hand side also stands modified. The first element remains same as 4 because we have done nothing to it. Since we have replaced the second equation the second value becomes minus 15 which is nothing but the right hand side of this equation. You might find this elaborate explanation not worthwhile for the community of teachers who have assembled here. But again I will remind you the purpose of this explanation is to indicate that perhaps such details might be essential to convey to our students not all of whom would understand linear systems of equations really equally well. Anyway now is the very simple step. Everybody will know now that how to solve this equation. I have 1 y plus 2 z equal to 4 0 y minus 5 z equal to minus 15. From this two equations we further multiply equation 2 by minus 5. Now this is subtle. We had this 0 y minus 5 z equal to minus 15. Ordinarily a student will know that this equation is nothing but minus 5 z equal to minus 15 therefore z is equal to 3. So he will just divide minus 15 by 5 which is the coefficient of z because this term does not come. What a student will do naturally? We want to indicate how to do it formally in the context of a matrix representation. Notice that we have got 1 here on this diagonal by using the previous operation. We now tell our students that we want to get 1 here because if the coefficient of z is 1 then whatever is the right hand side is actually the value of z. So in fact what they would intuitively do for finding out the value of z namely divide this by the coefficient minus 5. We are actually trying to show that this is done by using the same fact. So by doing this we will get 1 y plus 2 z equal to 4 and 0 y plus 1 z equal to 3. This might appear trivial even to our students but the idea is to tell them that by doing this what I do is I get a coefficient matrix of this form. So I have once on the diagonal zeros on the lower triangular portion and non-zero values or whatever values I get in the upper triangular portion. It is a 2 by 2 matrix. So the notion of a triangular upper triangular lower triangular may not be very obvious here but essentially we say that we wish to get a matrix from the coefficients which have once on the diagonal and zeros below the diagonal. Why? Because by putting these equations in this form which now we call equation 1 dash and equation 2 dash starting with the last equation we can use a back substitution and get the solution for all variables. The last equation itself does not require any substitution. We directly get z equal to 3. Why? Because this coefficient equation represents 0 into y plus 1 into z equal to 3. 0 into y plus 1 into z equal to 3 that is this equation which means z is equal to 3. So we say we have identified one solution. Having obtained this solution z we substitute the value of z in the previous equation. The previous equation was 1 y plus 2 z equal to 4. When we put the value of z which was 3 here we get 1 y plus 2 into 3 equal to 4 which gives us y plus 6 equal to 4 and which therefore gives us y equal to minus 2. If we have more equations we can tell our students. We can keep on doing this. This process is called back substitution. So we keep finding out values of the variables at the bottom. Move upwards by substituting the values of those variables whose results we have found and finally we will get values of all the variables. This in short is the method of Gauss elimination. So we note that the essence of the method is to reduce the coefficient matrix to an upper diagonal matrix with all elements in the diagonal as 1 and then we use back substitution. We note that the process is susceptible to round off errors. Why? Because in practice the coefficients will not be nice ones as 1, 5, 3, etc. In real life for example if you are simulating chemical processes or as I said you are doing some finite element analysis. We are not even talking about differential or partial differential equation sets but there are many, many situations where you get a large number of simultaneous equations to be solved whose coefficients are arbitrary real numbers. And if there are arbitrary real numbers we know that when we represent such real numbers by floating point numbers inside we have the problem of limited precision. In particular we recall that if a large value is added to or subtracted from a very small value the result may not be significantly different from the original value that is what we say is the impact of the limited precision we can have. We just noted at this stage in the first course after all this is not a course on solution of linear equation. This is a course on algorithms programming and so on. But we are taking this example to show how matrix manipulation is done in C programming. If our students are interested and if we are so mathematically inclined we might indicate that there are other variations such as Gauss Jordan elimination, pivoting, LUD composition. Many of you would recall these to be the standard techniques which are used in several cases. We also note a useful reference. This is the reference that I mentioned. Numerical recipes in C. Actually this original book was Numerical Recipes in FOTA. Then when C language came about they introduced the same numerical recipes in C. Subsequently when C++ came they wrote the book, rewrote the book in numerical recipes in C++. There is an interesting dilemma which these people also face. This is what was raised by some of our participants also. Why does C indexing start with 0 when we naturally index our matrices or our way of looking at 1, 2, 3, 4, 5, 6, 7. Interestingly the same problem afflicted the authors of this book who had written it originally in FORTRAN and as many of you will know FORTRAN arrays are referenced, their elements are referenced by referring to them as 1, 2, 3, 4, 5 up to the side. So what they did when they wrote the book in numerical recipes in C is that they wrote a whole lot of backend software called they wrote actually they wrote function library such that in the actual algorithms they could use indexes 1 to n. Consequently while this book implements all the algorithms rigorously it is slightly odd. Why? Because if we want to ever use any other functions that we write in terms of the arrays that they deploy it becomes extremely difficult because our arrays we will refer to between 0, 2 whatever n whereas they will refer to 0 to n minus 1 whereas they will be referring to 1 to n. They have corrected this in the C plus plus series. By and large is one of the best books by the way some of you might want to just have this book because of the excellent treatment of variety of numerical recipes problems that the book offers. These are the authors and the book is extremely popular amongst numerical scientists. We now proceed to look at the generalization of what we have just seen through an example. This then is the general representation. We say that a system of linear equations in n variables can be represented by a11, a12, a13, a1n, x1, x2, x3, xn equal to b1. a21 into x1, a22 into x2, a23 into x3, etcetera, a2n into xn is equal to b2. In short, an n by n coefficient matrix and an n valued right hand side represents a system of n linear equations. We now note that while we as human beings might be accustomed to this notation of referring to coefficients of as a11, a12, a1n, etcetera, since the C programming language uses for its matrix indexes the notation from 0 to n minus 1. It is worthwhile for us to treat this system of equation as if it is represented like this. a00, a01, a02, etcetera up to a0, n minus 1, a10, a11, a12, etcetera up to a1, n minus 1 and so on and our variables instead of calling them as x1, x2, x3, we call them as x0, x1, x2. Why? Because clearly then the conventional matrix both one dimensional and two dimensional of C programming language using the index expression rules of C programming language can map one to one on to this system of simultaneous equations and when we do it at this stage it is not very hard. Particularly to programming student who are already accustomed to array indexes being between 0 to n we go to the previous slide show them that this is what we you will find in mathematics books or this is what you might have studied in your school algebra or this is what you will be studying in your engineering or science algebra. However, since you know how C programming treats arrays for our purposes we shall treat this equation to be like this. There is absolutely no change except for the nomenclature. I have found that most students immediately understand this particularly you show both the things side by side. Problem arises if we start only with this kind of representation and never compare it with the conventional representation. So, this is one rule I have learnt in teaching whenever we are introducing anything new as far as possible related to something that students already know and then indicate the variation. Now, we know that the Gaussian elimination technique essentially reduces the coefficient matrix to an upper triangular form. So, the C style matrix will have 11111111 on this diagonal will have zeros below notice that this is now an upper triangular matrix because the coefficients will be a 0 1 a 0 2 etcetera up to a 0 n minus 1 a 1 2 a 1 3 etcetera up to a 1 n minus 1 and so on and of course these are the variables with the appropriate right hand side values. Once we reduce this we can tell students how I can do simple matrix operation the last value x n will be simply equal to b n I can use these to back substitute in the previous equation and so on. We shall now look at the computational implementation of this particular problem we note in general the representation in C style will be that I shall have a 2 dimensional matrix a I will have a 1 dimensional array x and I will have a 1 dimensional array b. The 2 dimensional matrix a will contain the coefficients which originally would be all arbitrary values filled up in the n by n matrix starting from 0 to n minus 1 for rows and 0 to n minus 1 for columns the x matrix is trivial we just define it is a place holder actually it will not contain anything for most of the operations except in the end when using back substitution we are going to find out the value of the variables. So, these x matrix will represent actually our results and it will get filled up as we know because the back substitution not from x 0 x 1 but first we will calculate x n then x n minus 1 x n minus 2 and so on. We note in particular that if we recall our gauss elimination technique that we have seen then after the steps which will lead to modification of the left hand side coefficient matrix and of course the corresponding modifications on the right hand side we will get the coefficient matrix in the upper triangular form and then we will have the following system which using back substitution we can solve for x what will that be x 0 plus a 0 1 into x 1 plus a 0 2 into x 2 etcetera a 0 n minus 1 into x n minus 1 equal to b 0 x 1 plus whatever whatever lastly plus a 1 n minus 1 into x n minus 1 is equal to b 1. Notice that because the coefficient matrix has 1 on the diagonals the coefficients of x 0 x 1 x 2 etcetera will be 1 and now if we see very easily the back substitution means x n minus 1 is directly equal to b n minus 1. We substitute this value back and we get x n minus 2, substitute it back we get x n minus 3 etcetera etcetera, substitute it back we get x 1, substitute all those values back we get x 0. A small question is this elaborate explanation necessary. My answer is yes I have tried shorter explanations some of the smarter students in the class understand that very easily. Those who have been playing with simultaneous equations for a long time in their school days or have picked it up in their first year engineering in the mass portion they can understand it very quickly. But there are a large number of people who are significantly benefited by some elaborate discussion of the Gauss-Elin. So, the question I am often asked by my colleagues is this a programming course or is this a mathematics course. Thankfully our course is named computer programming and utilization as I told you we do not name the language that we use although we use a combination of C C plus plus for teaching programming principles. But traditionally we take up an application explain what that application is about explain the underlying algorithm and then work out how the C programming implementation will be which is what we shall now proceed to achieve. We further illustrate our computations in terms of C matrices consider our example equations in two variables 2 y plus 4 z equal to 20 y plus 3 z equal to 11. We represent y by x 0 and z by x 1 then this will be represented as this. So, a 0 0 into x 0 etcetera where a 0 0 is 2 a 0 1 is 4 a 1 0 is 4 a 1 1 is 3 they are namely the coefficients here now we write the C program once again I will remind you the only reason why I am calling this Gauss dot C P P is because I am going to use C in and C out other than C in and C out there is no feature in our programs which does not match one to one with the features of C. So, we are essentially writing C programs but including input output statements of C plus plus for the reasons that I had mentioned earlier. So, for that purpose I of course have to say include I O stream using namespaces to the et cetera. I declare all my variables in arrays I J K N R common sense indexing variables that I will use L is the size of the system of equation I define mat A as matrix A, mat B as matrix B and x as the variable matrix. I describe as divisor factor and sum as float these are the computational variables or place holders that I will require during my computations. I have the usual input portion which reads all the values I read a matrix A first since I have to it is an n by n matrix I start by reading n and then I read the coefficient matrix in row order. So, for first row I equal to 0 I read all the columns then for row I equal to 1 that is second row I read all the columns et cetera I do exactly the same thing in matrix B matrix B is a single dimensional array. So, there is only one column for I equal to 0 to n minus 1 I read all the elements of matrix B having obtained the input I now proceed to do the Gauss elimination. Since we have discussed an example of a two by two equation system it is not very difficult for our students to understand these things very quickly what I do divide each row by the coefficients on the diagonal. People will recall that the purpose is to get 1 on the diagonal and therefore multiplication for any system when I proceed any row. So, I am starting with I equal to 0. So, I start with that row and in that row I make the coefficient of the diagonal 1. So, I calculate the divisor mat A I I which is the diagonal on the Ith row. Please note I am starting with the 0th row and please also note that before I complete this for any row I would have got by the elimination 0s in the lower triangular portion. So, for this row I say divisor is mat A I I and I simply say mat A I I equal to 1.0 I am forcing it to 1 and now recalculate all coefficients in that row. So, for j equal to I plus 1 j less than n j plus plus I recalculate the I comma jth coefficient of mat A by dividing itself by divisor. Divisor is the diagonal. Why I do not do that for even I I please note that mat I I is desired to be 1. I am dividing each row by the coefficients on the diagonal in order to get that as 1. There is absolutely therefore no harm in forcing that to be exact one or as close to one as possible because we know that internally the decimal fractional numbers and binary fractional numbers in which form the computer stores or number do not have 1 to 1 match and that is because 1 by 10 cannot be translated into an exact binary fraction. That is a matter which we have not yet discussed. I would suggest that the teachers amongst groups deliberate on these issue to discover what is the implication. Anyway, now normalize the corresponding right hand side element because I have to divide the right hand side element also by same I get this. So, for so good all that I have achieved is I have taken each row and converted the element on the diagonal to 1. Now replace subsequent rows by subtracting the appropriate portion of the ith equation from it. So, I am considering ith equation. Initially I is 0. So, for if I plus 1 is less than n because I should have a subsequent row. If I am already at the last row there is nothing else to be modified. So, I just check whether there exists I plus 1. That means I plus 1 should be less than n. At most it should be equal to n minus. Then for a value of k varying from I plus 1 to less than n I do the following. I calculate the factor which is mat A k, i and I set mat k, i to 0 because that is the purpose of the elimination. Notice what we are doing. We are now reducing the matrix row by row into an upper triangular form. So, just as I force the diagonal element to 1 I force the k comma of I element to 0 because that is what I am going to achieve after the substitution. And what do I do after the substitution after the subtraction of a linear combination and so on which is what we attempt. So, for j equal to i plus 1 to n minus 1. That means for all the elements in the I plus 1th row I calculate mat A kj equal to mat A kj minus factor times mat A ij. So, mat A ij is the deciding row. It is multiple that is factor. I am subtracting from the k a through corresponding jth element. The multiple of each row when subtracted from k a through it will not be the same factor. So, notice the factor is calculated separately for k a through. So, each k a through has one factor. All operations on the k a through are done using that factor. When I have completed that I will have to do the same factor reduction in the right hand side matrix B k. So, I calculate mat B k is equal to mat B k minus factor into mat B k. When this iteration finishes let us just look at couple of iterations initially i is 0. So, i plus 1 is 1. So, I start k equal to 1 to n minus 1. So, for each row second, third, fourth, fifth or the index number 1, 2, 3 for a plane minus 1. Let us take the next row. First row I have got one on the diagonal. Second row the first element I am setting to 0 because it is below the diagonal. The next row has been already set to 1 the next element because all diagonal element we converted into 1. So, now I am saying for j equal to i plus 1 I calculate mat k j equal to this, mat B k is equal to this at the end of this I would have got the entire matrix into an upper diagonal form with the right hand side coefficient matrix representing the effect of all these operations correctly. Notice how small the code is. So, it is not that one has to write very lengthy programs to implement even a complex process such as Gauss elimination. Now, I do the back substitution starting with the last variable. As we observed the last variable is simply the right hand side coefficient. So, x n minus 1 is equal to matrix B n minus 1 and then for i equal to n minus 2 to 0 that is I go backwards in decrement of 1. Notice that this is i minus 1. I will sum up i throw using values of x already determined. So, sum is 0 j is equal to i plus 1 for j less than n j plus plus sum is equal to sum plus mat A i j into x j. What am I doing? These x j's represent the values which I have already found out. I am multiplying the already found out values with the coefficients and I am summing them up. So, in a particular row where the diagonal is 1 and other coefficients are there I am using the values of the remaining elements which I have found substituting them here I have only one unknown in this equation which is x i. So, the unknown values are i plus 1, i plus 2, i plus 3 etcetera. I calculate x i by subtracting from right hand side mat B i the sum that I have calculated. This is back substitution. Let us very briefly go over this again to consolidate our thought processes. I read the two matrices A and B. For the Gauss elimination I divide each row by coefficients on the diagonal. Effectively I normalize, I convert all the elements on the diagonal to be one on the coefficient matrix and correspondingly normalize the right hand side. Then I do the Gauss elimination technique implementation of getting the entire coefficient matrix in upper triangular form. For that I do that subtraction of a linear combination of one equation from the other kind of stuff and I will get all of these done. At the end I use back substitution to calculate values of all my variables. Finally, I output the results. Well for good measure I also output the matrices. So, this is one matrix A, this is another matrix B and then for i equal to 0 to n minus 1 I print the values of x. Notice a very funny kind of printing because I am printing a string x opening bracket. Then I am printing the value of i. Then I am printing a string closing bracket, e's colon. This is merely to get some meaningful print out as we shall see in a moment. Then finally I print x i which is the value and go to the next line. Now here is the question. I have used the c in and c out. In an actual program you may use scan f and print f. It does not matter. But what is important is that every time you run this program you will have to give that input by typing in values on the keyboard. If you have to give a few values which is what we have seen in most of our algorithms it is not a problem. But imagine if it is a matrix. Imagine it is a 10 by 10 matrix. So, you have to give 100 values. Not actually 100. You have to give first n which is 10. Then you have to give row by row 10, 10, 10. So, 100 values plus you have to give the 10 values on the right hand side. So, 111 values you have to type. Now suppose you run this program and you find there is some problem. The program is not working properly. You go back, check that program, correct it, run it again. Again you have to type in 110 values. This seems silly. For that purpose we look for some shortcuts. Fortunately the Unix operating system provides us with that shortcut. In fact there are some other facilities I would like to discuss both of them at this juncture. So, as I observed ordinarily we use the keyboard to give our input data and screen to see the output values. But if you have to repeatedly execute a program during trials then the same input has to be given again and again. Also once a program executes if its output comes on the screen, we read that alright. But next time when we run it again after making some changes we get some other output. Usually we would like to compare this output with the previous output. But the previous output has gone with the screen. So, getting output only on screen it is good provided I am just running a program to get final results. The program is perfectly working. But during the development stage I may want to retail the output of these programs at least two or three major runs for comparison purposes later. Just as you saw when I had shown you the results by running the Pi estimation program using time. How do you do that? Well this is the objective to see here. If the input values are large in number we know we can store these numbers in a file. What is the file? It is a text file like our program file. Such a file is pre-edited and it contains the required input data in the prescribed format. For example, roll marks dot text. We have roll numbers and corresponding mark. Roll number mark, roll number mark, roll number mark that could be one format. In this particular case also we could prepare our input data in the desired format. The question is how do we make my computer read data not from keyboard but from a file? For that there is a very simple facility called a read direction. When we use C in greater and greater this or when we use scanf in our C programs actually the operating system gives our C program a standard input file called stdin. stdin by the operating system is normally connected to keyboard. So whenever our scanf is executed in C program or whenever CIN is executed in C++ program or C program then what happens is that the operating system connects that to a stream of bytes coming from the keyboard. That is why all input is read from the keyboard. However the Unix operating system can redirect that stdin. We can tell operating system that look while you execute this program please do not connect stdin to keyboard as you normally do but instead connected to something else and what is that something else? Well a file which contains the desired data in the desired format which of course we would have pre-prepared. Consequently instead of simply saying dot slash a dot out if we say dot slash a dot out less than input data dot text. Please do not confuse this less than or greater than sign as we see here with the greater greater less less sign which we use inside the program. C in and C out are the programming instructions which are used inside the program. Here we are talking about the operating system instruction not that dollar prompt. So this is not what you write ever inside your program but this is the command that you give to the operating system. After compiling your program if you say dot slash a dot out less than input data dot txt this less than symbol is called input redirection. That means for this program in its entirety now the keyboard will be disabled. So you would not be giving any data on the keyboard because operating system has decided that you do not want keyboard to be standard input file. Instead you want input data dot txt file to be treated as standard input file. So operating system will automatically open this file, start reading bytes from this file. It is our responsibility to ensure that the text which is inside this file actually represents the values in exactly the same form in which we would have typed it on the keyboard. So same blank space between two values or line feed whatever whatever. Similarly it is possible to redirect all output to a named file just as operating system has STD in which is connected to keyboard the operating system also has STD out which is called a standard output and which by default is connected to our monitor. That is the reason why whenever we say c out or print f for that matter the output comes on our screen because the operating system simply gives a standard out file and c out or print f right to that output file. Since that file is connected by the operating system to our monitor that is where we see things. But if we use the redirection by saying dot slash a dot out greater than output data file dot txt in our command line then instead of any output coming to the screen that output will go to that file. That file can be later on examined. Please note that both redirections can be used as simultaneous. This is important and useful for us because now we can run this program may be full time during trial every time giving a different name for this file. Note that this file does not pre exist input file must pre exist but the output file is created by the operating. So let us say output data file one output data file two output data file three or output data file third July output data file fourth July we can use meaningful names for our trial and later on we can print these files and compare the results it makes it extremely easy for us to compare things across multiple executions. In any case the most important advantage for us is that in a limited time if you want to run this program multiple times you do not have to type the large number of values that will accompany us. So far in our labs also you have been giving only small value. You will realize that the same thing will be happening to your students. Later on when with your students you declare large data handling you discuss large data handling there could be serious problems.