main.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8. #define CELLS_COUNT 3000
  9. //#define EVENTS_MULTIPLIER 6
  10. #if CELLS_COUNT > RAND_MAX
  11. # error CELLS_COUNT > RAND_MAX
  12. #endif
  13. int main(int argc, char *argv[]) {
  14. if (argc <= 1) {
  15. fprintf(stderr, "syntax: test events_multiplier\n");
  16. return -1;
  17. }
  18. int events_multiplier = atoi(argv[1]);
  19. long cells[CELLS_COUNT];
  20. memset(cells, 0, sizeof(cells));
  21. srand((unsigned) time(NULL) ^ getpid());
  22. for (int i = 0; i < CELLS_COUNT*events_multiplier; i++) {
  23. long value = rand() % CELLS_COUNT;
  24. cells[value]++;
  25. }
  26. long average = events_multiplier;
  27. long derivation = 0;
  28. for (int i = 0; i < CELLS_COUNT; i++) {
  29. derivation += fabs(cells[i] - average);
  30. printf("% 4li", cells[i]);
  31. if ((i+1) % 50 == 0) {
  32. printf("\n");
  33. }
  34. }
  35. double average_derivation = ((double)derivation) / CELLS_COUNT;
  36. printf("\n\naverage:\t\t\t%li\naverage derivation:\t\t%lf\naverage relative derivation:\t%lf\n", average, average_derivation, average_derivation / average);
  37. return 0;
  38. }