C program: Modify the following function to have two dice rather than one. Write
ID: 3750298 • Letter: C
Question
C program:
Modify the following function to have two dice rather than one. Write a rollDice function and also a function called maximum to find the max value in an array. Call the function maximum and pass the array frequency to find which sum is the maximum and print the max index as well as the max value. Ro1l a six-sided die 60,000,000 times 2 3 #include #include #include 6 #define SIZE 7 8 function main begins program execution 9 int main(void unsigned int frequency [SIZE]t0I7 clear counts srand (time CNULL77 seed random number generator /roll die 60,000,000 times 14 for (unsigned int rol1 1; rollExplanation / Answer
Hello, there is a confusion in rolling two dices. You didn’t specify how the two rolls should be used. It can either be in two ways – roll two dices in a single turn, increment the frequencies of both values separately, or roll two dice in a single turn, increment the frequency of sum of face values. In the second way, there is a possibility of having values between 2 (both dice roll 1) and 12 (both dice roll 6). Therefore I have modified the program for the above two ways. The first one is using separate incrementation of dice values and the second one is using sum of rolled values. You can choose whichever you want. And just drop a comment if you have any doubts. Thanks
First way:
//This program rolls two dice in a single turn, and increments frequencies
//of rolled values seperately. For example, if the first dice rolled a 5
//and second dice rolled a 3, this will increment the frequencies of 5 and 3
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SIZE 7
//method to simulate rolling of one dice
size_t rollDice(){
return 1+rand()%6;
}
//method to find the highest element in the given array and the index of
//highest element, and print both
void maximum(unsigned int frequency[]){
unsigned int highest=0;
size_t index=-1;
//iterating through the array
for(size_t face=0;face<SIZE;++face){
if(index==-1){
//first value, assuming this as the largest
highest=frequency[face];
index=face;
}else if(frequency[face]>highest){
//current value is bigger than current largest, so making
//this value as the new largest value
highest=frequency[face];
index=face;
}
}
//printing maximum and index of maximum
printf(" Maximum value: %d ",highest);
printf("Index of maximum value in the array: %d ",index);
}
int main(){
unsigned int frequency[SIZE]={0};
srand(time(NULL));
for(unsigned int roll=1;roll<=60000000;++roll){
//rolling two dice instead of one, also using rollDice() method
size_t face1=rollDice();
size_t face2=rollDice();
//incrementing frequencies of rolled faces
++frequency[face1];
++frequency[face2];
}
printf("%s%17s ","Face","Frequency");
for(size_t face=1;face<SIZE;++face){
printf("%4d%17d ",face,frequency[face]);
}
//finding and displaying the maximum value in the array
maximum(frequency);
}
//OUTPUT
Face Frequency
1 20002664
2 20005310
3 19993102
4 19999362
5 20004296
6 19995266
Maximum value: 20005310
Index of maximum value in the array: 2
Second way:
//This program rolls two dice in a single turn, and increments frequency
//of sum of two rolled faces. For example, if the first dice rolled a 5
//and second dice rolled a 3, this will increment the frequency of 8 (5+3)
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SIZE 13
//method to simulate rolling of one dice
size_t rollDice(){
return 1+rand()%6;
}
//method to find the highest element in the given array and the index of
//highest element, and print both
void maximum(unsigned int frequency[]){
unsigned int highest=0;
size_t index=-1;
for(size_t face=0;face<SIZE;++face){
if(index==-1){
highest=frequency[face];
index=face;
}else if(frequency[face]>highest){
highest=frequency[face];
index=face;
}
}
printf(" Maximum value: %d ",highest);
printf("Index of maximum value in the array: %d ",index);
}
int main(){
unsigned int frequency[SIZE]={0};
srand(time(NULL));
for(unsigned int roll=1;roll<=60000000;++roll){
//rolling two dice instead of one, finding the sum
size_t face1=rollDice();
size_t face2=rollDice();
size_t sum=face1+face2;
//incrementing frequency of sum value
++frequency[sum];
}
printf("%s%17s ","Face","Frequency");
for(size_t face=2;face<SIZE;++face){
printf("%4d%17d ",face,frequency[face]);
}
//finding and displaying the maximum value in the array
maximum(frequency);
}
//OUTPUT
Face Frequency
2 1669120
3 3335832
4 4999418
5 6666061
6 8334119
7 9997858
8 8335479
9 6665858
10 4996581
11 3334413
12 1665261
Maximum value: 9997858
Index of maximum value in the array: 7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.