CS 112 A1 - Fall 2004: Homework 2

Due Tuesday, October 12, 11:59 P.M.

Reading: Chapters 3, pages 78-96 and chapter 5, pages 157-183.

Please keep in mind the policy on collaboration.

Don't forget to sign up for our email list by typing csmail -a cs112a1


PROBLEM 1:

(30 points)

The two files below, orderedlist.h and orderedlist.cpp give an ordered list which stores nodes with items ordered from smallest to largest. The .cpp file is missing two methods, the bool Remove(ItemType item) method and the bool Find (ItemType item) method., The Remove method should return false if the item is not the list, and otherwise remove the item from the list and returns true. The Find method should find item in the list and print out where it is found and how many times it occurs in the list. If the item is in the list it returns true, otherwise false. So it might output, " 3.0 was found 4 times in the list starting at position 7." Add these two methods to orderedlist.cpp and test it.

PROBLEM 2:

(60 points)

Here is the header for the class Polynomial.

Implement the polynomial class in a new file polynomial.cpp. It may help you implement EqualToSum, EqualToDiff, and EqualToProd if you remember that a method of a class is allowed to access private members of other instances of the same class. Be sure to handle memory management properly, not leaving any dangling pointers and lost memory. Also be sure to follow the comments in polynomial.h about how the linked list arranged, and print the polynomials as shown below. To test out your class, you should write a main program which prompts you to input monomials at the terminal and build two polynomial. It should then print out the 2 polynomials and evaluate each one at a number input by the user. You will be implementing a method named Eval (see polynomial.h) in order to do this. It should then find their sum, difference and product and print out each.

Put your program into the file test2.cpp

The output of main should look something like,

(Note: For grading, the polynomials must print precisely as in the examples below. We will take points off if they do not.)

Please input the first polynomial. Use the coefficient -999 to end.

coeff: 4

deg: 2

coeff: 5.1

deg : 3

coeff: .2

deg : 1

coeff: -999

deg: 1

The first polynomial is

5.1x^3 + 4x^2 + .2^x
Please input the second polynomial. Use the coefficient -999 to end. etc.

Documentation required for hw2:

For problem 1 you need not do any additional documentation as you are simply adding some code to .cpp files we have written.

For problem 2 you should make sure the two program files polynomial.cpp and test2.cpp are well-commented, formatted and observe the usual rules of good programming. (See the "Coding Standards" link on the course web page.)

You should also submit a users manual for the polynomial class in a file called polynomial.txt. For information about users' manuals see the "Documentation Standards" link on the course web page.

To submit hw2:

  1. You must be logged onto csa, and all files must be on the csa computer.
  2. Test that your program works by compiling it with g++ -Wall.
  3. Create a subdirectory called hw2, using the mkdir command.
  4. Copy all files to be submitted and no others into this subdirectory. The following files (and ONLY these files) should be in subdirectory hw1:
  5. To submit the assignment, cd to the directory containing the hw2 subdirectory and type:
    gsubmit cs112 -cp hw2
    This should submit the entire hw2 subdirectory.

Important: Please follow all instructions about file names and how to submit assignment exactly, or there is a very real chance that your assignment will not be accepted! For example, a directory named "Hw2" will probably not be seen by the grader.