Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   statistic variable need help please (http://www.go4expert.com/forums/statistic-variable-help-t23659/)

mocha 25Oct2010 03:04

statistic variable need help please
 
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?


xpi0t0s 25Oct2010 12:52

Re: statistic variable need help please
 
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.

mocha 26Oct2010 04:33

Re: statistic variable need help please
 
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


All times are GMT +5.5. The time now is 07:26.