Trouble with creating class to handle array objects

This program is supposed to track rainfall statistics, but something is wrong with either the class declaration/implementation. Any help would be greatly appreciated

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>

using namespace std;

class Stats
{
private:

int numArray = 12;
double ary[12] = {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
double total = 0;
double avg = 0;
double largest = 0;
double smallest = 0;

public:

Stats();
~Stats();
void setValue(int, double);
void getValues();
double getTotal();
double getAvg();
double getLargest();
double getSmallest();

};

Stats::Stats()
{
for (int i = 0; i < numArray; i++)
{
ary[i] = 0;
}
}

Stats::~Stats()
{

}

void Stats::setValue(int element, double numVal)
{
if (numVal < 0) {
ary[element] = 0;
}
else {
ary[element] = numVal;
}
return;
}

void Stats::getValues()
{
for (int i = 0; i < numArray; i++)
{
cout << ary[i] << endl;
}
return;
}

double Stats::getTotal()
{
total = 0;
for (int i = 0; i < numArray; i++)
{
total += ary[i];
}
return total;
}

double Stats::getAvg()
{
total = 0;
for (int i = 0; i < numArray; i++)
{
total += ary[i];
}

avg = total / numArray;

return avg;
}

double Stats::getLargest()
{
for (int i = 0; i < numArray; i++)
{
if (largest < ary[i]) {
largest = ary[i];
}
}
return largest;
}

double Stats::getSmallest()
{
for (int i = 0; i < numArray; i++)
{
if (smallest > ary[i]) {
smallest = ary[i];
}
}
return smallest;
}

int main()
{
const int numEl = 12;
double arys[numEl];
Stats rainfall[numEl];

for (int i = 0; i < numEl; i++)
{
cout << "Please enter a rainfall number: ";
cin >> arys[i];
}

for (int i = 0; i < numEl; i++)
{
setValue(i, arys[i]);
}

cout << "The rainfall statistics you entered were: \n";
rainfall.getValues();
cout << "The total rainfall is: ";
rainfall.getTotal();
cout << "\nThe average rainfall is: ";
rainfall.getAvg();
cout << "\nThe largest value is: ";
rainfall.getLargest();
cout << "\nThe smallest value is: ";
rainfall.getSmallest();

system("pause");
return 0;
}
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
#include <array>
using std::array;
#include <iostream>
using std::cout;

class Stats
{
public:
	static const unsigned _Length = 12;
	typedef std::array<double, _Length> DataType;

private:
	DataType mData;
	double mTotal;
	double mAverage;
	double mLargestValue;
	double mSmallestValue;

public:
	Stats()
		: mData()
		, mTotal(0)
		, mAverage(0)
		, mLargestValue(0)
		, mSmallestValue(0)
	{}
	Stats(DataType _data)
		: mData(_data)
		, mTotal(0)
		, mAverage(0)
		, mLargestValue(0)
		, mSmallestValue(0)
	{
		update();
	}
	~Stats()
	{}
	void setValue(unsigned element, double value)
	{
		if (element >= _Length)
			return;
		mData[element] = value;
	}
	void printValues()
	{
		for each (double value in mData)
			std::cout << value << std::endl;
	}
	void update()
	{
		
		const unsigned _inverseSize = 1 / _Length;
		double _sum = 0;
		double _min = mData[0];
		double _max = mData[0];
		for each(double value in mData){
			//computing smallest value
			if (value < _min) _min = value;
			//computing largest value
			if (value > _max) _max = value;
			//computing total
			_sum += value;
		}
		//computing average
		mAverage = _sum * _inverseSize;

		mTotal = _sum;
		mSmallestValue = _min;
		mLargestValue = _max;
		
	}
	inline double getTotal() const
	{
		return mTotal;
	}
	inline double getAverage() const
	{
		return mAverage;
	}
	inline double getLargest() const
	{
		return mLargestValue;
	}
	inline double getSmallest() const
	{
		return mSmallestValue;
	}

};



int main(int argc,char* argv[])
{

	Stats::DataType _tmpData;

	for (unsigned i = 0; i < Stats::_Length; i++)
	{
		std::cout << "Please enter a rainfall number: ";
		std::cin >> _tmpData[i];
	}

	Stats rainfall(_tmpData);

	std::cout << "The rainfall statistics you entered were: \n";
	rainfall.printValues();

	std::cout << "The total rainfall is: " << rainfall.getTotal();

	std::cout << "\nThe average rainfall is: " << rainfall.getAverage();

	std::cout << "\nThe largest value is: " << rainfall.getLargest();

	std::cout << "\nThe smallest value is: " << rainfall.getSmallest();

	system("pause");
	return 0;
}
@Ericool

Line 52: Integer division - why can't you do division on line 65?
we can replace it by mAverage = _sum / _Length; and remove the line 52.
Last edited on
@Ericool thank you for the reply, the code compiles and works. Unfortunately I can't use parts of it in my own as I don't understand how it works. For instance, I've never had arguments in my main() parameter list before.

My current problem is that I'm able to read integers into the the class's private data member array via setValue(), but none of my other functions are returning avg, largest, smallest. I just get blank output. Any idea why that might be?
they were returning value but they were not use by the cout instance, the semicolon should tell where an instruction ends. for the main parameters you can ignore them, for now. the rest is up to you to figure it out. good luck :)
Topic archived. No new replies allowed.