Homework 15


Due : 9.30am, 2 August

Exercise 8 from Chapter 3 of the textbook (p.143)
(to do with using an ADT for a polynomial)

Solution

Assume P, Q and R are polynomials.
  1. P.Coefficient (P.Degree ())
    
  2. P.ChangeCoefficient (P.Coefficient (3)+8, 3)
    
  3. // to compute: R = P + Q
    
    // find maximum degree
    int MaxDegree = P.Degree () > Q.Degree () ? P.Degree () : Q.Degree ();
    
    // loop through and add coefficients
    for ( int i=0; i<=MaxDegree; i++ ) // loop through coefficients
    {
       R.ChangeCoefficient (P.Coefficient (i)+Q.Coefficient (i), i);
    }
    

Last updated : 2 August 2000 7.50pm