Write a program that will prompt the user for the number of times the program sh
ID: 3547636 • Letter: W
Question
Write a program that will prompt the user for the number of times the program should roll the die, then roll the die, count the occurrence of each number, and finally print out a list giving the frequency of each outcome, as well as the percentage each occurrence represents of the total number of rolls. Use an array to store the count of each number rolled.Here is a sample output screen:
------------------------------
Welcome to the Roll Couner
------------------------------
How many times do you want me to roll the die? 100
Results
Roll Frequency Percentage
1 18 18.00
2 16 16.00
3 17 17.00
4 17 17.00
5 14 14.00
6 18 18.00
Done!
Write a program that will prompt the user for the number of times the program should roll the die, then roll the die, count the occurrence of each number, and finally print out a list giving the frequency of each outcome, as well as the percentage each occurrence represents of the total number of rolls. Use an array to store the count of each number rolled.
Here is a sample output screen:
------------------------------
Welcome to the Roll Couner
------------------------------
How many times do you want me to roll the die? 100
Results
Roll Frequency Percentage
1 18 18.00
2 16 16.00
3 17 17.00
4 17 17.00
5 14 14.00
6 18 18.00
Done!
Explanation / Answer
Here is what I solved before. Please substitute the figures as per your question. Let me know if you want some clarification on it. Please rate 5 stars if I succeeded in helping you.
Write a C++ program that will prompt the user for the number of times the program should roll a die. Your program then rolls the die (generate a number between 1 and 6 inclusive) the requested number of time and counts the occurrence of each number. It finally prints out a list giving the frequency of each outcome, as well as the percentage each occurrence represents of the total number of rolls. Following is a sample output screens. The count of the occurrence of each role should be stored in an array with 6 elements.
------------------------------------
Welcome to the Roll Counter
------------------------------------
How many times do you want me to roll the die? 1000
RESULTS
---------
ROll Frequency Percentage
1 177 17.70
2 175 17.50
3 154 15.40
4 159 15.90
5 179 17.90
6 156 15.60
Done!
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int num, rolls;
int dice[6] = {0};
cout << "--------------------------- ";
cout << "Welcome to the Roll Counter ";
cout << "--------------------------- ";
cout << "How many times do you want me to roll the die? ";
cin >> rolls;
srand(time(NULL));
for(int i = 0; i < rolls; i++){
num = rand()%6 + 1;
switch(num){
case 1:
dice[0]++;
break;
case 2:
dice[1]++;
break;
case 3:
dice[2]++;
break;
case 4:
dice[3]++;
break;
case 5:
dice[4]++;
break;
case 6:
dice[5]++;
break;
}
}
cout << "RESULTS ";
cout << "------- ";
cout << "Roll Frequency Percentage ";
for(int i = 0; i < 6; i++){
cout << i + 1 << " " << dice[i] << " ";
cout << fixed << setprecision(2);
cout << (double)dice[i]/rolls*100 << endl;
}
cout << "Done! ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.