class statisticianThe 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.
void next(double r);
double mean( ) const;
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 sThe output statement will print 1.0, since 1.0 is the mean of the three numbers 1.1, 2.8 and -0.9.
// Give the three numbers 1.1, 2.8 and -0.9 to the statistician
// Call the mean function, and print the result followed by a carriage return
cout << s.mean( ) << endl;
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:
bool operator ==(const statistician& s, const statistician& t);In order for two statisticians to be equal, they must have the same length (i.e., they have been given the same number of numbers). Also, if their length is greater than zero, they must also have the same mean, the same minimum, the same maximum, and the same sum. For example: Suppose that a statistician s has been given four numbers 1, 2, 3, 4. A second statistician t has been given four numbers 1, 1.5, 3.5, 4. Then the test (s==t) must return true since both s and t have equal values for all the member functions, as shown here:
statistician operator +(const statistician& s, const statistician& t);
statistician operator *(double scale, const statistician& s);This is not a member function. The result of a multiplication such as 2*s is a new statistician that looks as if it had been given all the numbers of s, multiplied by the constant 2. Examples: Suppose that s is a statistician that has been given 1, 2, 3, and u is another statistician. Then the assignment statement u=2*s will result in u behaving as if it had been given the numbers 2, 4, 6. As another example, the assignment statement u=-3*s will result in u behaving as if it had been given the numbers -3, -6, -9. Notice that neither + nor == are member functions. (See Section 2.5 in your textbook and the notes in lecture 3). The result of s+t is a new statistician that looks as if it had been given all the numbers of the sequence for s, followed by all the numbers of the sequence for t. For example: Suppose that we have three statisticians s, t, and u. The statistician s has been given the numbers 1, 2, 3; the statistician t has been given the numbers 4, 5. Then the assignment statement u=s+t will result in u behaving as if it had been given the five numbers 1, 2, 3, 4, 5.
void statistician::next(double r)A first implementation might have only:
// This is just a stub, to be implemented later.
g++ -Wall -c stats.cxx
g++ -Wall -c stattest.cxx
g++ stattest.o stats.o -o stattest
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)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.
(s1.length( ) == s2.length( ))
(s1.sum( ) == s2.sum( ))
(s1.minimum( ) == s2.minimum( ))
(s1.maximum( ) == s2.maximum( ));
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.
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.
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)...now the rest of my code doesn't need to worry about s1 or s2 being empty.
if (s2.length( ) == 0)
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.
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.
ANSWER: See the the answer to the previous question.
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++.
ANSWER: Not much of that is needed until a function gets
longer than 10-15 lines.
// 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()).