In: Computer Science
Write a program to simulate rolling a six-sided fair die. Allow the user to enter the
number of rolls.
Your program should use rand() to get the result of die rolling.
Compute the number of occurrences for each number roll.
Calculate the percentage for each number roll.
Out put the results in the following format.
Use a do while loop to allow the user to repeat the process.
Here is the sample run for your program:
Please enter the number of die rolls
25
Number Frequency Percentage
1 3 0.12
2 6 0.24
3 10 0.40
4 1 0.04
5 3 0.12
6 2 0.08
Do you want to continue rolling die another time? Press Y or N
Y
Please enter the number of die rolls
10
I want to be able to do it with a switch statement but am unsure
Code-
#include <iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
//generating random variable
srand(time(0));
//variable declare
int n,min =1 ,max = 6,randNo,i;
int a[6] ;
char choice;
//do while loop
do{
//initialize array a[i] to 0
for(i=0;i<6;i++){
a[i]=0;
}
//ask user number of die rolls he want to roll
cout<<"Please enter the number of die
rolls"<<endl;
cin>>n;
//generating random number and storing value in a[] array
for(i = 0 ; i < n ;i++){
randNo = min + rand() % (( max + 1 ) - min);
switch(randNo){
case 1 : a[0]++;break;//if randon number generated is 1 than
increment a[0]++
case 2 : a[1]++;break;//if randon number generated is 2 than
increment a[1]++
case 3 : a[2]++;break;//if randon number generated is 3 than
increment a[2]++
case 4 : a[3]++;break;//if randon number generated is 4 than
increment a[3]++
case 5 : a[4]++;break;//if randon number generated is 5 than
increment a[4]++
case 6 : a[5]++;break;//if randon number generated is 6 than
increment a[5]++
}
}
//printintg result
for(i=0;i<6;i++){
cout<<(i+1)<<" "<<a[i]<<"
"<<(float)(a[i]/25.0)<<endl;
}
cout<<"Do you want to continue rolling die another time?
Press Y or N"<<endl;
cin>>choice;
}while(choice!='N');
return 0;
}
Screenshots -