// 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

// Marc Chee (marc.chee@unsw.edu.au), last modified June 2019

#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 die1 = 1;
    while (die1 <= diceOneSize) {
        int die2 = 1;
        while (die2 <= diceTwoSize) {
            numRolls++;
            int total = die1 + die2;
            if (total == targetValue) {
                printf("%d, %d. Total: %d\n", die1, die2, total);
                numSuccesses++;
            }
            die2++;
        }
        die1++;
    }
    
    // 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 Tuesday 18 June 2019, 01:55:52 AM, last modified Tuesday 18 June 2019, 08:56:59 PM.

file: diceStats.c


Back to top

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