//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 <iostream>
usingnamespace 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;
}
This code wants some numbers and adds them, divides to N and than calculates Variance.
#include <iostream> //iostream active
using namespace std; //use standart namespaces
int main() //i dont know but we always writing this
{
int N=0; //N is an integral and it is 0
float number, sum=0.0, sumSquares=0.0; //float is something and it means 0X0
float mean, variance; //float is 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) //N will be between 1 and 10
{
cout << "Number of entries (1-10) = ";
cin >> N; //enter value of 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++ ) //i is 1. If i is <= N than plus i to other i
{
cout << "Input item number " << i << " = ";
cin >> number; //enter the number
sum = sum + number;
sumSquares = sumSquares + number*number; //that must be something about variance
}
mean = sum/N; //mean is middle number
variance = sumSquares/N - mean*mean; //variance formule
cout << "The mean of the data is " << mean <<
" and the variance is " << variance << endl;