# statistic variable need help please

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

1. ### mochaNew Member

Joined:
Oct 24, 2010
Messages:
11
1
Trophy Points:
0
write the following class .Also provide a test file for the class(use separate compilation)​
Statistic class interface:​
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. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,009
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
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. ### mochaNew Member

Joined:
Oct 24, 2010
Messages:
11
1
Trophy Points:
0
Code:
```21)write the following class .Also provide a test file for the class(use separate compilation)

Statistic class interface:

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();
double avg(); //return the average of numbers
double stdev(); //return the standers deviation
double value();
private:
double numbers,sum,average,deviation,val;
int elements;
};
Statistics::Statistics()
{elements=0;
sum=0;
average=0;
deviation=0;
}
{sum+=elem;
numbers[elements]=elem;
elements++;
}
double Statistics::value()
{return val;
}
{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;
cout<<"Enter a value: ";
cin>>a;
cout<<"How many times do you want to add it? ";
cin>>n;