1. Ch 10 Exercises 2:

O(n2). The outer two loops depend on n causing the inner loop and the computation within the inner loop to be performed n2 times. Since the inner most  loop computation is independent of n, it must be a constant time computation. The inner loop is performing a constant time operation 10 times and hence is also a constant time operation. So a constant time operation is being performed n2 times.

  1. Ch 10 Exercise 3:

When i is 0, the while loop performs 1 comparison. When i is 1 the while loop performs 2 comparisons.  This continues in this fashion until i is n-1, in which case the while loop performs n operations. Thus, the while loop performs 1 + 2 + ... + n = n(n+1)/2 operations.

  1. Ch 10 Exercise 6:

From the hint, logan = logbn/logba. But for given a and b, logba is a constant. Since in big O analysis, we don’t care about constant multipliers, we have O(logan) the same as O(logba). Therefore we may ignore the base of the logarithm.

  1. Ch 4 Exercise 12:

a)      p.getCoefficient(p.degree());

b)      p.changeCoefficient(p.getCoefficient(3)+8, 3);

c)      for(int i = 0; i <= p.degree() || i <= q.degree(); i++) {
            sum.changeCoefficient(p.getCoefficient(i)+q.getCoefficient(i), i);

}

  1. Ch 4 Programming Problem 9:

package hw3;

 

public class Polynomial {

    

    private double[] coefficients;

    private final int INITIAL_DEGREE = 5;

   

    // create a Polynomial

    public Polynomial() {

        coefficients = new double[INITIAL_DEGREE + 1];

    }

   

    // increases length of array coefficients to degree, if

    // length of coefficients < degree

    private void expandToAtLeast(int degree) {

        if (coefficients.length >= degree+ 1) {

            return;

        }

        double[] temp = new double[degree + 1];

        for (int i = 0; i < coefficients.length; i++) {

            temp[i] = coefficients[i];

        }

        coefficients = temp;

    }

   

    // returns the highest degree having a non-zero coefficient

    public int degree() {

        int degree = 0;

        for (int i = 0; i < coefficients.length; i++) {

            if (coefficients[i] != 0.0) {

                degree = i;

            }

        }

        return degree;

    }

   

    // returns the coefficent of x with power degree

    public double getCoefficient(int degree) {

        // if degree > length of coefficients array, coefficint is zero

        if (degree > coefficients.length) {

            return 0.0;

        }

        return coefficients[degree];

    }

   

    // Change the coefficient of x with power degree to new_coefficient

    public void changeCoefficient(double new_coefficient, int degree) {

        expandToAtLeast(degree);

        coefficients[degree] = new_coefficient;

    }

   

    // Return String formatted for display of Polynomial

    public String toString() {

        String poly = "";

        boolean first_shown = false;

        for (int i = coefficients.length - 1; i >= 0; i--) {

            if (coefficients[i] != 0) {

                if (!first_shown) {

                    poly += String.format("%d:%1.1f", i, coefficients[i]);

                    first_shown = true;

                } else {

                    poly += String.format(" + %d:%1.1f", i, coefficients[i]);

                }

            }

        }

        return poly;

    }

   

    // test cases, not complete

    public static void main(String[] args) {

        // create a polynomial

        Polynomial p = new Polynomial();

        p.changeCoefficient(1.0, 0);

        p.changeCoefficient(2.0, 1);

        p.changeCoefficient(1.0, 2);

       

        // check that p is 2:1.0 + 1:2.0 + 0:1.0

        System.out.println(p);

       

        // create another Polynomial and check degree()

        p = new Polynomial();

        p.changeCoefficient(2.0, 1);

        p.changeCoefficient(1.0, 8);

        p.changeCoefficient(1.5, 15);

       

        // degree() should be 15

        System.out.println(p + " has degree() = " + p.degree());

       

        // get a few coefficients. Check for coefficients that were

        // set, not set, and larger than largest

        System.out.printf("8:%1.1f\n", p.getCoefficient(8));

        System.out.printf("7:%1.1f\n", p.getCoefficient(7));

        System.out.printf("17:%1.1f\n", p.getCoefficient(17));

       

    }

   

}