Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

******Please write this program in C++ only, and post with output. Thanks!******

ID: 3862408 • Letter: #

Question

******Please write this program in C++ only, and post with output. Thanks!******

Called statistician initialized, it can be given a sequence of double numbers. Each number in the sequence is function called next_number delcare a statistician called s sequence of numbers 1.1, -2.4, 0.8 as shown here: statistician s;

s.next_number(1.1);

s.next_number(-2.4);

s.next_number(0.8);

After a sequence has been given to a statistician, about the sequence. Include member functions that will provide the length of the sequence, the last number of the sequence, the sumof all numbers in the sequence, the arithmetic mean of the numbres (i.e., the sum of the numbers divided by the length of the sequence), the smallest number in the sequence, and the largest number in the sequence. Notice that the lnegth 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.

You should also provide a member function that erases the sequence (so that the statistician can start with a new sequence).

Notes: Do not start the entire sequence (because you don't know how long this sequence will be). Instead, just store the necessary information about the sequence: What is the sequence length? What is the sum of the numbers in the sequence? What are the last, smallest, and largest numbers? Each of these pieces of information can be stared in a private member variable that is updated whenever next_number is activated.

Explanation / Answer

#include <iostream>

using namespace std;

class Statistician{
    double* array;
    int size;
    int cap;
    public:
        Statistician();
        void next_number(double val);
        double getSum();
        double getMean();
        double getFirst();
        double getLast();
        double getLength();
        double getSmallest();
        double getLargest();
        void print();
};

Statistician::Statistician(){
    size=0; //default
    array = new double[10];
    cap = 10;
}
double Statistician::getSmallest(){
    double max = 999999.0;
    for(int i=0;i<size;i++){
        if(array[i]<max){
            max = array[i];
        }
    }
    return max;
}
double Statistician::getLargest(){
    double max = -999999.0;
    for(int i=0;i<size;i++){
        if(array[i]>max){
            max = array[i];
        }
    }
    return max;
}
double Statistician::getSum(){
    double t = 0.0;
    for(int i=0;i<size;i++){
        t += array[i];
    }
    return t;
}
double Statistician::getMean(){
    double t = 0.0;
    for(int i=0;i<size;i++){
        t += array[i];
    }
    return t/(double)size;
}
double Statistician::getLast(){
    return array[size-1];
}

double Statistician::getFirst(){
    return array[0];
}
double Statistician::getLength(){
    return size;
}
void Statistician::next_number(double val){
    if(size>=cap){
        double* temp = array;
        array = new double[size+10];
        for(int i=0;i<size;i++){
            array[i] = temp[i];
        }
        cap = size +10;
    }
    array[size] = val;
    size++;
}
void Statistician::print(){
    for(int i=0;i<size;i++){
        cout << array[i] <<endl;
    }
}
int main()
{
  
   Statistician s;
   s.next_number(1.2);
   s.next_number(0.11);
   s.next_number(-45.2);
   s.next_number(32.2);
   s.next_number(-0.34);
   s.next_number(0.2);
   s.next_number(3.2);
   s.next_number(10.2);
   s.print();
   double t = s.getSum();
   cout << "Sum : "<< t <<endl;
   double m = s.getMean();
   cout << "Mean : "<< m<<endl;
   cout << "getFirst : "<< s.getFirst()<<endl;
   cout << "getLast : "<< s.getLast()<<endl;
   cout << "getLargest : "<< s.getLargest()<<endl;
   cout << "getSmallest : "<< s.getSmallest()<<endl;
   return 0;
}