-5

I have problem with my variance when i put 2,5,1,3 numbers result is 3,46 but correct is 2,18

double result=0;
string x1 = textBox1.Text;
string[] tab = x1.Split(',');
int n = tab.Length;
double sum = 0;
double ob = 0;

for (int i = 0; i < n; i++)
{
    sum = sum + double.Parse(tab[i]);
    result = sum / n;
    ob = (ob + Math.Pow((double.Parse(tab[i]) - result),2)) / n;

}

textBox2.Text = ob.ToString();
BWA
  • 5,672
  • 7
  • 34
  • 45
Ops
  • 115
  • 2
  • 9

1 Answers1

1

The formula for the standard variance is:

enter image description here

As you can see, you have to divide by n after you calculated the sum. But your division is in the loop. If you will perform this operation at the outsite of the loop, you will recieve the correct result.