Computer Science  City College of New York
  CSc21200 Data Structures, Fall 2016

Programming Assignment 1  - Implement and Test the Statistician Class

Modified from a Chapter 2 project at www.cs.colorado.edu/~main/projects/
of Data Structures and Other Objects Using C++,  Second Edition
by Michael Main and Walter Savitch

The Assignment:
You will implement and test a small class called statistician, which is similar to some of the small classes in Chapter 2 of the text.
Purposes:
Ensure that you can write a small class that meets a precise specification.
Make sure you understand how to write a class that is separated into a header file and an implementation file.
Give you experience in using a test program to track down bugs in a class's implementation.
Before Starting:
Read all of Chapter 2.
Know how to compile and run C++ programs on your system (Unix or Windows).
Due Date:
Wednesday, Sep 14, 2016 before class. If you have problems, late work will be accepted before Thursday with no penalties. Late work may be submitted on Friday or Saturday with 5% penalty per day. No work will be accepted after Saturday.
How to Turn In:
Files to be submitted: source code (*.cxx and *.h) . Please don't send your excutable files. Please submit homeworks using this link. On the upload page, you can select the file(s) you want to submit (multiple files can be selected by pressing down CTRL(or Cmd if you are a Mac user) key while selecting the files). Then enter your last name and the last 4 digit of your student ID (from CUNYfirst, NOT your SSN!!!). Once successfully submitted, it will show a page with RECEIPT NUMBER, save that number or that page because you will need it to retrieve your grade for that assignment. You can submit your assignment as many times as you want before the deadline, only the latest one will be graded.

To retrieve your grade for an assignment (after I finish grading it), please go to the download page here, select the assignment, enter the receipt number for that assignment, your last name and your last 4 digit of your student ID. The page will then download a TXT file which contain your graded submission.

Files that you must write:
  1. stats.h: The header file for the new statistician class. Actually, you don't have to write much of this file. Just start with our version and add your name and other information at the top. If some of your member functions are implemented as inline functions, then you may put those implementations in this file too.
  2. stats.cxx: The implementation file for the new statistician class. You will write all of this file, which will have the implementations of all the statistician's member functions.
Other files that you may find helpful:
    You shall compile one of the following files with your stats.cxx and link them to generate your executable:
  1. stattest.cxx: A simple interactive test program for you to test your implementations.
  2. statexam.cxx: A non-interactive test program that will be used to grade the correctness of your statistician class.

The Statistician Class
Discussion of the Assignment

As indicated above, you will implement a new class called statistician, using a header file (most of which is written for you) and an implementation file (which you will write by yourself). The statistician is a class that is designed to keep track of simple statistics about a sequence of real numbers. There are two member functions that you should understand at an informal level before you proceed any further. The prototypes for these two functions are shown here as part of the statistician class declaration:
   class statistician
   {
   public:
       ...
       void next(double r);
       double mean( ) const;
       ...
   };
The member function "next" is used to give a sequence of numbers to the statistician one at a time. The member function "mean" is a constant member function that returns the arithmetic mean (i.e., the average) of all the numbers that have been given to the statistician.

Example: Suppose that you want a statistician to compute the mean of the sequence 1.1, 2.8, -0.9. Then you could write these statements:

   // Declares a statistician object called s
   statistician s; 

   // Give the three numbers 1.1, 2.8 and -0.9 to the statistician
   s.next(1.1);
   s.next(2.8);
   s.next(-0.9);

   // Call the mean function, and print the result followed by a carriage return
   cout << s.mean( ) << endl;
The output statement will print 1.0, since 1.0 is the mean of the three numbers 1.1, 2.8 and -0.9.

Once you understand the workings of the next and mean member functions, you can look at the complete specification of the statistician class, which is in the file stats.h . Notice that the statistician class in this file is part of a namespace called main_savitch_2C. You should use this namespace for your statistician. In this file you will find a precondition/postcondition contract for all the statistician's member functions, including:

Hints and Frequently Asked Questions

The Private Member Variables
Carefully read the class definition in stats.h. Notice how the private member variables are being used to keep track of information about the statistician's sequence of numbers. The statistician does NOT keep track of all the numbers in the sequence. There is no need to do so, and trying to do so can get you into trouble. Instead, it keeps track of only the information that is relevant to its member functions: How many numbers have been seen? What is the sum of those numbers? If you have seen at least one number, then what are the smallest and largest numbers that you've seen so far? These four items should be your only private member variables.
Be careful about how you set the private member variable that keeps track of the smallest number. My suggestion is that you do NOT have the constructor initialize this member variables (because when the constructor does its work, there have not yet been any numbers, so there is no smallest number). But part of the work of the "next" function is to correctly maintain the private member variables. This means that the first time that the next function is called, it should set the private member variable that keeps track of smallest values. Later, if next is called again with a smaller number, then the next function will change the member variable that is keeping track of the smallest value. (You'll have a similar process for the member variable that's keeping track of the largest value).
Check Boundary Values
Make sure that your + and * operators work correctly when the arguments are statisticians with no numbers.
Check Preconditions
Your implementations should use the assert function to check preconditions of all functions.
Input and Output
Your implementations must NOT produce any output to cout, nor expect any input from cin. All the interaction with the member functions occurs through their parameters.
Implement and Test Small Pieces
Don't tackle to whole project at once. Start by implementing what you can, using "stubs" for the harder functions. A "stub" is the implementation of a function with the lines of the body omitted. For example:
    void statistician::next(double r)
    {
        // This is just a stub, to be implemented later.
    }
