OK so here is what I'm working with:
Header file:
Code:
// Provided by: Megan Kalscheuer
// Email Address: [email protected]
// FILE: stats.h
// CLASS PROVIDED: statistician
// (a class to keep track of statistics on a sequence of real numbers)
// This class is part of the namespace main_savitch_2C.
//
// CONSTRUCTOR for the statistician class:
// statistician( );
// Postcondition: The object has been initialized, and is ready to accept
// a sequence of numbers. Various statistics will be calculated about the
// sequence.
//
// PUBLIC MODIFICATION member functions for the statistician class:
// void next(double r)
// The number r has been given to the statistician as the next number in
// its sequence of numbers.
// void reset( );
// Postcondition: The statistician has been cleared, as if no numbers had
// yet been given to it.
//
// PUBLIC CONSTANT member functions for the statistician class:
// int length( ) const
// Postcondition: The return value is the length of the sequence that has
// been given to the statistician (i.e., the number of times that the
// next(r) function has been activated).
// double sum( ) const
// Postcondition: The return value is the sum of all the numbers in the
// statistician's sequence.
// double mean( ) const
// Precondition: length( ) > 0
// Postcondition: The return value is the arithmetic mean (i.e., the
// average of all the numbers in the statistician's sequence).
// double minimum( ) const
// Precondition: length( ) > 0
// Postcondition: The return value is the tinyest number in the
// statistician's sequence.
// double maximum( ) const
// Precondition: length( ) > 0
// Postcondition: The return value is the largest number in the
// statistician's sequence.
//
// NON-MEMBER functions for the statistician class:
// statistician operator +(const statistician& s1, const statistician& s2)
// Postcondition: The statistician that is returned contains all the
// numbers of the sequences of s1 and s2.
// statistician operator *(double scale, const statistician& s)
// Postcondition: The statistician that is returned contains the same
// numbers that s does, but each number has been multiplied by the
// scale number.
// bool operator ==(const statistician& s1, const statistician& s2)
// Postcondition: The return value is true if s1 and s2 both have length
// zero. Also, if the length is greater than zero, then s1 and s2 must
// have the same length, the same mean, the same minimum,
// the same maximum, and the same sum.
//
// VALUE SEMANTICS for the statistician class:
// Assignments and the copy constructor may be used with statistician objects.
#ifndef STATS_H // Prevent duplicate definition
#define STATS_H
#include <iostream>
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
IMPLEMENTATION FILE:
Code:
//FILE: stats.cpp
// CLASS IMPLEMENTED: stats
//#include <iostream>
//#include <cassert> //according to the text it provides assert // unnecessary
#include "stats.h" // provides the stats class definition
namespace main_savitch_2C
{
statistician::statistician() : count(0), total(0), tinyest(0), largest(0)
{} //initializing everything to 0
/*-----------------------------------------------------------------------+
| next(double r)
|
| Give the statistician a new number
|
| Post: the count of the numbers statistician has seen has been
| incremented
| the sum of all the numbers statistician has seen has been
| increment by r
| the values of tinyest and largest have been adjusted if
| necessary
*/
void statistician::next(double r)
{
if (count <= 0) { // update the number of entries statistician has
count = 1; // this is the first data item of the statistician
total = r;
tinyest = r;
largest = r;
return;
}
count = count + 1;
total += r; // adjust the sum
if (r < tinyest) { // update the min if necessary
tinyest = r;
}
if (largest < r) { // update the max if necessary
largest = r;
}
}
void statistician::reset( )
{
count = 0; // resets all values back to 0
tinyest = 0;
largest = 0;
total = 0;
}
/*-------------------------------------------------------------------+
| int length( ) const
|
| RETURN count which is the length of the sequence that has
| been given to the statistician (i.e., the number of times that the
| next(r) function has been activated).
*/
int statistician::length() const
{
return count;
}
double statistician::sum() const
{
return total;
}
double statistician::mean() const
{
return total/count; // mean = total divided by the number of numbers
}
double statistician::minimum() const
{ // minimum = tinyest number
return tinyest;
}
double statistician::maximum() const
{
return largest; // maximum = largest number
}
bool operator ==(const statistician& s1, const statistician& s2)
{
return true;
}
statistician operator + (const statistician & s1, const statistician & s2)
{
statistician s3;
s3.next(s1.sum() + s2.sum());
return s3;
}
statistician operator * (double scale, const statistician & s2)
{
return s2;
}
}
and the test program that I did not write:
Code:
/*---------------------------------------------------------------------------+
| stattest.cpp Aug 2 1996 Michael Main |
| |
| An interactive test program for the statistician class |
+---------------------------------------------------------------------------*/
#include <cctype> // Provides toupper
#include <iomanip> // Provides setw to set the width of an output
#include <iostream> // Provides cout, cin
#include <cstdlib> // Provides EXIT_SUCCESS
#include "stats.h"
using namespace main_savitch_2C;
using namespace std;
/*---------------------------------------------------------------------------+
| PROTOTYPES of functions used in this test program |
+---------------------------------------------------------------------------*/
void print_menu( );
// Postcondition: A menu of choices for this program has been written to cout.
// Library facilties used: iostream
char get_user_command( );
// Postcondition: The user has been prompted to enter a one character command.
// A line of input (with at least one character) has been read, and the first
// character of the input line is returned.
double get_number( );
// Postcondition: The user has been prompted to enter a real number. The
// number has been read, echoed to the screen, and returned by the function.
void print_values(const statistician& s);
// Postcondition: The length, sum, minimum, mean, and maximum of s have been
// written to cout, using a field width of 10 for each of these values.
// (If length is zero, then minimum, mean, and maximum are not written.)
int main( )
{
statistician s1, s2, s3; // Three statisticians for us to play with
char choice; // A command character entered by the user
double x; // Value for multiplication x*s1
cout << "Three statisticians s1, s2, and s3 are ready to test." << endl;
do {
cout << endl;
print_menu( );
choice = toupper(get_user_command( ));
switch (choice) {
case 'R': cout << "Which one should I reset (1, 2, 3) " << endl;
choice = get_user_command( );
switch (choice) {
case '1': s1.reset( );
break;
case '2': s2.reset( );
break;
case '3': s3.reset( );
break;
}
cout << "Reset activated for s" << choice << "." << endl;
break;
case '1': s1.next(get_number( ));
break;
case '2': s2.next(get_number( ));
break;
case '3': s3.next(get_number( ));
break;
case 'T': cout << "The values are given in this table:" << endl;
cout << " LENGTH SUM"
<< " MINIMUM MEAN MAXIMUM" << endl;
cout << " s1";
print_values(s1);
cout << " s2";
print_values(s2);
cout << " s3";
print_values(s3);
break;
case 'E': if (s1 == s2)
cout << "s1 and s2 are equal." << endl;
else
cout << "s1 and s2 are not equal." << endl;
break;
case '+': s3 = s1 + s2;
cout << "s3 has been set to s1 + s2" << endl;
break;
case '*': cout << "Please type a value for x: ";
cin >> x;
s3 = x * s1;
cout << "s3 has been set to " << x << " * s1" << endl;
break;
case 'Q': cout << "Ridicule is the best test of truth." << endl;
break;
default: cout << choice << " is invalid. Sorry." << endl;
}
} while ((choice != 'Q'));
return EXIT_SUCCESS;
}
void print_menu( )
{
cout << endl;
cout << "The following choices are available: " << endl;
cout << " R Activate one of the reset( ) functions" << endl;
cout << " 1 Add a new number to the 1st statistician s1" << endl;
cout << " 2 Add a new number to the 2nd statistician s2" << endl;
cout << " 3 Add a new number to the 3rd statistician s3" << endl;
cout << " T Print a table of values from the statisticians" << endl;
cout << " E Test whether s1 == s2" << endl;
cout << " + Set the third statistician s3 equal to s1 + s2" << endl;
cout << " * Set the third statistician s3 equal to x*s1" << endl;
cout << " Q Quit this test program" << endl;
}
char get_user_command( ) // Library facilties used: iostream
{
char command;
cout << "Enter choice: ";
cin >> command;
return command;
}
double get_number( ) // Library facilties used: iostream
{
double result;
cout << "Please enter the next real number for the sequence: ";
cin >> result;
cout << result << " has been read." << endl;
return result;
}
void print_values(const statistician& s) // Library facilties used: iostream
{
cout << setw(10) << s.length( );
cout << setw(10) << s.sum( );
if (s.length( ) != 0) {
cout << setw(10) << s.minimum( );
cout << setw(10) << s.mean( );
cout << setw(10) << s.maximum( );
} else {
cout << " none none none";
}
cout << endl;
}
I get to the problem when I reach the setting numbers to s1, s2 etc in the + and * option in the test program.
Can anyone help me here? Thanks!