Mathematical Error I Can't Find

I have written this code to find the standard deviation of the numbers in a partially filled array. There must be something wrong with my math, because when I input a list of integers such as 1, 2, 3, 5, 8, I get the result of 6.17206e+17 for the standard deviation. Can anyone tell me where I went wrong in the code?


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
37
38
39
40
41
  
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    int digits[10], x, input, i = 0;
    double ave, sum = 0, sum2 = 0, foo[i], standard_deviation;
    for(x = 0; x < 10; ++x)
    {
        cin >> input;
        if(input != -1)
            digits[x] = input;
        else
            break;
    }
    
    for(i = 0; i < x; ++i)
        cout << digits[i] << " ";
    
    cout << endl;
    
    for (i = 0; i < x; i++)
        sum += digits[x];
    ave = sum / x;
    
    
    for (i = 0; i < x; i++)
        foo[i] = pow ((digits[i] - ave), 2);
    
    
    for (i = 0; i < x; i++)
        sum2 += foo[i];
    standard_deviation = sum2 / i;
    

    
    cout << standard_deviation;
    
    return 0;
}
double ave, sum = 0, sum2 = 0, foo[i]

I guess you meant:
double ave, sum = 0, sum2 = 0, foo[10]

Also, there are places in your loops, where there is confusion regarding the loop variable. (i vs x for example)
Topic archived. No new replies allowed.