I need help in calculating the variance for randomly generated values (using srand(seed)). When i input 5 i should get 97359589.15265 but i am getting 1.29888e+010. I am almost positive i went wrong in the calculation of the variance but have no idea what to do in making it correct. Any help or suggestions would greatly be appreciated. (My code thus far):

``````#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

int main (){
double getMean(int a[], int n);
double getVariance(int a[], int n);
const int N = 50;
unsigned int seed;
int arr[N];

cout << " Enter a positive integer seed value: \n";
cin >> seed;

srand(seed);

for (int k=0; k<N; k++)
{
arr[k] = rand();
}

cout << endl << " The Mean of the 50 random numbers is: " << getMean(arr, N)<< endl;
cout << endl << " The Variance of the 50 random numbers is: " << getVariance(arr, N)<< endl;
system ("PAUSE");
return 0;
}

double getMean(int a[], int n)
{
double sum(0);
for (int i=0; i<n; i++)
sum +=a[i];

return sum/n;
}

double getVariance(int a[], int n)
{
double sum1(0);
for (int i=0; i<n; i++)
sum1 +=a[i];

return ((pow((sum1-(sum1/n)),2))/n);
}``````

I think it's your formula. When I did it old school style by finding the mean and summing the square of (each element - mean), and dividing that sum by n, I got 9.54124 * 10^7, which is closer to your goal.

If you want to use the formula where you don't have to do it twice, find the mean of the squares and subtract the square of the mean (see Wikipedia).