Problem Coding Variance?!

Ok so my code compiles and runs, but the output of my variance is wrong. I'm brand new to coding and not sure how to code it correctly. For those who are not sure of the formula for variacne it goes as follows...

If the size of the set is 7 and the set is represented by X[0], X[1], X[2],
X[3],X[4], X[5], X[6], then the (biased) variance may be computed as:

variance = [(X[0]- mean)^2 +(X[1]-mean)^2 + ... +(X[6]- mean)^2]=7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// This program calculates the sum, average, variance, and standard deviation of any numbers.
// Using the for structure
using namespace std;
#include <iostream>
#include <math.h>
 
int main()
{
   int n, count;
   double x, sum, avg, var, sd;
 
   sum = 0;
   cout << "How many numbers?  ";
   cin >> n;
   int array[n];
   
   for(count=0; count<n; count++){
       cout << "Enter Number:  "; //Changed prompt so it was more user friendly.
         cin >> array[n];
        sum = sum + array[n];
   }  //end for

   for (count=1; count<=n; count++){
       var = pow(array[n]-avg,2)/ (n);
   }

   avg = sum / n;
   sd= sqrt(var);
   cout << "The sum is " << sum << endl;
   cout << "The average is  " << avg << endl;
   cout << "The variance is  " << var << endl;
   cout << "The standard deviation is  " << sd << endl;
   system("pause"); //Used so I can ready the outputs.
   return 0;   //successful termination

 }//end main 
var = pow(array[n]-avg,2)/ (n);
1) Why is / (n) here? it isn't in your formula. If you mistyped and there is /7 (instead of =7) then your code should be:
1
2
3
4
5
var = 0;
   for (count=0; count<n; count++){
       var += pow(array[n]-avg,2);
   }
   var /= n;
Topic archived. No new replies allowed.