Skip to content

Instantly share code, notes, and snippets.

@kaldas
Created October 6, 2010 06:53
Show Gist options
  • Select an option

  • Save kaldas/612923 to your computer and use it in GitHub Desktop.

Select an option

Save kaldas/612923 to your computer and use it in GitHub Desktop.
/*******************************************
Processo e Desenvolvimento de Software
Aluno: rodrigo caldas de moura duarte
gcc test1.c -o test1 -lm;./test1
*******************************************/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
/* retorna desvio padrao */
double stdev(int buff[], int bfsize)
{
int cnt = 0;
double aqr = 0, mean = 0, dif = 0;
for(cnt=0;cnt != bfsize;cnt++)
mean += buff[cnt];
mean = mean / cnt;
for(cnt=0;cnt != bfsize;cnt++)
{
dif = buff[cnt] - mean;
aqr += (dif * dif);
}
aqr = (double) aqr / (bfsize - 1);
return sqrt(aqr);
}
/* exemplo de uso */
int main()
{
int *buff, bfsize = 0, cnt = 0;
fprintf(stdout,"\nDe quantos elementos deseja tirar a media e o desvio? ");
scanf("%i",&bfsize);
buff = (int *) calloc(bfsize, sizeof(int));
if(!buff || bfsize == 0)
{
fprintf(stderr,"erro de memoria ou entrada nula\n");
return -1;
}
for(cnt = 0; cnt != bfsize; cnt++)
{
printf(" \nEntre com a [%i] entrada.. : ",cnt + 1);
scanf("%i",buff+cnt);
}
fprintf(stdout," Standard Variation: %f \n",stdev(buff,bfsize));
//system("pause"); //aff windows babaca
free(buff);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment