Search code examples
csegmentation-faultdice

Segmentation fault error when finding the probability of the sum of 3 dice equaling an integer k


#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

TO DO: Implement a function named cum_prob below. This function takes an integer k, and a long integer trials as inputs. This function returns a double value. In the fucntion, we toss the 3 dice multiple times. The number of tosses is trials. We count the number of times that the outcomes of the 3 dice add up to at least k. And we then use this number and trials to calculate the probability that the sum of the 3 dice is at least k. Finally, we return this probablity.

double cum_prob(int k, long trials)
{   
    double count = 0;
    double all_trials = 0;
    double prob;
    if (trials == 0)
        return prob;
    if (rand() % 18 + 3 == k)
{
        (count ++);
        (all_trials ++);
        return cum_prob(k, -- trials);
}
    else
        {
            (all_trials ++);
            return cum_prob(k, -- trials);
        }
    prob = ((count / all_trials) * 100);


}


//Do not change the following code.
int main()

    long n = 10000000;
    int k;

    printf("Enter k :");    
    scanf("%d", &k);
    assert(k>= 3 && k<=18);
    srand(12345);
    printf("P(sum of the 3 dice is at least %d) = %.5lf\n", k, cum_prob(k, n));
    return 0;

}

Solution

  • the following proposed code:

    1. cleanly compiles
    2. performs the desired functionality
    3. incorporates a fixed table (calculated in the code)
    4. does not use recursion nor long loops

    and now, the proposed code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    
    
    double cum_prob(int targetValue, long trials)
    {
        (void)trials;
    
        int possibleResults[19] = {0};
    
        // build table
        for( int dice1 = 1; dice1 <=6; dice1++ )
        {
            for( int dice2 = 2; dice2 <= 6; dice2++ )
            {
                for( int dice3 = 1; dice3 <= 6; dice3++ )
                {
                    possibleResults[ dice1+dice2+dice3 ]++;
                }
            }    
        }
    
        // calculate total possible 
        double totalPossibilites = 0.0;
        for( int index=0; index<=18; index++ )
        {    
            totalPossibilites += (double)possibleResults[index];
        }
    
        int sumLessEqualTarget = 0;
        for( int i = 0; i<targetValue; i++ )
        {
            sumLessEqualTarget += possibleResults[i];
        }
    
    
        return  100.0 - ((double)sumLessEqualTarget / totalPossibilites) *100.0;
    }
    
    
    //Do not change the following code   
    int main()
    {
        long n = 10000000;
        //int k;
    
        //printf("Enter k :");    
        //scanf("%d", &k);
        //assert(k>= 3 && k<=18);
        //srand(12345);
        for( int i = 3; i<=18; i++ )
        {
            printf( "%d\n", i );
            printf("P(sum of the 3 dice is at least %d) = %.5lf\n\n", i, cum_prob(i, n));
        }
        return 0;
    }
    

    and here are the results for the range of valid inputs

    3
    P(sum of the 3 dice is at least 3) = 100.00000
    
    4
    P(sum of the 3 dice is at least 4) = 100.00000
    
    5
    P(sum of the 3 dice is at least 5) = 99.44444
    
    6
    P(sum of the 3 dice is at least 6) = 97.77778
    
    7
    P(sum of the 3 dice is at least 7) = 94.44444
    
    8
    P(sum of the 3 dice is at least 8) = 88.88889
    
    9
    P(sum of the 3 dice is at least 9) = 80.55556
    
    10
    P(sum of the 3 dice is at least 10) = 69.44444
    
    11
    Enter k P(sum of the 3 dice is at least 11) = 56.66667
    
    12
    P(sum of the 3 dice is at least 12) = 43.33333
    
    13
    P(sum of the 3 dice is at least 13) = 30.55556
    
    14
    P(sum of the 3 dice is at least 14) = 19.44444
    
    15
    P(sum of the 3 dice is at least 15) = 11.11111
    
    16
    P(sum of the 3 dice is at least 16) = 5.55556
    
    17
    P(sum of the 3 dice is at least 17) = 2.22222
    
    18
    P(sum of the 3 dice is at least 18) = 0.55556
    

    you can use the original source for the main() function, so it only calculates a single user entered value at each run of the code.