Created
March 2, 2011 00:58
-
-
Save javier-lopez/850249 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************************** | |
* P.L. Francisco Javier <[email protected]> 210368224 | |
*********************************************************************/ | |
#include <stdio.h> /* printf, scanf, fflush, getchar, etc */ | |
#include <stdlib.h> /* system, rand, exit, qsort*/ | |
#include <math.h> /* sqrt, -lm */ | |
#ifdef _WIN32 | |
#include <windows.h> | |
#include <conio.h> | |
#define clear "cls" | |
#define sleep(x) Sleep((x)*1000) | |
#else | |
#include <unistd.h> | |
#define sleep(x) usleep((x)*1000000) | |
#define clear "clear" | |
#endif | |
#define ALGUNOS 1000 | |
/* funcion callback para qsort*/ | |
int compara_flotantes(const void *a, const void *b) | |
{ | |
if(*(const float*)a < *(const float*)b) | |
return -1; | |
return *(const float*)a > *(const float*)b; | |
/* negativo si b>a o positivo si a>b */ | |
} | |
void imprimir_numeros(const float *numeros, int cantidad) | |
{ | |
int i; | |
for(i=0; i<cantidad; i++) | |
printf("| %6.2f | ",numeros[i]); | |
printf("\n"); | |
} | |
float *ordenar(float numeros[], int cantidad) | |
{ | |
qsort(numeros, cantidad, sizeof(float), compara_flotantes); | |
return numeros; | |
} | |
float mediana_fun (float numeros[], int cantidad) | |
{ | |
int i; | |
float mediana=0; | |
if (cantidad%2==0) | |
{//par | |
i=(cantidad/2)+1; | |
mediana=numeros[i]; | |
} | |
else | |
{//impar | |
i=cantidad/2; | |
mediana=(numeros[i]+numeros[i+1])/2; | |
} | |
return mediana; | |
} | |
float media_fun (float numeros[], int cantidad) | |
{ | |
int i; float media=0; | |
for (i = 0; i < cantidad; i++) | |
{ | |
media+=numeros[i]; | |
} | |
return media/i; | |
} | |
int moda_fun (float numeros[], int cantidad) | |
{ | |
int i,j; | |
float tmp[cantidad], tmp_[cantidad], moda=0, *max; | |
for (i = 0; i < cantidad; i++) | |
{ | |
tmp[i]=0; //se limpian las variables | |
tmp_[i]=0; | |
} | |
for (i = 0; i < cantidad+1; i++) | |
{//+1 necesario para probar con el ultimo digito | |
for (j = 0; j < cantidad; j++) | |
{ | |
if (numeros[i]==numeros[j]) | |
{ | |
tmp[i]+=1; | |
tmp_[i]+=1; | |
} | |
} | |
} | |
max=ordenar(tmp_, cantidad); | |
moda=max[cantidad-1]; | |
for (i = 0; i < cantidad; i++) | |
{ | |
if(moda==tmp[i]) | |
{ | |
moda=numeros[i]; | |
break; | |
} | |
} | |
return moda; | |
} | |
float varianza_fun (float numeros[], int cantidad) | |
{ | |
float media=media_fun(numeros, cantidad); | |
float varianza=0, sumatoria_cuadrados=0; | |
int i; | |
for (i = 0; i < cantidad; i++) | |
{ | |
sumatoria_cuadrados+=numeros[i]*numeros[i]; | |
} | |
varianza=(sumatoria_cuadrados-media)/cantidad; | |
return varianza; //s² | |
} | |
float desviacion_standard_fun (float numeros[], int cantidad) | |
{ | |
float media=media_fun(numeros, cantidad); | |
float desviacion_standard=0, sumatoria_cuadrados=0; | |
int i; | |
for (i = 0; i < cantidad; i++) | |
{ | |
sumatoria_cuadrados+=(numeros[i]-media)*(numeros[i]-media); | |
} | |
desviacion_standard=sqrt((sumatoria_cuadrados/5)); | |
return desviacion_standard; | |
} | |
int main(int argc, char const* argv[]) | |
{ | |
system("clear"); float numeros[ALGUNOS]; int i; | |
printf("Calculo de la media, moda, mediana, varianza y desviacion standard, introduzca digitos (-1, cuando sean suficientes):\n\n\t[1] > "); | |
for (i = 0; i < ALGUNOS; i++) | |
{ | |
scanf("%f", &numeros[i]); | |
if (numeros[i]==-1) | |
break; | |
printf("\t[%d] > ", (i+2)); | |
} | |
if (i!=0) printf("\n\n"); | |
float *numeros_ordenados, mediana=0, media=0; | |
float moda=0, varianza=0, desviacion_standard=0; | |
int cantidad=i; | |
numeros_ordenados=ordenar(numeros, cantidad); | |
system("clear");printf("Dump >>> ");imprimir_numeros(numeros_ordenados, cantidad);printf("\n"); | |
media=media_fun(numeros, cantidad); | |
mediana=mediana_fun(numeros, cantidad); | |
moda=moda_fun(numeros, cantidad); | |
varianza=varianza_fun(numeros, cantidad); | |
desviacion_standard=desviacion_standard_fun(numeros, cantidad); | |
printf("######################################################\n\n"); | |
printf("La media es\t\t -> %6.3f\n", media); | |
printf("La mediana corresponde a -> %6.3f\n", mediana); | |
printf("La moda es\t\t -> %6.3f\n", moda); | |
printf("La varianza es\t\t -> %6.3f\n", varianza); | |
printf("La desviacion estandard es -> %6.3f\n\n", desviacion_standard); | |
printf("######################################################\n\n"); | |
sleep(5); | |
fflush(stdout); | |
if (ferror(stdout)) | |
exit(EXIT_FAILURE); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment