Kajetan Johannes Hammerle 3 years ago
commit
9fac947586
3 changed files with 66 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 58 0
      Main.c
  3. 7 0
      Makefile

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+dice_variation

+ 58 - 0
Main.c

@@ -0,0 +1,58 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int* numbers = NULL;
+int dices = 0;
+
+void calc(int diceSum, int leftDices) {
+    if(leftDices <= 0) {
+        numbers[diceSum - dices]++;
+        return;
+    }
+    for(int i = 1; i <= 6; i++) {
+        calc(diceSum + i, leftDices - 1);
+    }
+}
+
+int main(int argAmount, const char** args) {
+    if(argAmount < 3) {
+        if(argAmount > 0) {
+            printf("%s <dices> <print_width>\n", args[0]);
+        } else {
+            puts("... <dices> <print_width>");
+        }
+        return 0;
+    }
+    dices = atoi(args[1]);
+    int width = atoi(args[2]);
+    if(dices < 1 || dices > 10) {
+        puts("dices must be in the range [1, 10]");
+        return 0;
+    } else if(width < 1 || width > 200) {
+        puts("print width must be in the range [1, 200]");
+        return 0;
+    }
+
+    int range = 6 * dices - dices + 1;
+    numbers = calloc(range, sizeof(int));
+    calc(0, dices);
+    int max = 0;
+    for(int i = 0; i < range; i++) {
+        if(numbers[i] > max) {
+            max = numbers[i];
+        }
+    }
+
+    const char* format = dices == 1 ? "%d: " : "%2d: ";
+    for(int i = 0; i < range; i++) {
+        printf(format, i + dices);
+        int chars = (numbers[i] * width) / max;
+        for(int k = 0; k < chars; k++) {
+            putchar('X');
+        }
+        putchar('\n');
+    }
+
+    free(numbers);
+    return 0;
+}

+ 7 - 0
Makefile

@@ -0,0 +1,7 @@
+all: dice_variation
+	
+dice_variation: Main.c
+	gcc -o $@ Main.c -Wall -Wextra -Werror -pedantic -O3
+	
+clean:
+	rm -f dice_variation