Design and create a class named Stats that has an array of 12 doubles as one of
ID: 3625883 • Letter: D
Question
Design and create a class named Stats that has an array of 12 doubles as one of its member variables. The values in the array should be set by making 12 calls to a public member function named setValue that accepts two arguments, an integer indicating which value is being provided (the first number, the second number….etc) and a double holding the actual data value.In addition to the setValue member function, the class should have the following additional member functions:
¨ A default constructor that sets all of the values in the array to zero
¨ A getValues function that displays all of the values in the array
¨ A getTotal function that returns the total of the 12 values in the array
¨ A getAvg function that returns the average of the 12 values in the array
¨ A getLargest function that returns the largest value in the array
¨ A getSmallest function that returns the smallest values in the array
Remember that the class specification/declaration (.h file) will contain the member variables and member function prototypes. Name the class declaration file Stats.h. The class implementation file (.cpp) will contain the function definitions (don’t forget to include the Stats.h file). Name the class implementation file Stats.cpp.
Next create a program that uses the Stats class to hold rainfall data and report annual rainfall statistics. The program should first create a Stats object named rainfall and call its setValue member function to set each of the 12 monthly rainfall totals to a user entered amount. It should then produce an annual rainfall report that shows the total, average, lowest and highest rainfall amounts for the year. Note that the printing of the rainfall report should be done in the client program file not in the Stats class.
Input Validation: If any amount less than 0 is passed into the setValue function, a default value of 0 should be used in his place.
The program utilizing the employee class should be in a separate .cpp file. Name this program rainfall.cpp.
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
class Stats{
private:
double data[12];
public:
Stats();
void getValues();
void setValues(int,double);
double getTotal();
double getAvg();
double getLargest();
double getSmallest();
};
Stats::Stats()
{int i;
for(i=0;i<12;i++)
data[i]=0;
}
void Stats::setValues(int i, double v)
{if(v<0)
v=0;
data[i]=v;
}
void Stats::getValues()
{cout<<"The data Month RainFall ";
int i;
for(i=0;i<12;i++)
cout<<i+1<<" "<<data[i]<<endl;
}
double Stats::getTotal()
{double total;
int i;
for(i=0;i<12;i++)
total+=data[i];
return total;
}
double Stats::getAvg()
{
return getTotal()/12.;
}
double Stats::getLargest()
{double m;
int i;
m=data[0];
for(i=1;i<12;i++)
if(data[i]>m)
m=data[i];
return m;
}
double Stats::getSmallest()
{double m;
int i;
m=data[0];
for(i=1;i<12;i++)
if(data[i]<m)
m=data[i];
return m;
}
int main()
{Stats rainfall;
int i;
double d;
cout<<"Enter monthly data ";
for(i=0;i<12;i++)
{cout<<"Enter rainfall for month "<<i+1<<": ";
cin>>d;
rainfall.setValues(i,d);
}
cout<<"RAINFALL SUMMARY ";
rainfall.getValues();
cout<<"Total rainfall: "<<rainfall.getTotal()<<" inches"<<endl;
cout<<"Average rainfall: "<<rainfall.getAvg()<<" inches"<<endl;
cout<<"Largest rainfall: "<<rainfall.getLargest()<<" inches"<<endl;
cout<<"Smallest rainfall: "<<rainfall.getSmallest()<<" inches"<<endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.