statistic variable need help please

Discussion in 'C' started by mocha, Oct 24, 2010.

  1. mocha

    mocha New Member

    Joined:
    Oct 24, 2010
    Messages:
    11
    Likes Received:
    1
    Trophy Points:
    0
    write the following class .Also provide a test file for the class(use separate compilation)​
    Statistic class interface:​
    Void add(double elem) //adds a number to the current sum​
    Void add(double elem,int count) // add a number count times​
    Double avg() //return the average of numbers​
    Double stdev() //return the standers deviation​
    Provide other member function as needed. Overload the operator<<and operator>> to read and write the statistic variable. The operator <<will print both the average and standard deviation to an output stream. The operator >> will read on element from the input stream.​
    I don't know how can I solve this kind of problem?

     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Void add(double elem) //adds a number to the current sum
    I don't know how can I solve this kind of problem?

    You can't, it's impossible. In C and C++ you have to pass by reference if you want to modify a parameter, and that isn't a pass by reference. If this is the interface you've been given then it's wrong.
     
  3. mocha

    mocha New Member

    Joined:
    Oct 24, 2010
    Messages:
    11
    Likes Received:
    1
    Trophy Points:
    0
    Code:
    21)write the following class .Also provide a test file for the class(use separate compilation)
    
    Statistic class interface:
    
    	Void add(double elem)  		 //adds a number to the current sum
    	Void add(double elem,int count)	// add a number count times
    	Double avg()		  //return the average of numbers
    	Double stdev()     	 //return the standers deviation
    
    
    
    
    
    
    #include <iostream>
    #include <cmath>
    using namespace std;
    class Statistics
    {
    friend ostream &operator<<( ostream &, const Statistics & );
    friend istream &operator>>( istream &, Statistics & );
    public:
    Statistics();
    void add(double elem); //adds a number to the current sum
    void add(double elem,int count); // add a number count times
    double avg(); //return the average of numbers
    double stdev(); //return the standers deviation
    double value();
    private:
    double numbers[50],sum,average,deviation,val;
    int elements;
    };
    Statistics::Statistics()
    {elements=0;
    sum=0;
    average=0;
    deviation=0;
    }
    void Statistics::add(double elem) //adds a number to the current sum
    {sum+=elem;
    numbers[elements]=elem;
    elements++;
    }
    double Statistics::value()
    {return val;
    }
    void Statistics::add(double elem,int count) // add a number count times
    {int i;
    sum+=(elem*count);
    for(i=0;i<count;i++)
       {numbers[elements]=elem;
        elements++;
       }
    }
    double Statistics::avg() //return the average of numbers
    {average= sum/elements;
    return average;
    }
    double Statistics::stdev() //return the standers deviation
    {int i;
    double tot=0;
    double mean;
    average=avg();
    for(i=0;i<elements;i++)
        tot+=(pow(numbers[i]-average,2.));
    mean=tot/elements;
    deviation=sqrt(mean);
    return deviation;
    }
    ostream& operator<<( ostream &output, const Statistics &num )
    {
    output <<"average= "<<num.average<<"\nstandard deviation= "<<num.deviation<<endl;;
    return output;
    }
    istream &operator>>( istream &input, Statistics &num )
    {
    input>>num.val;
    return input;
    }
    #include <iostream>
    using namespace std;
    int main()
    {Statistics a;
    int n;
    cout<<"Enter a value: ";
    cin>>a;
    a.add(a.value());
    cout<<"Enter a value: ";
    cin>>a;
    cout<<"How many times do you want to add it? ";
    cin>>n;
    a.add(a.value(),n);
    a.stdev();
    cout<<a;
    system("pause");
    }
     
    
    
    some on in my class did it :0
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice