/* calc_dice_average.c - a program that calculates the average of a roll of dice in which a certain number of the minimal dice values are omitted. Programmed by Shlomi Fish, 1997 Part of Math-Ventures' article: "Combinatorics and the Art of Dungeons and Dragons". */ #include #include #include #define MAX_DICE 100 /* A function that compares two integers. Used by qsort() */ int int_compare(const void * a, const void * b) { if (*(const int *)a > *(const int *)b) return 1; else if (*(const int *)a < *(const int *)b) return -1; else return 0; } int main() { int a, b; unsigned dice[MAX_DICE]; unsigned sorted_dice[MAX_DICE]; unsigned num_dice, max_die_value, dice_to_remove; unsigned long grand_total; /* Input the values of the parameters */ printf("Please enter the number of sides that every die has:\n"); scanf("%u", &max_die_value); printf("Please enter the number of dice that are thrown:\n"); scanf("%u", &num_dice); printf("Please enter the number of dice with minimal values" " to be removed from a throw total:\n"); scanf("%u", &dice_to_remove); /* Some verifications */ if (num_dice > MAX_DICE) { fprintf(stderr, "Sorry, but the number of dice cannot exceed %u!\n", MAX_DICE); return -1; } if (dice_to_remove > num_dice) { fprintf(stderr, "Sorry, but the number of dice to remove cannot be greater that the number of dice thrown!\n"); return -1; } /* Initiliaze the dice to the first permutation */ for(a=0;a