Variance help?

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
^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?
I was only asked to calculate variance, so I just went with the easiest one to execute.
Topic archived. No new replies allowed.