Create a new file (in Dev C++) and save it as lab12_XYZ.cpp (replace XYZ with your initials). Create ANOTHER new file (in Dev C++) and save it as lab12_XYZ.h (replace XYZ with your initials). Use the H (header) file to declare constants and define functions. Make sure to #include "lab12_XYZ.h", and place H file in same location as your main program. Consider rolling two six-sided dice. While the result of each throw is independent of all rolls that have come before, we can still calculate the odds of rolling any given number between 2 and 12. This program will calculate those odds by generating a large number (100,000) of random values (i.e. "rolling the dice") and storing the results in an array. When all values have been rolled, we will display the results (counts and odds to 2 decimal places) to the user. x x 2 3 4 5 6 7 8 9 10 11 12 //value for sum of 2 dice rolls+---+---+---+---+---+---+---+---+---+---+---+---+---+| | | | | | | | | | | | | | //counters for number of times value is rolled+---+---+---+---+---+---+---+---+---+---+---+---+---+ 0 1 2 3 4 5 6 7 8 9 10 11 12 //array index We need some constants int constants - NUM_ROLLS, NUM_SIDES, NUM_DICE, PRECISION, COLW1, COLW2, COLW3, etc. string constants - HELLO, GOODBYE, etc. We need some functions: void displayMessage(string); //HELLO, GOODBYE, etc. (constants declared in your lab12_XYZ.h file...) int rollDice(); //returns a number between 2 and 12 (by rolling two dice and summing the result) void displayResults(int[]); //displays results in the array In Lab 5, sample code was provided for generating a random number. Seed the random number generator in main(), then use rand() in the rollDice() function. #include <cstdlib>#include <ctime>unsigned seed = time(0);srand(seed); //seed the RNGint num = (rand() % NUM_SIDES) + 1; For this assignment: Header comments must be present in both CPP and H files Hello and goodbye messages must be shown Array(s) must be used for implementation DO NOT make arrays global variables Results should be formatted neatly (2 decimal places for odds, decimals lined up, etc.) Use comments and good style practices HINT: Make sure to initialize counter array before starting loop HINT: To calculate odds percentage: odds = count * 100 / NUM_ROLLS; SAMPLE OUTPUT:Welcome! This program will calculate the odds of rolling 2 - 12 with two 6-sided dice.Number of 2s rolled: 2776 ( 2.78%) Number of 3s rolled: 5518 ( 5.52%) Number of 4s rolled: 8234 ( 8.23%) Number of 5s rolled: 11273 (11.27%) Number of 6s rolled: 13884 (13.88%) Number of 7s rolled: 16594 (16.59%) Number of 8s rolled: 13848 (13.85%) Number of 9s rolled: 11102 (11.10%) Number of 10s rolled: 8394 ( 8.39%) Number of 11s rolled: 5580 ( 5.58%) Number of 12s rolled: 2797 ( 2.80%)Exiting program. Goodbye! Lanauge is C++ Please make it as simple as possible.
Create a new file (in Dev C++) and save it as lab12_XYZ.cpp (replace XYZ with your initials).
Create ANOTHER new file (in Dev C++) and save it as lab12_XYZ.h (replace XYZ with your initials).
Use the H (header) file to declare constants and define functions. Make sure to #include "lab12_XYZ.h", and place H file in same location as your main program.
Consider rolling two six-sided dice. While the result of each throw is independent of all rolls that have come before, we can still calculate the odds of rolling any given number between 2 and 12. This program will calculate those odds by generating a large number (100,000) of random values (i.e. "rolling the dice") and storing the results in an array. When all values have been rolled, we will display the results (counts and odds to 2 decimal places) to the user.
x x 2 3 4 5 6 7 8 9 10 11 12 //value for sum of 2 dice rolls+---+---+---+---+---+---+---+---+---+---+---+---+---+| | | | | | | | | | | | | | //counters for number of times value is rolled+---+---+---+---+---+---+---+---+---+---+---+---+---+
0 1 2 3 4 5 6 7 8 9 10 11 12 //array index
We need some constants
- int constants - NUM_ROLLS, NUM_SIDES, NUM_DICE, PRECISION, COLW1, COLW2, COLW3, etc.
- string constants - HELLO, GOODBYE, etc.
We need some functions:
- void displayMessage(string); //HELLO, GOODBYE, etc. (constants declared in your lab12_XYZ.h file...)
- int rollDice(); //returns a number between 2 and 12 (by rolling two dice and summing the result)
- void displayResults(int[]); //displays results in the array
In Lab 5, sample code was provided for generating a random number. Seed the random number generator in main(), then use rand() in the rollDice() function.
#include <cstdlib>#include <ctime>unsigned seed = time(0);srand(seed); //seed the RNGint num = (rand() % NUM_SIDES) + 1;
For this assignment:
- Header comments must be present in both CPP and H files
- Hello and goodbye messages must be shown
- Array(s) must be used for implementation
- DO NOT make arrays global variables
- Results should be formatted neatly (2 decimal places for odds, decimals lined up, etc.)
- Use comments and good style practices
HINT: Make sure to initialize counter array before starting loop
HINT: To calculate odds percentage: odds = count * 100 / NUM_ROLLS;
SAMPLE OUTPUT:Welcome! This program will calculate the odds of rolling 2 - 12 with two 6-sided dice.
Number of 2s rolled: 2776 ( 2.78%)
Number of 3s rolled: 5518 ( 5.52%)
Number of 4s rolled: 8234 ( 8.23%)
Number of 5s rolled: 11273 (11.27%)
Number of 6s rolled: 13884 (13.88%)
Number of 7s rolled: 16594 (16.59%)
Number of 8s rolled: 13848 (13.85%)
Number of 9s rolled: 11102 (11.10%)
Number of 10s rolled: 8394 ( 8.39%)
Number of 11s rolled: 5580 ( 5.58%)
Number of 12s rolled: 2797 ( 2.80%)
Exiting program. Goodbye!
Lanauge is C++
Please make it as simple as possible.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images