This program should simulate the roll of a single die (dice) (1-6) using the C++
ID: 3641035 • Letter: T
Question
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 of the total times the die (dice) roll landed on each number. Do NOT use functions or arrays on this#include<iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand ((unsigned int)time(NULL));
int times = 0;
int roll;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int num5 = 0;
int num6 = 0;
int Num1, Num2, Num3, Num4, Num5, Num6;
roll = rand() % 6 + 1;
while(times >= 1)
{
cout << "How many times would you like to roll the dice?";
cin >> times;
}
for (int i = 1; i <= times; roll++)
{
if (roll == 1)
{
num1++;
}
else if (roll == 2)
{
num2++;
}
else if (roll == 3)
{
num3++;
}
else if (roll == 4)
{
num4++;
}
else if (roll == 5)
{
num5++;
}
else if (roll == 6)
{
num6++;
}
}
Num1 = num1/times *100;
Num2 = num2/times *100;
Num3 = num3/times *100;
Num4 = num4/times *100;
Num5 = num5/times *100;
Num6 = num6/times *100;
cout <<"# Rolled # Times % Times" << endl;
cout <<"----- ------- -------" << endl;
cout <<" 1 " << num1 << " " << fixed << setprecision(2) << Num1 << "% ";
cout <<" 2 " << num2 << " " << fixed << setprecision(2) << Num2 << "% ";
cout <<" 3 " << num3 << " " << fixed << setprecision(2) << Num3 << "% ";
cout <<" 4 " << num4 << " " << fixed << setprecision(2) << Num4 << "% ";
cout <<" 5 " << num5 << " " << fixed << setprecision(2) << Num5 << "% ";
cout <<" 6 " << num6 << " " << fixed << setprecision(2) << Num6 << "% ";
system("PAUSE");
return 0;
}
Explanation / Answer
Here ya go, I changed a few things and added comments so you could see what I did. Hope this helps! #include #include #include #include using namespace std; int main() { //First, seed the random number generator so we get different random numbers //everytime we run the program srand((unsigned int)time(NULL)); //Declare the local variables: The number of rolls, and each dice outcome int numRolls = 0, num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0, roll; //Ask user for number of rolls while(numRolls > numRolls; } //Roll the dice for(int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.