 Welcome back, in the last segment we discussed a variation on the marks display program in which we needed to store the roll numbers as well as the marks. Now we are going to look at another interesting use of arrays mainly for polynomials representing polynomials and operating upon them. So let us say we are given two polynomials a of x and b of x. This is what a of x might look like a0 plus a1 x plus a2 x square plus an x raise to n. And this is what b of x might look like b0 plus b1 x plus b2 x square all the way till bm x raise to n. In general m may not be equal to n. We would like to find the product c of x equals a of x times b of x. So you know that the product is going to look like this. There is going to be a constant term c0 plus c1 x plus c2 x square all the way till cm plus n x to the power m plus n. So there are going to be m plus n plus 1 coefficients. So basically what is the problem? As input we are given these numbers a0, a1 all the way till an. So these numbers are given to us. We are also given these numbers and we are supposed to find these numbers. So we are given n plus 1 numbers these, m plus 1 numbers these and we are supposed to find m plus n plus 1 numbers which are these over here. So the question is how do we do that? And first where do we store these numbers? So by now the answer should be obvious. We should store these numbers in an array. So it is natural to use an array of n plus 1 elements to store the coefficients of a degree in polynomial. In fact we should store a sub i in array element with index i. So now we are supposed to generate the ci's. So how do we do that? So here is sort of the simple idea. So when we do this multiplication every term over here is going to multiply every term over here. So in particular let us say aix to the power i is a term in ax. Now that will multiply some term bj xj in bx. And if you just look at the product of these two terms that will be aibj times x to the power i plus j. What will be the effect of this term? So this is going to contribute to the coefficient of x to the power i plus j. And what is that coefficient? That coefficient is xc sub i plus j. So basically the idea over is that we form every such possible product and then we look at well if this was the coefficient of x to the i and if this was the coefficient of x to the j then the product that we calculate should contribute to the coefficient of i plus j. So in fact we should add the product into the current value of ci plus j. So that is exactly whatever algorithm is going to be. So let us say for simplicity we have two polynomials both of degree 10 and we want to multiply those. So clearly a will have 11 coefficients. So we will reserve an array with 11 elements for it and array of 11 elements for b and c will have 21 coefficients. So we will use an array of 21 elements for c. So let us see how do we read them? Reading is straight forward. So we are going to read a first starting with a0 going all the way till a10. So when I say read in the polynomial a of x well the polynomial is determined by the coefficient. So I really mean reading the coefficients. And of course you know by now that reading in the coefficients is done simply using a for loop. So by the way for loops are sort of the ideal thing to use with arrays because the index can be the control variable can simply be the index. Next we are going to read in the polynomial b in a similar manner. And now we are going to generate our polynomial c. So first we have to set all the coefficients and it to 0. So the coefficients range from 0 through 20 and so we are going to set all those coefficients to 0. And next we are going to form the product of every term in the a polynomial with every term in the b polynomial. So i is going to be the index which is going to range over the a polynomial the terms in the a polynomial and j is going to be the index which ranges over the terms in the b polynomial. And if we form the product ai times bj we said earlier it should affect c of i plus j. So that is exactly what we are going to do. So we are going to add ai times bj to ci plus j remember that this plus equal to is a short form for saying ci plus j equals ci plus j plus ai times bj. So that is it. So that is the entire multiplication. After that we just had to print out the result. Now I am going to just describe another polynomial multiplication algorithm and this algorithm uses sort of an interesting manipulation of the indices. So the problem is really the same we are given polynomials we want to find the product and this time I am going to note explicitly what is ck going to equal. So ck is going to get the product the value of a0 bk will contribute to ck then a1 bk minus 1 will also contribute to ck and so on until akb0. So now what we are going to do is we are going to compute c0 first then c1 then c2 then all the way till cm plus n. So earlier we sort of did the multiplications in a convenient order and we updated ck's. Now we are going to say no let us construct ck's let us construct the complete ck's and c0 then the complete c1 complete c2 and so on. And for that we need this formula and as you can see if you want to implement this formula we have to access the elements of a and b in an interesting order. So the elements of a have to be accessed starting with 01 all the way till k which is simple enough but the elements of b have to be accessed starting at k then going down k minus 1 k minus j coming back down to 0. So that is sort of the unusual part of this program. So we are going to do this by setting ck equal to 0 first and then this loop can be written down in this manner. So ck so i is going to range from 0 to k. So this is i the suffix the index of a is i so i is going to be 0 first 1 all the way till k. So that is what this loop range is telling us. So ai then b of k minus i so indeed if you see the coefficient over here is k minus this coefficient this is k minus this coefficient and so on. So that is what we end up writing. So this will generate ck for any fixed k. So all that remains is to just put this into a loop and we are done. So as an exercise I will let you complete the program to multiply polynomials using the second method and then I would also suggest you to change both programs so that instead of multiplying a degree 10 polynomial by a degree 10 polynomial you write it for a general m. You can put m and m as constants but make them different and write them as use the idea define them as constant m whatever equal to 10 constant m equal to 15 and then write in terms of m and m. Then I would also like you to go over the loops and estimate how many multiplications are performed in multiplying a degree m polynomial by a degree n polynomial and you should see that roughly there are about m times n multiplications that get performed. So this is just to get a sense of how much time is this algorithm going to take let us say if we are multiplying really huge polynomials. So what did we discussed? So we said that polynomials can be nicely represented by arrays nicely in the sense that the coefficient of the ith power can be stored in the ith element and we do not need to store i explicitly just the position tells us the position of the coefficient in our memory tells us what i it refers to and this is actually very similar to the case when we had consecutive roll numbers starting at 0. Then we also saw that multiplication can be done in different ways and especially the second way is slightly trickier and I would request you to go over that maybe print out the indices that get accessed in the loop if you are not sure but anyway so there are two ways and please also look at the exercises that were given. So in the next segment we are going to discuss a somewhat longish problem called taxi dispatch which uses the array in another interesting manner but we will take a quick break.