Computer Science 3675
Summer 2003
Programming Assignment 1

Due: Thu. July 3

A polynomial can be represented by an array holding its coefficients. For example, polynomial 3x2 - 8 is represented by an array holding (-8, 0, 3). It is convenient to store the coefficient of xk at index k in the array of coefficients.

This assignment has you implement a few simple operations on polynomials.


Functions

Write implementations of addition, subtraction and multiplication of polynomials, in C++. Make your functions have the following contracts and prototypes.

  //=================================================================
  //
  // sum(R,Rdeg,A,Adeg,B,Bdeg) stores the sum of polynomials A and B 
  // into array R.  That is, it computes R = A + B.
  //
  // Parameters:
  //   A    (in) The coefficient array for a polynomial
  //   Adeg (in) The degree of A
  //   B    (in) The coefficient array for a polynomial
  //   Bdeg (in) The degree of B.
  //   R    (out) An array that will be set to hold the coefficients
  //        of polynomial A + B.
  //   Rdeg (out) An integer variable that will be set to the degree of R
  //
  // Array R must have at least max(Adeg,Bdeg)+1 cells available.

  void sum(double R[], int& Rdeg, const double A[], int Adeg, 
           const double B[], int Bdeg);

  //=================================================================
  //
  // diff(R,Rdeg,A,Adeg,B,Bdeg) stores the difference of polynomials A and B 
  // into array R.  That is, it computes R = A - B.
  //
  // Parameters:
  //   A    (in) The coefficient array for a polynomial
  //   Adeg (in) The degree of A
  //   B    (in) The coefficient array for a polynomial
  //   Bdeg (in) The degree of B.
  //   R    (out) An array that will be set to hold the coefficients
  //        of polynomial A - B.
  //   Rdeg (out) An integer variable that will be set to the degree of R
  //
  // Array R must have at least max(Adeg,Bdeg)+1 cells available.

  void diff(double R[], int& Rdeg, const double A[], int Adeg, 
            const double B[], int Bdeg);

  //=================================================================
  //
  // product(R,Rdeg,A,Adeg,B,Bdeg) stores the product of polynomials A and B 
  // into array R.  That is, it computes R = A * B.
  //
  // Parameters:
  //   A    (in) The coefficient array for a polynomial
  //   Adeg (in) The degree of A
  //   B    (in) The coefficient array for a polynomial
  //   Bdeg (in) The degree of B.
  //   R    (out) An array that will be set to hold the coefficients
  //        of polynomial A * B.
  //   Rdeg (out) An integer variable that will be set to the degree of R
  //
  // Array R must have at least Adeg+Bdeg+1 cells available.

  void product(double R[], int& Rdeg, const double A[], int Adeg, 
               const double B[], int Bdeg);

Note: An in parameter only passes information into the function. An out parameter only passes information out of a function. A function must not use the value that one of its out parameters has when the function starts running. That is, you cannot pass information into the function through an out parameter.


Requirements

You are not asked to write a complete application here. Only the functions listed are to be provided. They are for use in other programs. I will test them by using them in another program.

For ease of grading, call your implementation file polynomial.cpp and your header file polynomial.h. Do not include a main function in polynomial.cpp.

Your functions should meet the following requirements. (And yes, I will grade down for failure to meet these requirements, even if you have not been asked to meet such stringent requirements in the past.)

  1. Your functions must have exactly the prototypes shown. If they do not, they will not link with my tester.

  2. All three of these functions should set the degree of the result to the index of a coefficient that is not zero. For example, polynomial 0x2 + 2x + 3 should be reported as having degree 1, not 2. If all coefficients of the polynomial are 0, say that its degree is 0. However, none of the functions can presume that the degrees of A and B that are given necessarily are their true degrees. It is possible, for example, for A[Adeg] to be 0.

  3. Your functions must not make any assumptions about how large the arrays are, other than those explicitly stated in the contracts. For example, it is unacceptable to assume that the polynomials have degree no greater than 100. It must be possible to compile your functions and put them into a library, and find that they can be used for arbitrarily large polynomials without recompiling the functions.

  4. Your functions must not have requirements that are not stated in the contracts. For example, it is unacceptable to insist that array R be set to all zeros by the caller before calling sum since the contract does not say anything about that.

  5. Your functions must not have any visible actions not stated in the contracts. For example, the contracts do not indicate that anything is being read or written, so nothing should be read or written. (Don't use cin or cout, for example.)

  6. Your functions must implement something close to the standard sum, difference and product algorithms.

  7. Strive for simplicity and elegance in your program. This is important, since you will be comparing your solution in C++ to another form of solution.

  8. Comment your program well. The given contracts must be included in your source file. Make your program clear and readable. Literate programming is a form of programming where programs are written in a way that is intended to be read, as a textbook would be read. In literate programming, the comments are primary, and the code is secondary. Get as close to literate programming as you can. Write your program for other people to read.

If you feel that these requirements are impossible to meet, or you do not see how to meet them, ask for help.


Testing the functions

Test your functions. In order to do the testing, you will want a function that prints a polynomial. That is not part of the assignment, and should not be turned in, but software designers often find that they need to write extra functions to aid in testing.

IF YOU DO NOT TEST YOUR FUNCTIONS, YOU CAN REST ASSURED THAT THEY DO NOT WORK, AND THEY WILL FARE POORLY WHEN GRADED.


How to turn in the assignment

Information on handing in programs will be supplied later.