Variance help?

Sep 25, 2015 at 2:57pm
Hey all! I'm trying to write a program that will give me the variance of a set of numbers in an array. The variance is part of a larger program, so I'll just give you the variance function.

n = array size
sorted[26] = array with ascending sorted list of numbers
Also, i = 1 out of preference, I know a lot of people use i = 0

1
2
3
4
5
6
7
8
9
10
11
  void getVariance(int sorted[26], int n){
	int i, variance, sum = 0;

	for (i = 1; i <= n; i++){
		sum = (sorted[i] ^ 2) + sum;
	}
	
	variance = sum / n;

	cout << "Variance: " << variance << endl;
}
Last edited on Sep 25, 2015 at 2:58pm
Sep 25, 2015 at 3:03pm
^2 doesn't mean what you think. Just write sorted[i] * sorted[i].
Additionally, note that you're calculating an integer variance. Are you sure that's what you want?
Sep 25, 2015 at 6:01pm
I was only asked to calculate variance, so I just went with the easiest one to execute.
Topic archived. No new replies allowed.