Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 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:

A default constructor, which merely does any initialization needed for the statistician to start its work

The next and mean functions, described above

A constant member function called length, which returns the count of how many numbers have been given to the statistician

Two constant member functions called minimum and maximum, which return the smallest and largest numbers that have been given to the statistician. (By the way, these two functions and the mean function all have a precondition that requires length( ) > 0. You cannot use these three member functions unless the statistician has been given at least one number!)

A constant member function called sum, which returns the sum of all the numbers that have been given to the statistician. This function does NOT have a precondition. It may be called even if the statistician has NO numbers (in which case it should return 0).

An overloaded operator == which tests to see whether two statisticians are "equal". The prototype is:

int 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:

s.length( ) and t.length( ) are both 4

s.mean( ) and t.mean( ) are both 2.5

s.sum( ) and t.sum( ) are both 10.0

s.minimum( ) and t.minimum are both 1

s.maximum( ) and t.maximum are both 4

An overloaded + operator which has two statisticians as arguments, and returns a third statistician, as shown in this prototype:

statistician operator +(const statistician& s, const statistician& t);

An overloaded * operator which allows you to "multiply" a double number times a statistician. Here is the prototype:

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 the class notes). 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.

Hints

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:

The constructor

A simple version of next that increments the private member variable to keep track of how many numbers have been seen

The length function

Even with just stubs, your stats.cpp file will correctly compile and link with the interactive test program, stattest.cpp.

This is my cpp file:

#include "awebb_stats.h"

//Open namespace

namespace main_savitch_2C {

statistician::statistician()

{

count = 0;

total = 0;

tinyest = 0;

largest = 0;

}

void statistician::next(double r)

{

count++;

r = total;

total += r;

if (count == 1)

{

tinyest = r;

largest =r;

}

else

{

if (tinyest > r ) tinyest = r;

if (largest > r ) tinyest = r;

}

}

void statistician::reset()

{

count = 0;

total = 0;

tinyest = 0;

largest = 0;

}

int statistician::length() const

{

return count;

}

double statistician::sum() const

{

return total;

}

double statistician::mean() const

{

if (count >0)

return total/count;

}

double statistician::minimum() const

{

if (length() < 0)

{

return tinyest;

}

}

double statistician::maximum() const

{

if (length () > 0)

{

return largest;

}

}

statistician operator + (const statistician& s1, const statistician& s2)

{

statistician result;

result.length = s1.length() + s2.length();

result.sum = s1.sum() + s2.sum();

if (s1.minimum() < s2.minimum())

result.tinyest = s1.minimum();

else

result.tinyest = s2.minimum();

if (s1.maximum() < s2.maximum())

result.largest = s2.maximum();

else

result.largest = s1.maximum();

return result;

}

statistician operator * (double scale, const statistician& s)

{

statistician result;

result.count = s.length() * s.length();

result.total = s.sum() * s.sum();

if (s.minimum() < s.minimum())

result.tinyest = s.minimum();

else

result.tinyest = s.minimum();

if (s.maximum() < s.maximum())

result.largest = s.maximum();

else

result.largest = s.maximum();

return result;

}

bool operator ==(const statistician& s1, const statistician& s2)

{

if (s1.length() != s2.length) return false;

unsigned home = s1.length();

unsigned s = 0;

while ((s < home) && (s1 == s2)) s++;

return (s == home);

}

//close namespace

}

#ifndef awebb_STATS_H // Prevent duplicate definition

#define awebb_STATS_H

#include

namespace main_savitch_2C

{

class statistician

{

public:

// CONSTRUCTOR

statistician();

// MODIFICATION MEMBER FUNCTIONS

void next(double r);

void reset();

// CONSTANT MEMBER FUNCTIONS

int length() const;

double sum() const;

double mean() const;

double minimum() const;

double maximum() const;

// FRIEND FUNCTIONS

friend statistician operator +

(const statistician& s1, const statistician& s2);

friend statistician operator *

(double scale, const statistician& s);

private:

int count; // How many numbers in the sequence

double total; // The sum of all the numbers in the sequence

double tinyest; // The smallest number in the sequence

double largest; // The largest number in the sequence

};

// NON-MEMBER functions for the statistician class

bool operator ==(const statistician& s1, const statistician& s2);

}

#endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications Dasfaa 2023 International Workshops Bdms 2023 Bdqm 2023 Gdma 2023 Bundlers 2023 Tianjin China April 17 20 2023 Proceedings Lncs 13922

Authors: Amr El Abbadi ,Gillian Dobbie ,Zhiyong Feng ,Lu Chen ,Xiaohui Tao ,Yingxia Shao ,Hongzhi Yin

1st Edition

3031354141, 978-3031354144

More Books

Students also viewed these Databases questions

Question

9. Explain the relationship between identity and communication.

Answered: 1 week ago

Question

a. How do you think these stereotypes developed?

Answered: 1 week ago

Question

a. How many different groups were represented?

Answered: 1 week ago