Question: Given the code below, modify the random number generator to roll a 6-sided dice. DO NOT DISPLAY THE RESULTS OF EACH ROLL. Ask the user
Given the code below, modify the random number generator to roll a 6-sided dice.
DO NOT DISPLAY THE RESULTS OF EACH ROLL.
Ask the user how many times to roll the dice.
Roll the dice that many times and track of how many times each number is rolled.
Display a nicely formmatted tally of how many times each number is rolled
Code:
// Write a program which prompts the user for the number of coin flips, // and prints the number of heads and tails for the sequence of flips. // A flip() function is provided which randomly returns 1 or 0..
#include
#include
#include
int flip(); // Prototype for flip function below
int main()
{
// Declare some variables
srand(time(0));
int i, c, head, tail, n;
// Prompt the user for the number of times to flip the coin
printf("How many times want to flip coin: ");
scanf("%d", &n);
// Flip the coin that number of times, keeping track of heads and/or tails
head = 0; // 1 for head
tail = 0; // 0 for tail
i = 1;
while(i <= n) {
c = rand()%2; //0 or 1
if(c == 0)
tail++;
else
head++;
i++;
}
// Print the results: number of flips, and number of heads and tails
printf("Number of heads: %d ", head);
printf("Number of tails: %d ", tail);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
