help with a stats class

Hello, I'm currently working on a stats class project for my data structures class, we've been given a header file with pre/post conditions (located below) and must create the implementation file and driver program to test all functions and possible conditions.

Having some trouble figuring out a few things



-How do i pass a stats a sequence of numbers in my driver program?
-my * operator's, I'm failing to understand how the scale value works and could really use a point in the right direction here
-general code oversight, please inform me if you see something odd/out of place

Below is what I've been given to work off of

All help is greatly appreciated, thanks for your time.

// FILE: stats.h
// CLASS: stats -- A class to keep track of statistics on a sequence of real numbers

// CONSTRUCTOR for the class:
// Postcondition: The object has been initialized, and is ready to accept a sequence of numbers.

// PUBLIC MODIFICATION member functions for the statistician class:
// void next(double r)
// Postcondition: The number r has been given to the statistician as the next number in its sequence of
// numbers. All member variables will be updated.

// void reset( );
// Postcondition: The statistician has been cleared, as if no numbers had yet been given to it.

// void setcount(int init_count)
// Postcondition: count will be set to init_count

// void settotal(double init_total)
// Postcondition: total will be set to init_total

// void setminimum(double init_min)
// Postcondition: minimum will be set to init_min

// void setmaximum(double init_max)
// Postcondition: maximum will be set to init_max

// PUBLIC CONSTANT member functions for the statistician class:
// int getcount( ) const
// Postcondition: The return value is the count of the number of elements in the statistician

// double gettotal( ) const
// Postcondition: The return value is the total of all the numbers in the statistician's sequence.

// double mean( ) const
// Precondition: getcount( ) > 0
// Postcondition: The return value is the average of all the numbers in the statistician's sequence.

// double getminimum( ) const
// Precondition: getcount( ) > 0
// Postcondition: The return value is the minimum number in the statistician's sequence.

// double getmaximum( ) const
// Precondition: getcount( ) > 0
// Postcondition: The return value is the maximum number in the statistician's sequence.


// NON-MEMBER functions for the statistician class:
// stats operator+ (const stats & left, const stats & right)
// Postcondition: The statistician that is returned contains all the numbers of the sequences of left and right

// stats operator* (double scale, const stats & right) and stats operator* (const stats & left, double scale)
// Postcondition: The statistician that is returned contains the same
// numbers that left or right does, but each member has been multiplied by the scale number.

// bool operator ==(const stats& left, const stats& right)
// Postcondition: Return true if the left and right have the same count, mean, minimum, maximum, and total

// ostream& operator<< (ostream & cout, const stats & right)
// Postcondition: Displays the contents of the statistician (count, total, minimum, maximum, and mean)


header file "stats.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  #include <iostream>
using namespace std;
#ifndef STATS_H     // Prevent duplicate definition
#define STATS_H
class stats
{
    public:
        // CONSTRUCTOR
        stats( );
        // MODIFICATION MEMBER FUNCTIONS
        void next(double r);
        void reset( );
        void setcount(int) { init_count = count; }
        void settotal(double)  { init_total = total; }
        void setminimum(double)  { init_minimum = count; }
        void setmaximum(double)  { init_maximum = count; }
        // CONSTANT MEMBER FUNCTIONS
        int getcount( ) const { return count; }
        double gettotal( ) const { return total; }
        double mean( ) const { return mean; }
        double getminimum( ) const { return minimum; }
        double getmaximum( ) const { return maximum; }
    private:
        int count;       // How many numbers in the sequence
        double total;    // The sum of all the numbers in the sequence
        double minimum;  // The smallest number in the sequence
        double maximum;  // The largest number in the sequence
    };
    // NON-MEMBER functions for the stats class
    bool operator== (const stats& left, const stats& right);
    stats operator+ (const stats& left, const stats& right);
    stats operator* (double scale, const stats& right);
    stats operator* (const stats & left, double scale);
    ostream& operator<< (ostream & cout, const stats & right);
#endif


implementation file stats.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "stats.h"
#include <cassert>

using namespace std;

stats::stats()  : count(0), total(0) 
{

}

void stats::next(double r)
{
	if (count <= 0)
{
count = 1;
total = r;
minimum = r;
maximum = r;
return;

count = count + 1;
total +=r;
if (r < minimum)
{
minimum = r;
}
if (maximum < r)
{
maximum = r;
}

}
}

void stats::reset()
{
count= 0;
total = 0;
}

void stats::setcount(int init_count)
{
	init_count = count;
}

void stats::settotal(double init_total)
{
	init_total = total;
}

        
void stats::setminimum(double init_minimum)
{
		init_minimum = minimum;
}


void stats::setmaximum(double init_maximum)
{
		init_maximum = maximum;
}

int stats::getcount( ) const
{
	return count;

}

double stats::gettotal( ) const
{
	return total;
}

double stats::mean( ) const
{

	double mean;
	assert (count > 0);
	mean = total / count;

	return mean;
}

double stats::getminimum( ) const
{

	assert (count > 0);
	return minimum;
}

double stats::getmaximum( ) const
{
	assert (count > 0);
	return maximum;
}

bool operator== (const stats& left, const stats& right)
{
		
		return 	(left.getcount() == right.getcount() && 
				 left.mean() == right.mean() &&	
				 left.getminimum() == right.getminimum() &&	
				 left.getmaximum() == right.getmaximum() &&	
				 left.gettotal() == right.gettotal());	

}

stats operator+ (const stats& left, const stats& right)
{
	stats result;
		
result.next (left.gettotal() + right.gettotal());

		return result;

}
stats operator* (double scale, const stats& right)
{
	stats result;
	result.next (right.gettotal() * scale);
		return result;

}
stats operator* (const stats & left, double scale)
{
		stats result;
		result.next (left.gettotal() * scale);
		return result;

}
ostream& operator<< (ostream & cout, const stats & right)
{
	cout << "the count of this stat is " << right.getcount() << endl 
	<< " the total is " << right.gettotal() << endl
	<< "the minimum is " << right.getminimum() << endl
	<< "the maximum is " << right.getmaximum() << endl
	<< "the mean is " << right.mean() << endl;

	return cout;
}

How do i pass a stats a sequence of numbers in my driver program?


You have to use the member functions provided in the interface to load your numbers. Typically you would pass in an object into the constructor but as you can see in the header, the constructor doesn't take any arguments.

Did you professor make this? I am puzzled as to why he implemented many of the class methods in the header and repeated them in the source file. That is a great way to confuse the hell out of your students.
Topic archived. No new replies allowed.