Advertisement
luizaspan

[CANZIAN - C] Random (gaussiana)

Nov 13th, 2015
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4.  
  5. #define n 30 // hst.length = n
  6. #define N 10000
  7.  
  8. int main(void)
  9. {
  10.     double hst[n]={0};
  11.     double dx = (double) 1/n; // 1/n devolve int!!!
  12.     double x;
  13.     int i,j;
  14.  
  15.     // for (j=0; j<n; j++)
  16.     //  hst[j]=0;
  17.  
  18.     for (i=0; i<N; i++) {
  19.         x = sqrt((double) rand()/RAND_MAX); // rand()/RAND_MAX devolve int!!!
  20.         // printf("%lf\n",x);
  21.         for (j=0; j<n; j++) {
  22.             if (x>=j*dx && x<(j+1)*dx) {
  23.                 hst[j]++;
  24.             }
  25.         }
  26.     }
  27.  
  28.     // printf("\n");
  29.  
  30.     for (j=0; j<n; j++) {
  31.         printf("%lf \n",hst[j]);
  32.     }
  33.  
  34.     // achando o máximo da distribuição:
  35.     double hmax = log(0); // pra ser menos infinito
  36.     for (i=0; i<n; i++) {
  37.         if (hst[i]>hmax) hmax = hst[i];
  38.     }
  39.  
  40.     // normalizando a distribuição (sendo o maior valor 100):
  41.     for (i=0; i<n; i++) {
  42.         hst[i] = hst[i]/hmax*100;
  43.     }
  44.  
  45.     // graficando a distribuição:
  46.     for (i=0; i<n; i++) {
  47.         for (j=0; j<hst[i]; j++) {
  48.             printf("o");
  49.         }
  50.         printf("\n");
  51.     }
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement