In: Computer Science
C++ please
Create a Stats class whose member data includes an array capable of storing 30 double data values, and whose member functions include total, average, lowest, and highest functions for returning information about the data to the client program. These are general versions of the same functions you created for Programming Challenge 7, but now they belong to the Stats class, not the application 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 this function to keep track of how many values are currently in the array, so it will know where to put the next value it receives and will know how many values there are to process when it is carrying out its other functions. It is also the job of this 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 same rainfall analysis requested by Programming Challenge 7. Notice that the Stats object does no I/O. All input and output is done by the client program.
#include <iostream>
#include <limits>
#include <random>
using namespace std;
class Stats
{
private:
int nextIndex; //this will maintain
where to insert next element in array
double highestValue; //this will maintain the highest value in
array
double lowestValue; //this will maintain the highest value in
array
double statValues[30]; //this will maintain the values in an array
form
public:
//This will be the constructor of this
class
Stats ()
{
this->nextIndex = 0;
this->highestValue = numeric_limits < double >::min
();
this->lowestValue = numeric_limits < double >::max ();
}
double highest () //this function will
return the highest value in the stats array
{
return highestValue;
}
double lowest () //this function will return the lowest value in the stats array
{
return lowestValue;
}
double total () //this function will return the total of values present in the stats array
{
double total = 0;
for (int i = 0; i < this->nextIndex; i++)
{
total = total + this->statValues[i];
}
return total;
}
double average () //this function will
return the average of values present in the stats array
{
double total = 0;
for (int i = 0; i < this->nextIndex; i++)
{
total = total + this->statValues[i];
}
return (total / (this->nextIndex + 1));
}
bool storeValue (double value) //this
function will insert a value in the stats array and return a
boolean status that was this job successfull.
{
if (this->nextIndex > 29)
return false;
if (value > highestValue) highestValue = value;
if (value < lowestValue) lowestValue = value;
this->statValues[this->nextIndex] = value;
this->nextIndex++;
return true;
}
};
/* The main function below is the client program*/
int main ()
{
Stats s;
uniform_real_distribution < double
>unif (-10000, 10000);
default_random_engine re;
for (int i = 0; i < 31; i++)
{
if (s.storeValue (unif (re)))
{
cout << "Yay!! a value stored in stats at index: " << i << endl;
}
else
{
cout << "OOPS!! index: " << i << " does not exist in stats" << endl;
}
}
cout << "highest: " <<
s.highest () << endl;
cout << "lowest: " << s.lowest () << endl;
cout << "total: " << s.total () << endl;
cout << "average: " << s.average () <<
endl;
return 0;
}
OUTPUT:-