Can Anyone help me with this C problem? Exercise 4 Write a program that simulate
ID: 2292373 • Letter: C
Question
Can Anyone help me with this C problem?
Exercise 4 Write a program that simulates the rolling of two die. Ask the user for a seed value to initialize the srand library function. The program > » should use the rand function (random number generator) to roll the first die, and then use rand again to roll the second die. The sum of the two values should then be calculated. Make your program rolls the dice 50,000 times. Use a 1-D array to store the number of times each sum appeared. Print the results in a tabular format, along with the percentage that each sum appeared, ie., (count/50,000)* 100% Note: Since each die has an integer value from 1 to 6, then the sum of the two values will vary from 2 to 12. The total possible number of combinations is 36. The value 7 will be the most frequent sum with six possible combinations ((16),(6,1),(25),(5,2), {3,4),(4,3) 6/36-16.7% of the time). The values 2 and 12 are the least frequent sums ({1,1}-> 1/36-2.8%, and {6,6}-1/36- 2.8%). In your simulation, the calculated percentages should be similar to the theoretical values Sample Run: Enter seed value 862 SumCount Appear 2 1365 2.73% 3: 2796 5.59% 4: 4283 8.57% 5w: 5546 11.09% 6': 6843 13.69% 7 8278 16.56% 6993 13.99% 2 5504 11.01% 10: 4187 8.37% 11: 2770 5.54% 12: 1435 2.87%Explanation / Answer
#include <stdio.h>
int main (){
int seed, dice1, dice2, sum, sum_array[11], i = 0;
for (i=0;i<11;i++){
sum_array[i] = 0; /*initializing array elements as 0*/
}
printf ("Enter seed value : ");
scanf ("%d", &seed);
srand (seed);
for (i = 0; i < 50000; i++){
/*First dice roll*/
dice1 = 1 + rand () % 6; /*Will return a number between 1 - 6 */
/*Second dice roll*/
dice2 = 1 + rand () % 6;
sum = dice1 + dice2;
sum_array[sum - 2] = sum_array[sum - 2] + 1; /*2 is saved in 0-th index of array, 3 is saved in
1st index of array and so on */
}
printf ("Sum Count Appear ");
for (i = 0; i < 11; i++){
printf ("%d : %d %.2f%% ", i+2, sum_array[i], 0.002*sum_array[i]);
/* Percentage that (i+2) sum appeared = (sum_array[i]/50000)*100 = 0.002*sum_array[i]*/
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.