Dmitry Yu Okunev лет назад: 7
Сommit
aa7c62068f
3 измененных файлов с 52 добавлено и 0 удалено
  1. 1 0
      .gitignore
  2. 2 0
      Makefile
  3. 49 0
      main.c

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+test

+ 2 - 0
Makefile

@@ -0,0 +1,2 @@
+all:
+	$(CC) -std=gnu99 -lm *.c -o test

+ 49 - 0
main.c

@@ -0,0 +1,49 @@
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#define CELLS_COUNT 3000
+//#define EVENTS_MULTIPLIER 6
+
+#if CELLS_COUNT > RAND_MAX
+#	error CELLS_COUNT > RAND_MAX
+#endif
+
+int main(int argc, char *argv[]) {
+	if (argc <= 1) {
+		fprintf(stderr, "syntax: test events_multiplier\n");
+		return -1;
+	}
+
+	int events_multiplier = atoi(argv[1]);
+
+	long cells[CELLS_COUNT];
+	memset(cells, 0, sizeof(cells));
+
+	srand((unsigned) time(NULL));
+
+	for (int i = 0; i < CELLS_COUNT*events_multiplier; i++) {
+		long value = rand() % CELLS_COUNT;
+		cells[value]++;
+	}
+
+	long average    = events_multiplier;
+	long derivation = 0;
+
+	for (int i = 0; i < CELLS_COUNT; i++) {
+		derivation += fabs(cells[i] - average);
+		printf("% 4li", cells[i]);
+		if ((i+1) % 50 == 0) {
+			printf("\n");
+		}
+	}
+
+	double average_derivation = ((double)derivation) / CELLS_COUNT;
+
+	printf("\n\naverage:\t\t\t%li\naverage derivation:\t\t%lf\naverage relative derivation:\t%lf\n", average, average_derivation, average_derivation / average);
+
+	return 0;
+}
+