//ForStatistics.cc
//Computes the sample mean and variance of N numbers input at the keyboard.
//N is specified by the user but must be 10 or fewer in this example.
#include
using namespace std;
int main()
{
int N=0;
float number, sum=0.0, sumSquares=0.0;
float mean, variance;
// Wait until the user inputs a number in correct range (1-10)
// Stay in loop if input is outside range
while(N<1 || N>10)
{
cout << "Number of entries (1-10) = ";
cin >> N;
}
// Execute loop N times
// Start with i=1 and increment on completion of loop.
// Exit loop when i = N+1;
for(int i=1; i<=N; i++ )
{
cout << "Input item number " << i << " = ";
cin >> number;
sum = sum + number;
sumSquares = sumSquares + number*number;
}
mean = sum/N;
variance = sumSquares/N - mean*mean;
cout << "The mean of the data is " << mean <<
" and the variance is " << variance << endl;
return 0;
}