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

using c++ The class averager is designed to accept integers, and at any point re

ID: 3828740 • Letter: U

Question

using c++

The class averager is designed to accept integers, and at any point return the average of the numbers entered until that point. The class contains the following: An integer data member name total, containing the sum of the numbers added so far An integer data member name count, containing the number of the numbers added so far A constructor that initialize total and count to 0 A function, add, that has a single integer parameter(and no return value). The value of the parameter is added to total (and count is suitably modified) A function, getAverage, that returns the average of the numbers entered so far. If no numbers have been entered(i.e., count equals 0), throw exception "illegal average-empty sequence" A function reset that resets the total and count to 0 An

Explanation / Answer

code:

#include<cstdio>
#include<string.h>
#include<cstdlib>
#include<iostream>
using namespace std;

class averager
{
public:
   int total,count;
   averager()
   {
       total=0;
       count=0;
   }
   int add(int no)
   {
       total+=no;
       count++;
   }
   float get_average()
   {
return float(total)/float(count);
   }
};


int main ( int argc, char *argv[] )
{
   averager av;
   int i=1;
   for(i=1;i<=10;i++)
       av.add(i);
   cout<<av.get_average();
return 0;
}