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 // Standard I/O library

#include // Standard library of C commands

#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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!