Design and implement a class called statistician. After a statistician is initia
ID: 3784072 • Letter: D
Question
Design and implement a class called statistician. After a statistician is initialized, it can be given a sequence of double numbers by invoking a member function next. For example, we can declare a statistician called s, and then give it the sequence of numbers 1.1, -2.4, 0.8 as shown here: statistician s; s.next(1.1); s.next(-2.4); s.next(0.8); After a sequence has been given to a statistician, there are various member functions to obtain information about the sequence. Include member functions that will provide the length of the sequence, the sum of all the numbers in the sequence, the arithmetic mean of the numbers, the smallest number in the sequence, and the largest number in the sequence. Notice that the length and sum functions can be called at any time, even if there are no numbers in the sequence. In this case of an “empty” sequence, both length and sum will be zero. But the other member functions all have a precondition requiring that the sequence is non-empty. Please implement stats.cpp given the following stats.h header file
#ifndef STATS_H
#define STATS_H
class statistician
{
public:
// CONSTRUCTOR
statistician( );
// MEMBER FUNCTIONS
void next(double r);
// CONSTANT MEMBER FUNCTIONS
int length( ) const { return count; }
double sum( ) const { return total; }
double mean( ) const;
double minimum( ) const;
double maximum( ) const;
private:
int count; // How many numbers in the sequence
double total; // The sum of all the numbers in the sequence
double tiniest; // The smallest number in the sequence
double largest; // The largest number in the sequence
};
}
#endif
Please help. I haven't done a programming assignment in many quarters so I'm very rusty and having a hard time understanding where to start. Thanks!
Explanation / Answer
#include"stats.h"
stats:: int n
stats::double arr[n];
stats::mean=0.0;
stats::sum=0.0;
stats::int k=0;
stats::statistician( ){
cout<<"Enter the number of entries:";
cin>>n;
}
stats::next(double r){
arr[k]=r;
k++;
}
stats::length( ) const {
count=k;
return count;
}
stats::sum( ) const {
for(int i=0;i<k;i++)
{
sum+=arr[i];
}
total=sum;
return total; }
stats:: mean( ) const
{
if(k==0){
break;
}
for(int i=0;i<k;i++)
{
sum+=arr[i];
}
mean=sum/k;
return mean;
}
stats:: minimum( ) const{
if(k==0){
break;
}
int min=arr[0]
for(int i=0;i<k;i++)
{
if(min>arr[i]){
min=arr[i];
}
}
return min;
}
stats::maximum( ) const{
if(k==0){
break;
}
int max=arr[0]
for(int i=0;i<k;i++)
{
if(max<arr[i]){
max=arr[i];
}
}
return max;
}
int main()
{
Statistician s;
s.next(1.1);
s.next(2.4);
s.next(0.8);
cout<<s.length();
cout<<s.sum();
cout<<s.mean();
cout<<s.minimum();
cout<<s.maximum();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.