Skip to the content of the web site.

Project A.3: Accessing or setting coefficients

Up to this point, we have defined both a polynomial-as-array class and a -as-linked-list, but we must now set the terms. We will consider this problem for both. In either case, the function declaration will be the same:

	public:
		// Constructors, destructor and other public member functions
		double get( unsigned int degree ) const;
		void set( double coefficient, unsigned int degree );

The function definition will be, however, very different.

Polynomials as arrays

To get the coefficient from an array, we just have to do a little math: The last entry (array_capacity - 1)of the array is storing the coefficient of the term with degree zero; thus, the coefficient we are interested in is:

double Polynomial_as_array::get( unsigned int degree ) const {
	return coefficient_array[array_capacity - 1 - degree];
}

The only problem is that if array_capacity - 1 - degree < 0, we are outside the bounds of the array, so instead we must...what should we do?

Initially, from almost every other example we have looked at, our initial reaction would be to throw a out_of_range exception; however, within the context of polynomials, if the array capacity is $10$, the highest degree of any term is $9$, so if I ask for the coefficient corresponding to $x^{12}$, the answer is zero:

double Polynomial_as_array::get( unsigned int degree ) const {
	if ( degree > array_capacity - 1 ) {
		return 0.0;
	} else {
		return coefficient_array[array_capacity - 1 - degree];
	}
}

Setting the coefficient corresponding to a given degree now has us ask what we should do:

  1. again, we could throw an out_of_range exception, but
  2. we could also increase the capacity of the array.

Because you should be able to easily handle the first, we'll look at the second.

Suppose that degree > array_capacity - 1. In order to store the coefficient of a term with the given degree, we must increase the capacity to degree + 1.

void Polynomial_as_array::set( double coefficient, unsigned int degree ) {
	if ( degree > array_capacity - 1 ) {
		size_t new_array_capacity{degree + 1};
		double new_coefficient_array = new double[new_array_capacity];

		// Copy over the entries...

		// Because we are explicitly increasing the size of the array, it must be index 0
		coefficient_array[0] = coefficient;
	} else {
		coefficient_array[array_capacity - 1 - degree] = coefficient;
	}
}

It's up to you to:

  1. copy over the entries from the old array to the new array,
  2. zero out all other entries in the array (except for maybe the first as we overwrite that entry anyway),
  3. delete the memory allocated for the old array, and
  4. update both coefficient_array and array_capacity.

As a hint, consider the following, as the coefficient of the constant term must continue to be in the last entry:

Question: What is the reasonable response if the polynomial currently has a capacity of storing a polynomial of degree $9$, but the user sets the coefficient of $x^{12}$ to $0.0$? Should you increase the capacity of the array? Of course, there may be no point in increasing the capacity of an array until necessary, so the correct move would be to ignore such a request if it is already before the begining of the array.

Polynomial as linked lists

To get the coefficient of a polynomial as a linked list, we need only walk through the linked list until we either

  1. find a term where the degree equals the requested degree, in which case we return the corresponding coefficient,
  2. find a term where the degree is less than the requested degree, in which case, there cannot be a term with the requested degree, so we return 0.0, or
  3. get to the end of the linked list, in which case, again, there cannot be a term with the requested degree, so we return 0.0.
double Polynomial_as_linked_list::get( unsigned int degree ) const {
	for ( Term *p_current_term{p_list_head};
	      p_current_term != nullptr;
	      p_current_term = p_current_term->p_next_term
	) {
		if ( p_current_term->term_degree == degree ) {
			return p_current_term->term_degree == degree ) {
		} else if ( p_current_term->term_degree < degree ) {
			return 0.0;
		}
	}

	return 0.0;
}

If we are setting a term, we must now ask ourselves:

  • are we setting the coefficient to 0.0, in which case
    1. if a term corresponding to the degree is in the linked list, we must delete it,
    2. otherwise, if there is no term in the linked list, we do nothing;
  • otherwise,

    1. if a term corresponding to the degree is in the linked list, we must update it,
    2. otherwise, we must include a new term into the linked list.
    // To be written...
    void Polynomial_as_linked_list::set( double coefficient, unsigned int degree ) {
    	if ( 0.0 == coefficient ) {
    	} else {
    		for ( Term *p_current_term{p_list_head};
    		      p_current_term != nullptr;
    		      p_current_term = p_current_term->p_next_term
    		) {
    			if ( p_current_term->term_degree == degree ) {
    				p_current_term->coefficient == coefficient ) {
    			} else if ( p_current_term->term_degree < degree ) {
    				return;
    			}
    		}
    	}
    }
    

    Questions and practice: