Create a STATS CLASS whose member data includes an array capable of storing 30 d
ID: 3546932 • Letter: C
Question
Create a STATS CLASS whose member data includes an array capable of storing 30 double data values, and whose member function includes total, lowest, highest, and average function for returning information about the data to the client program. In addition to these functions, the STATS CLASS should have a Boolean storeVALUE function that accepts a double value from the client program and stores it in the array. It is the job of the function to keep track of how many valuse are currently in the array, so it will know where to put the next value it recives and will know how many values there are to process when it is carrying out its other function. It is also the job of the function to make sure that no more than 30 values are accepted. If the storeVALUE function is able to successfully store the value sent to it, it should return "True" to the client program. However if the client program tries to store a thirty-first value, the function should not store the value and should return "False" to the client program.
The client program should create and use a stats object to carry out the rainfall analysis. Notice that the stats object does no O/I. All the input and output is done by the client program.
Explanation / Answer
#include<iostream>
using namespace std;
class STATS
{
private:
double rain_values[30];
int data_count;
public:
STATS()
{
data_count = 0;
}
double total()
{
double sum = 0;
for(int i=0; i<data_count; i++)
{
sum = sum + rain_values[i];
}
return sum;
}
double lowest()
{
double min = rain_values[0];
for(int i=1; i<data_count; i++)
{
if(rain_values[i]<min) min = rain_values[i];
}
return min;
}
double highest()
{
double max = rain_values[0];
for(int i=1; i<data_count; i++)
{
if(rain_values[i]>max) max = rain_values[i];
}
return max;
}
double average()
{
return total()/data_count;
}
bool storeVALUE(double value)
{
if(data_count<30)
{
rain_values[data_count++] = value;
return true;
}
else
return false;
}
};
int main()
{
STATS new_stats;
for(int i=0; i<10; i++)
{
double value;
cout << "Enter rain fall value for "<<(i+1)<< " : ";
cin >> value;
cout << endl;
new_stats.storeVALUE(value);
}
cout << "Total of rain fall values are " << new_stats.total() << endl;
cout << "Lowest of rain fall values are " << new_stats.lowest() << endl;
cout << "Highest of rain fall values are " << new_stats.highest() << endl;
cout << "Average of rain fall values are " << new_stats.average() << endl;
return 0;
}