CS 60, Fall 2009
Assignment 3

Copies of necessary files are available in ~cs60/hw3/ on CSIL, along with an executable version of our solution (testpoly).

Due: Tuesday, November 3, 9:00pm
Worth: 100 homework points

  1. Complete and test poly.c to implement the functions declared in poly.h.
    1. Save copies of both of these files and testpoly.c in your working directory for this project (and mv poly.c-skeleton poly.c). Do not change poly.h or testpoly.c - you will not turn in these files.
    2. Study and understand the data types declared near the top of poly.h, and think about how these types can be used to form linked lists. Also see the four functions that are already implemented in poly.c:
      • newTerm - creates a new Term structure by dynamically allocating memory for it; stores copies of the coeff and exponent parameters in the structure's members; and returns a pointer to this structure (or returns NULL if there is insufficient memory to store the node). You must use this function whenever your solution creates a new polynomial term.
      • freeTerm - frees the memory used by the parameter. You must use this function to free any temporary polynomial terms that you create (the freePoly function must use freeTerm too).
      • termsCreated and termsFreed are used by testpoly.c to evaluate memory management.
    3. Implement the eight required functions as follows:
      • addTerm - adds a term to the polynomial - either by changing the coefficient of an existing term, or by inserting a new term; maintains the terms in order from greatest to least exponent. This is the most difficult one (hints).
      • value - returns the double value of the first parameter (the Poly) at the value of the second parameter. In other words, plug in the second parameter as the value for x, and return the result of this calculation.
      • degree - returns degree of the parameter (greatest exponent value).
      • count - returns the number of terms in the parameter.
      • sum - returns a new polynomial equal to the sum of the 2 parameters.
      • diff - returns a new polynomial equal to the difference between the two parameters, as first Poly minus second Poly.
      • mult - returns a new polynomial equal to the first parameter times the second parameter.
      • freePoly - frees all memory space used by the polynomial, and sets pointer to NULL. You must use freeTerm to free each term (do not use free directly).
      Note - assume that an empty list indicates a polynomial equal to zero.
    4. Test your implementation with testpoly.c.
      • This program reads from stdin until it reads an EOF - it does not prompt the user for input. The program just echoes comment lines (first non-blank character is '#') and blank lines.
      • All other lines are commands. Most commands have one or more parameters, and these parameters must be separated from the command and from each other by one or more spaces or tabs.
      • Available commands and parameters are: add p c e, value p x, degree p, count p, sum, diff p p, mult, clear p, and print p, where p is either 1 or 2 to represent polynomial 1 or 2, c is a double coefficient value, e is an integer exponent greater than or equal to 0, and x is a double value.
      • For this sample input, our solution produces this sample output. Your results for these data should be identical, except for the last part, the total counts of terms created and freed. Since these counts depend on specific implementation details, it does not matter if your counts do not exactly match our solution's counts - what matters is that the number of terms created exactly matches the number of terms freed.
      • The object code for this program must be linked to the object code for poly.c. Here is the text of a suitable Makefile for this purpose.
      • The programs has one other feature that may help you. If run with the "-i" option, it will create initial polynomials. You can use this option to test parts of your solution even if your addTerm function is not working.
  2. Turn in poly.c from your engineering account as follows:
    turnin hw3@cs60 poly.c
    Late assignments may not be accepted.

Updated 10/23/09 by C. Michael Costanzo