Advertisement
Kl43z

Moda/Media/Mediana C

Nov 26th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. #include <stdio.h>
  2.  #define SIZE 9
  3.  
  4. void media (const int resp[]);
  5. void mediana (int resp[]);
  6. void moda(int frec[], const int resp[]);
  7. void ordenamBurbuja(int a[]);
  8. void imprimeArreglo(const int a[]);
  9.  
  10. int main(){
  11.     int frecuencia[10]={0};
  12.     int respuesta[SIZE]={0};
  13.     int i;
  14.     for(i=0;i<SIZE;i++){
  15.         printf("Ingrese el %d° valor: ", i+1);
  16.         scanf("%d", &respuesta[i]);
  17.     }
  18.     media(respuesta);
  19.     mediana(respuesta);
  20.     moda(frecuencia, respuesta);
  21.     return 0;  
  22. }
  23.  
  24. void media (const int resp[]){
  25.     int j;
  26.     int total=0;
  27.     printf("%s\n%s\n%s\n", "********", "   Media", "********");
  28.     for (j=0;j<SIZE;j++){
  29.         total+=resp[j];
  30.     }
  31.     printf ("La media corresponde a : %d\n", total/SIZE);
  32. }
  33.  
  34. void mediana (int resp[]){
  35.     printf("\n%s\n%s\n%s\n%s", "********", " Mediana", "********", "El arreglo de respuestas desordenado es");
  36.     imprimeArreglo(resp);
  37.     ordenamBurbuja(resp);
  38.     printf ("\n\nEl arreglo ordenado es ");
  39.     imprimeArreglo(resp);
  40.     printf ("\nLa mediana es el elemento N°%d del arreglo.\n",SIZE/2);
  41.     printf ("En este caso corresponde a: %d\n", resp[SIZE/2]);
  42. }
  43.  
  44. void moda (int frec[], const int resp[]){
  45.     int rango, j, h, masGrande=0, valorModa=0;
  46.     printf ("\n%s\n%s\n%s\n", "********","    Moda", "********");
  47.     for (rango=1;rango<=9;rango++){
  48.         frec[rango]=0;
  49.     }
  50.     for (j=0;j<SIZE;j++){
  51.         ++frec[resp[j]];
  52.     }
  53.     printf ("%s%11s%19s\n\n", "Respuesta", "Frecuencia","Histograma");
  54.     for(rango=1;rango<=9;rango++){
  55.         printf ("%8d%11d            ", rango, frec[rango]);
  56.         if (frec[rango]>masGrande){
  57.             masGrande=frec[rango];
  58.             valorModa=rango;
  59.         }
  60.         for (h=1;h<=frec[rango];h++){
  61.             printf ("*");
  62.         }
  63.         printf ("\n");
  64.     }
  65.     printf ("La moda corresponde a: %d\n", valorModa);
  66.     printf ("Dicho valor se repitió %d veces.\n", masGrande);
  67. }
  68.  
  69. void ordenamBurbuja(int a[]){
  70.     int pasada, j, almacena;
  71.     for (pasada=1;pasada<SIZE;pasada++){
  72.         for (j=0;j<SIZE-1;j++){
  73.             if (a[j]>a[j+1]){
  74.                 almacena=a[j];
  75.                 a[j]=a[j+1];
  76.                 a[j+1]=almacena;
  77.             }
  78.         }
  79.     }
  80. }
  81.  
  82. void imprimeArreglo(const int a[]){
  83.     int j;
  84.     for (j=0;j<SIZE;j++){
  85.         if(j%20==0){ //comienza nueva linea cada 20 valores
  86.             printf ("\n");
  87.         }
  88.         printf (" %d", a[j]);
  89.     }  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement