fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define N 26
  5. static char *s = "asdfasdagsdfa";
  6.  
  7. int main() {
  8. static int a[N];
  9. int n, i;
  10. double e, t;
  11. char *p;
  12. for (p = s, n = 0; *p; p++, n++)
  13. a[*p - 'a']++;
  14. e = 0.0;
  15. for (i = 0; i < N; i++)
  16. if (a[i]) {
  17. t = (double)a[i] / n;
  18. e += -t * log(t) / log(2.0);
  19. }
  20. printf("entroy: %f bits\n", e);
  21. return 0;
  22. }
  23. /* end */
  24.  
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
entroy: 2.199688 bits