Calculate the standard deviation of a vector

I am not able to solve the following equation:

Equation

Here is the Code:

#include <stdio.h>
#include <math.h>

int main(){
    float m, media, sigma, p;
    int vetor[10];
    media = 0;
    m = 0;
    sigma = 0;
    p = 0;
    for(int i = 0; i < 10; i++){
        printf("Digite um número: ");
        scanf("%d", &vetor[i]);
    }
    for(int i = 0; i < 10; i++){
        m = m + vetor[i];
    }
    media = m / 10.0;
    for(int i = 0; i < 10; i++){
        p = p + (vetor[i] - media);
    }
    sigma = sqrt((p * 1)/10);
    printf("Resultado d = %.2f\n", sigma);
}
Author: Lacobus, 2017-10-30

3 answers

The standard deviation formula has to have the squared distance, which is not in yours. See this formula taken directly from wikipedia :

insert the description of the image here

Where (xi- x)² is raised to ²

In this formula i starts at 1 and goes to N which corresponds to yours that starts at 0 and goes to N-1, thus not affecting the calculations.

Applying this fix to your code:

int main(){
    ...
    media = m / 10.0;
    for(i = 0; i < 10; i++){
        p = p + pow(vetor[i] - media,2); //agora quadrado aqui utilizando a função pow
    }
    sigma = sqrt(p/(10-1)); //dividir por 10-1 que faltava, ou 9 se quiser simplificar
    printf("Resultado d = %.2f\n", sigma);

    return 0;
}

See the example in Ideone

 4
Author: Isac, 2017-10-30 10:42:33

The formula is wrong. You should take the sum of the variation of the mean squared .

Here is your revised and simplified program:

#include <stdio.h>
#include <math.h>

#define QTD_ELEMENTOS 5

int main() {
    int vetor[QTD_ELEMENTOS];

    for (int i = 0; i < QTD_ELEMENTOS; i++) {
        //printf("Digite um número: ");
        scanf("%d", &vetor[i]);
    }

    int somatorio = 0;
    for (int i = 0; i < QTD_ELEMENTOS; i++) {
        somatorio += vetor[i];
    }

    float media = somatorio / (float) QTD_ELEMENTOS;

    float variacoes = 0;
    for (int i = 0; i < QTD_ELEMENTOS; i++) {
        float v = vetor[i] - media;
        variacoes += v * v;
    }

    float sigma = sqrt(variacoes / QTD_ELEMENTOS);
    printf("Resultado d = %.2f\n", sigma);
}

See here working on ideone.

 3
Author: Victor Stafusa, 2017-10-30 03:26:24

Consider the following set containing 10 amostras:

{ 2, 3, 3, 4, 5, 6, 7, 8, 9, 10 }

First of all, we calculate the simple arithmetic mean of the samples of the set:

insert the description of the image here

Next, we calculate the deviation of all these samples from the mean:

insert the description of the image here

Thus, we Square the deviation of each sample from the mean:

insert the description of the image here

With this, we are able to calculate the variance :

insert the description of the image here

Calculate the standard deviation by extracting the square root of the variance:

insert the description of the image here

Follows a code capable of separately calculating the "mean", "variance" and "standard deviation"of a set of values:

#include <stdio.h>
#include <math.h>

#define MAXSIZE 10

double media( double s[], int n )
{
    double sum = 0.0;
    int i = 0;

    for( i = 0; i < n; i++ )
        sum += s[i];

    return sum / n;
}

double variancia( double s[], int n )
{
    double sum = 0.0;
    double dev = 0.0;
    double med = media( s, n );
    int i = 0;

    for( i = 0; i < n; i++ )
    {
        dev = s[i] - med;
        sum += (dev * dev);
    }

    return sum / n;
}

double desvio_padrao( double s[], int n  )
{
    double v = variancia( s, n );
    return sqrt( v );
}

int main( void )
{
    double vetor[ MAXSIZE ];
    int  i;

    for( i = 0; i < MAXSIZE; i++ )
    {
        printf("Digite um numero: ");
        scanf( "%lf", &vetor[i] );
    }

    printf("Media = %g\n", media( vetor, MAXSIZE ) );
    printf("Variancia = %g\n", variancia( vetor, MAXSIZE ) );
    printf("Desvio Padrao = %g\n", desvio_padrao( vetor, MAXSIZE ) );

    return 0;
}

Compiling:

$ gcc -lm desvio.c -o desvio

Heads:

Digite um numero: 2
Digite um numero: 3
Digite um numero: 3
Digite um numero: 4
Digite um numero: 5
Digite um numero: 6
Digite um numero: 7
Digite um numero: 8
Digite um numero: 9
Digite um numero: 10
Media = 5.7
Variancia = 6.81
Desvio Padrao = 2.6096
 2
Author: Lacobus, 2017-10-30 04:27:30