This program should simulate the roll of a single die dice 1
This program should simulate the roll of a single die (dice) (1-6) using the C++ random number functions. First ask the user how many times they would like to have the die (dice) rolled. Next, have the program simulate the number of rolls of the die (dice) the user requested and keep track of which number the die (dice) landed on for each roll. At the end of the program print out a report showing how many times the die (dice) roll landed on each number and what percentage (to two decimial places) of the total times the die (dice) roll landed on each number. Do NOT use functions or arrays on this. Input Validation: Do not allow the user to enter a number less than 1 as the number of times they would like to roll the dice. Use a do/while statement for the input validation.
Solution
Please find the code as below:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
int main()
{
srand ((unsigned int)time(0));
int times_rolled;
int rolled;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int num5 = 0;
int num6 = 0;
cout <<\"How many times would you like to roll the dice?\" << endl;
cin >> times_rolled;
for (int i = 0; i < times_rolled; ++i)
{
rolled = rand() % 6 + 1;
if (rolled = 1)
{
num1++;
}
else if (rolled = 2)
{
num2++;
}
else if (rolled = 3)
{
num3++;
}
else if (rolled = 4)
{
num4++;
}
else if (rolled = 5)
{
num5++;
}
else if (rolled = 6)
{
num6++;
}
}
cout <<\"#Rolled \\t #Times \\t %Times\" << endl;
cout <<\"----- \\t ------- \\t -------\" << endl;
cout <<\" 1 \\t \" << num1 << \"\\t \" << fixed << setprecision(2) << (num1/times_rolled)*100 << \"%\" << endl;
cout <<\" 2 \\t \" << num2 << \"\\t \" << fixed << setprecision(2) << (num2/times_rolled)*100 << \"%\" << endl;
cout <<\" 3 \\t \" << num3 << \"\\t \" << fixed << setprecision(2) << (num3/times_rolled)*100 << \"%\" << endl;
cout <<\" 4 \\t \" << num4 << \"\\t \" << fixed << setprecision(2) << (num4/times_rolled)*100 << \"%\" << endl;
cout <<\" 5 \\t \" << num5 << \"\\t \" << fixed << setprecision(2) << (num5/times_rolled)*100 << \"%\" << endl;
cout <<\" 6 \\t \" << num6 << \"\\t \" << fixed << setprecision(2) << (num6/times_rolled)*100 << \"%\" << endl;
system(\"PAUSE\");
return 0;
}
Thank you.