A first implementation might have only: Even with just stubs, your stats.cxx file will correctly compile and link with the interactive test program, stattest.cxx. For example, if you are using the g++ compiler with Linux in our student labs, you would compile and link the stattest program with these three commands:
    g++ -Wall -c stats.cxx
    g++ -Wall -c stattest.cxx
    g++ stattest.o stats.o -o stattest
Frequently Asked Questions
  1. I run the stattest (or statexam) and the program crashes with a failed assertion. Is it a good idea to remove the assertions (which I put in stats.cxx to check preconditions).
  2. ANSWER: No, leave those preconditions in there! The TA will clobber you (and I will too) if you delete the checks of the preconditions. Instead, you must find out where one of your functions is violating a precondition. Here is a typical example: Some students started by implementing the operator == along these lines:

    bool operator == (const statistician& s1, const statistician& s2)
    {
        return
            (s1.length( ) == s2.length( ))
            &&
            (s1.sum( ) == s2.sum( ))
            &&
            (s1.minimum( ) == s2.minimum( ))
            &&
            (s1.maximum( ) == s2.maximum( ));
    }
    The problem with this implementation is that the operator == is allowed to be called even if s1 or s2 or both are empty. In such a case, the function will eventually get down to the test (s1.minimum( ) == s2.minimum( )) and...assertion failed! because you cannot call minimum for an empty statistician.

    How do you fix this problem? In your operator == you should start with a test to see whether s1 or s2 is empty (and handle those cases in a way that does not call minimum() or maximum() ).

    MORAL: The functions you write can call other functions, but they must be careful to not violate preconditions.
     

  3. How should my constructor initialize the private member variables tiniest and largest?
  4. ANSWER: There are several solutions. One idea is to not initialize them at all. In this case, you must be careful to make sure of two things: (A) When the first number is given to the next function, it puts that first number into both tiniest and largest. (B) None of the other functions ever try to use tiniest or largest for an empty statistician.
     

  5. What other functions might need special cases for an empty statistician?
  6. ANSWER: Well, any function that accesses tiniest, largest, minimum() or maximum() probably needs a special case. Sometimes the special case can be simple. For example, the start of my operator + has two special cases:

    if (s1.length( ) == 0)
        return s2;
    if (s2.length( ) == 0)
        return s1;
    ...now the rest of my code doesn't need to worry about s1 or s2 being empty.
     
  7. What strange things happen in the operator * when the scalar is negative?
  8. ANSWER: Here's an example: Suppose that a statistician x has been given three numbers 10, 20 and 40, where y.minimum() will be 10 and y.maximum() will be 40. Then we execute the statement y = -1*x; The statistician y must act as if it had been given -10, -20 and -40 so y.minimum() will be -40 and y.maximum() will be -10.
     

  9. I'm having trouble compiling or linking my stats.cxx with statexam.cxx or stattest.cxx
  10. ANSWER: Make sure that all the code in your stats.cxx is in the namespace main_savitch_2C (look at the similar example of the point class on page 62). If you still have the compilation problem, send email to me for help. When you send messages for the Data Structures course, always remeber to include "CSC212" in your Subject line otherwise your messages could be ignored.
     

  11. When I write a friend function, the compiler still won't let the function access the private member variables of the statistician. Help!
  12. ANSWER: See the the answer to the previous question.
     

  13. Should I worry about warnings that occur when I compile.
  14. ANSWER: Yes. At this point of the game, about 60% of warnings are errors.  Spotting the cause of the warnings is an important part of learning about C++.
     

  15. Should I worry about putting lots of comments in my program.
  16. ANSWER: Not much of that is needed until a function gets longer than 10-15 lines.
     

  17. What causes these compilation errors:
  18.    // Problem 1: 
       s1.length( ) = s2.length( ) + s3.length( );
       // You can't assign to a function such as length. Try assigning to
       // s1.sum (the variable) instead.

       // Problem 2:
       if (s1.length == s2.length)
       // You have to call the function. Try (s1.length() == s2.length()).

Feng HU ( fhu@gradcenter.cuny.edu ), Fall 2016