Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
Objectives
After this project, students should be able to apply various programming techniques and concepts, such as
variables, arithmetic, user input, selection and repetition structures, output formatting, random-event simulation,
and functions to a single program.
Background
Games of chance use dice to achieve randomness. Most dice have the same likelihood of rolling each side.
Consider the calculation of probabilities for a six-sided die below:
However, some dice are weighted. This means that they have different likelihoods of rolling each side. The
probability to roll a side with weight w is (w / sum of all weights). Consider the calculation of probabilities
based on given sides and weights:
where sum of all weights = 3.5 + 1.5 + 1.0 + 4.0 = 10
Side Weight Probability
1 N/A 1 / 6 ≈ 16.67%
2 N/A 1 / 6 ≈ 16.67%
3 N/A 1 / 6 ≈ 16.67%
4 N/A 1 / 6 ≈ 16.67%
5 N/A 1 / 6 ≈ 16.67%
6 N/A 1 / 6 ≈ 16.67%
Side Weight Probability
1 3.5 3.5 / 10 = 35%
2 1.5 1.5 / 10 = 15%
3 1.0 1.0 / 10 = 10%
4 4.0 4.0 / 10 = 40%
Instructions
Write a program named diceroll.cpp that:
Implements a function named roll()that simulates a single roll of a weighted die, given its number of
sides and weight values for each side. It should return a value between 1 and sides to indicate which
side was rolled. Follow the specifications below:
o Return type: int
o Name: roll
o Parameters:
int sides: number of sides in the die
double *weights: a pointer to an array of weight values for each side in the die
weights[0]: weight value for Side 1
weights[1]: weight value for Side 2
...
weights[sides - 1]: weight value for Side sides
Presents the user with a menu comprised of four choices:
o Enter the sides and weights for a die:
Prompt the user to enter the number of sides of a die and validate the input (≥2)
Prompt the user to enter the appropriate number of weights and validate the input (>0)
o Specify an output file
Prompt the user for the name of a file to write all output in text format
o Simulate a specified number of rolls of the weighted die
Prompt the user for the number of rolls to perform and validate the input (≥1)
Present an error to the user if no output file has been specified (via menu choice 1)
Present an error to the user if the number of sides and associated weights have not yet
been entered (via menu choice 2)
All output should be displayed on the console AND written to the specified output file
o Exit
End the program
You must design your file-output code so that previous simulations in the same program session are not overwritten
(HINT: open the file in append mode).