// A dice calculator
// This program will show all possible ways that two dice can roll a number
// It will then tell the user what the chance is of rolling that number

#include <stdio.h>

int main (void) {
    int diceOneSize;
    int diceTwoSize;
    int targetValue;
    int numSuccesses = 0;
    double numRolls = 0;
    
    // User decides the two dice sizes
    printf("Please enter the size of the first die: ");
    scanf("%d", &diceOneSize);
    printf("Please enter the size of the second die: ");
    scanf("%d", &diceTwoSize);
    printf("Please enter the target value: ");
    scanf("%d", &targetValue);
    
    // loop through and output rolls with totals that match the target
    int counter1 = 1;
    while (counter1 <= diceOneSize) {
        int counter2 = 1;
        while (counter2 <= diceTwoSize) {
            numRolls++;
            int total = counter1 + counter2;
            if (total == targetValue) {
                printf("%d, %d. Total: %d\n", counter1, counter2, total);
                numSuccesses++;
            }
            counter2++;
        }
        counter1++;
    }
    
    // Calculate percentage chance of success
    double percentage = numSuccesses/numRolls * 100;
    printf("Percentage chance of getting your target number is: %lf\n",
            percentage);
    
    return 0;
}

Resource created Thursday 07 March 2019, 03:07:51 PM.

file: diceStats.c


Back to top

COMP1511 19T1 (Programming Fundamentals) is powered by WebCMS3
CRICOS Provider No. 00098G