Question

//In this assignment, we implement a function that simulates the outcome of tossing of 3 dice....

//In this assignment, we implement a function that simulates the outcome of tossing of 3 dice.
//We will learn how to generate random numbers, and how to seed a random number generator.
//For example, we use the following code to generate a random number between 1 and 6.
// rand() % 6 + 1
//The following code set the seed of the random number generator to 12345.
// srand(12345);

#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.

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

Homework Answers

Answer #1

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

#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.

//Do not change the following code.
double cum_prob(int k,long n)
{
long i=0;
long ct=0;
for(i=0;i<n;i++)
{
int a=rand()%6+1;
int b=rand()%6+1;
int c=rand()%6+1;
if(a+b+c>=k)
{
ct++;
}
}
return ((double)ct/(double)n);
}
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;
}

Kindly revert for any queries

Thanks.

​​​​​​​

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void Ge
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void GenerateFromArray(void); /************************************************************************************/ /************************************************************************************/ int main(void) { /* This is the main() program. It should call the functions ScanArray() and GenerateFromArray() */ GenerateFromArray(); system("pause"); return 0; } /*************************************************************************************** Define this function to scan the elements of an array from the given data file "ArrayInp.dat". If the input file is not found, or contains invalid data, the function should return a 0 and print an error message. If the input...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from...
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from a file. Specifically, you will implement the following function: /* * Reads one arithmetic "expression" at a time from a file stream, computes, then * returns the result. If there are additional expressions in the file, they are * read and computed by successive calls to “calculator”. * * “Expressions” are groups of operations (add, subtract, multiply, divide). Your * calculator will read and...
Use CPP This question is about providing game logic for the game of craps we developed...
Use CPP This question is about providing game logic for the game of craps we developed its shell in class. THe cpp of the class is attached to this project. At the end of the game, you should ask user if wants to play another game, if so, make it happen. Otherwise quit. craps.cpp /*** This is an implementation of the famous 'Game of Chance' called 'craps'. It is a dice game. A player rolls 2 dice. Each die has...
Here are Dice Game which programed in C++ below. For this program, add a "cheater" function,...
Here are Dice Game which programed in C++ below. For this program, add a "cheater" function, which will prevent user from winning three times in the row (two times is OK, too). Also, add comments. #include #include using namespace std; int main() { int user,computer,sum,u_diff,c_diff,u_win=0,c_win=0; do { cout<<"User guess is(1 - 18) :"; cin>>user; if(user>=3&&user<=18) //the sum of three dice should be between 3 to 18 { cout<<"Computer guess is :"; computer=rand()%16+3; cout< cout<<"Sum of the three rolls is :";...
Write a function that passes an array argument, getRandomNumbers, to get a pointer to an array...
Write a function that passes an array argument, getRandomNumbers, to get a pointer to an array of random numbers in the array. The function dynamically allocates an array, uses the system clock to seed the random number generator, populates the array with random values, and then returns a pointer to the array. Function getRandomNumbers to generate a random array and return a pointer. int* getRandomNumbers(int num); // The parameter indicates the number of numbers requested. The algorithm can be described...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
We see that this computes the product of two matrices. Add a new kernel code, called...
We see that this computes the product of two matrices. Add a new kernel code, called sum, to compute the sum of the two matrices. #include <stdio.h> #include <math.h> #include <sys/time.h> #define TILE_WIDTH 2 #define WIDTH 6 // Kernel function execute by the device (GPU) __global__ void product (float *d_a, float *d_b, float *d_c, const int n) {    int col = blockIdx.x * blockDim.x + threadIdx.x ;    int row = blockIdx.y * blockDim.y + threadIdx.y ;    float...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers and sort it in-place. Write a program that generates random integer arrays (hint: use seed appropriately to avoid generating same sequences) of lengths 10, 100, 1000, 10,000, 100,000, 1000,000, and then sorts each using each of the sorting functions from (a), and measures the time in nanoseconds. The program will repeat this process 30 times and will compute the average execution time for each...
For this assignment, design and implement a class to represent a die (singular of "dice"). A...
For this assignment, design and implement a class to represent a die (singular of "dice"). A normal bag of dice for playing Dungeons and Dragons, for example, contains dice with the following numbers of sides: 4, 6, 8, 10, 12, 20, and 100. In this program, the sides (or faces) of the die are numbered, starting with one. The current face value of the die corresponds to the side that is currently facing upward. By default, the face value is...