Computer Science 3675
Fall 2002
Programming Assignment 1

Due: Mon, Sep 16, 11:59pm

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.
  //
  // Parameter Adeg is the degree of A, and Bdeg is the degree of B.
  // The sum function stores the degree of R into variable Rdeg.  Parameter
  // Rdeg is strictly an out parameter.  Function sum does not use the
  // value that Rdeg has when sum starts.
  //
  // 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 differenc of polynomials A and B 
  // into array R.  That is, it computes R = A - B.
  //
  // Parameter Adeg is the degree of A, and Bdeg is the degree of B.
  // The diff function stores the degree of R into variable Rdeg.  Parameter
  // Rdeg is strictly an out parameter.  Function diff does not use the
  // value that Rdeg has when diff starts.
  //
  // 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.
  //
  // Parameter Adeg is the degree of A, and Bdeg is the degree of B.
  // This function stores the degree of R into variable Rdeg.  Parameter
  // Rdeg is strictly an out parameter.  This function does not use the
  // value that Rdeg has when product starts.
  //
  // 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);


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

Turn in your program using the handin program. Do that as follows.

  1. In your .cshrc file, put the following line, using a text editor.

         alias handin3675 "/export/stu/classes/csci3675/bin/handin csci3675"
    Be sure that any terminal window that you use is started after you make this change to the .cshrc file.

  2. To hand in file polynomial.cpp, use the following command (in a terminal window).

        handin3675 1 polynomial.cpp
      

  3. Check the response. You should get an indication that the file was handed in successfully. If you do not, the handin was not successful. Check that your alias line in the .cshrc file is correct, and that your file exists in the current directory. If handin says the assignment does not exist, or that you are not a student in the class, then there is a problem with the configuration files for handin. Send me email telling me what the problem is.