Write a C++ program which has a main-driver and a dieToss20Trials class . The di
ID: 3908955 • Letter: W
Question
Write a C++ program which has a main-driver and a dieToss20Trials class.
The dieToss20Trials class holds data about 20 tosses of a die using the following member variable:
• An array called dieToss[] . The dieToss member array entries's will hold
values from 1 to 6 indicating the side of the die that is facing up.
The dieToss20Trials class should have the following member functions:
• A default constructor that randomly determines the side of the die that is facing up
(from 1 to 6) and initializes the dieToss member entries accordingly.
• A void member mutator function named toss() that simulates the tossing of the die. When
the toss() member function is called, it randomly determines the side of the coin that
is facing up (from 1 to 6) and sets the dieToss array member variable accordingly.
• An accessor member function named getDieToss that returns the value of the dieToss member variable.
Write a program that demonstrates the dieToss20Trials class.
The program should create an instance of the class and display the side that is initially facing up. Then, use a loop to toss the die 20 times. Each time the die is tossed, display the side that is facing up. The program should keep count of the number of times each side is facing up in an array counts[] of 6 counters and display those values after the loop finishes.
So at the end, print on the screen the 2 arrays, dieToss[] and counts[].
Format nicely.
Explanation / Answer
here is your program : ----------->>>>>>>>>
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
class dieToss20Trials{
int dieToss[20];
int currentSide;
int numTrial;
public:
dieToss20Trials(){
srand(time(0));
currentSide = rand()%6 + 1;
numTrial = 0;
}
void toss(){
if(numTrial < 20){
currentSide = rand()%6 + 1;
dieToss[numTrial++] = currentSide;
}
}
int getSide(){
return currentSide;
}
int* getDieToss(){
return dieToss;
}
};
int main(){
dieToss20Trials trial;
int cur = trial.getSide();
int counts[6] = {0};
cout<<" Initial Side : "<<cur;
for(int i = 0;i<20;i++){
trial.toss();
cur = trial.getSide();
counts[cur-1] = counts[cur-1] + 1;
cout<<" Current Side : "<<cur;
}
int *tosses;
tosses = trial.getDieToss();
cout<<" Counts Of All Sides : ";
for(int i = 0;i<6;i++){
cout<<(i+1)<<" . "<<counts[i]<<" ";
}
cout<<" Die 20 Tosses : ";
for(int i = 0;i<20;i++){
cout<<tosses[i]<<" ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.