// Dice Statistics

// Finds all possible rolls of two theoretical dice.
// Checks how many combinations will results in a target number.
// Reports percentage chance of rolling that number.

// Marc Chee (cs1511@cse.unsw.edu.au), September 2020

#include <stdio.h>

int main(void) {
    // Take input from the user about the size of the two dice
    int dieOneSize = -55;
    int dieTwoSize = -55;
    int target = -55;
    
    printf("Please enter the size of the first die: ");
    scanf("%d", &dieOneSize);
    printf("Please enter the size of the second die: ");
    scanf("%d", &dieTwoSize);
    printf("Please enter the target value: ");
    scanf("%d", &target);
    
    int numRolls = 0;
    int numTargets = 0;
    
    // Loop through all values of both dice to get all combinations
    int dieOneValue = 1;
    while (dieOneValue <= dieOneSize) {
        // Loop through die two's values
        int dieTwoValue = 1;
        while (dieTwoValue <= dieTwoSize) {
            numRolls++;
            int total = dieOneValue + dieTwoValue;
            if (total == target) {
                numTargets++;
                printf("dieOne: %d, dieTwo: %d ", dieOneValue, dieTwoValue);
                printf("Total is %d\n", total);                
            }
            dieTwoValue++;
        }    
        dieOneValue++;
    }
    
    printf(
        "We have %d possible rolls. %d of them matched the target.\n", 
        numRolls, numTargets
    );
    
    // Calculate the percentage chance of rolling the target
    double chance = ((numTargets * 1.0)/numRolls) * 100;
    
    printf("Chance of rolling the target value is: %lf\n", chance);    
    
    return 0;
}

Resource created Friday 25 September 2020, 02:23:54 PM.

file: diceStats.c


Back to top

COMP1511 20T3 (Programming Fundamentals) is powered by WebCMS3
CRICOS Provider No. 00098G