in c++ I need two files .cpp and #include This program should simulate the roll of a single die (dice) (1-6)

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

in c++ I need two files .cpp and #include This program should simulate the roll of a single die (dice) (1-6)

Post by answerhappygod »

in c++
I need two files .cpp and #include <stdlib.h>
This program should simulate the roll of a single die (dice)(1-6) using the C++ random number functions. First ask the user ifthey want to set the random seed to 7 or the time of day (therandom values will be different). Use the following code:
char ch;
srand(time(NULL));
cout << "Use value 7 as a random seed (y/n)? ";
cin >> ch;
if (ch == 'y' || ch == 'Y')
srand(7);
Then ask how many times they would like to have the die (dice)rolled. Next, have the program simulate the number of rolls of thedie (dice) the user requested and keep track of which number thedie (dice) landed on for each roll – use a switch statement and notifs. At the end of the program print out a report showing how manytimes the die (dice) roll landed on each number and what percentageof the total times the die (dice) roll landed on each number. DoNOT use functions or arrays on this - use what I showed you duringlecture, you should always listen during lecture to get the righttechniques, if you forgot what I said during lecture look at theslides.
Input Validation: Do not allow the user to enter a number lessthan 1 as the number of times they would like to roll the dice. Usea do/while loop for the input validation. Your output should looksimilar to what is below:
In this example, the user entered n for “time-of-day” seed then25 as the number of times they would like to roll the dice.
Use value 7 as a random seed (y/n)? n
Please enter in number of rolls: 0
Please enter rolls > 1
Please enter in number of rolls: 25
DICE ROLL STATISTICS
# Rolled # Times % Times
-------- ------- -------
1 5 20.00%
2 1 4.00%
3 4 16.00%
4 2 8.00%
5 8 32.00%
6 5 20.00%
HINT: Put your srand() function outside of your loop (otherwiseyou reset the random sequence every time – not too random!) likeshown above. Watch what happens if you put it inside loop and seeif you can figure out why that happens.
Also do: #include <stdlib.h>
That is for hypergrade to work correctly with randomnumbers.
IMPORTANT NOTE - READ!: I put up the output so you could matchas closely as possible. If you try the to match exactly you'll beworking a long time.
Regarding output: For the dice number and percentage of timesrolled a setw(8) was used. **PSEUDOCODE IS REQUIRED FOR THISPROGRAM*** (be detailed and do it correctly)
Test Case 1
Use value 7 as a random seed (y/n)? yENTERPlease enter in number of rolls: -1ENTERPlease enter rolls > 1\nPlease enter in number of rolls: 30ENTER\nDICE ROLL STATISTICS\n\n# Rolled # Times % Times\n-------- ------- -------\n\n 1 6 20.00%\n 2 9 30.00%\n 3 6 20.00%\n 4 3 10.00%\n 5 2 6.67%\n 6 4 13.33%\n
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply