Question
IN C++ Instructions: 1. Implement a class named Die that simulates the throw of a die. 2. Create a dynamic array composed of n objects
IN C++
Instructions: 1. Implement a class named Die that simulates the throw of a die. 2. Create a dynamic array composed of n objects of type Die class, simulate the throwing of n dice. 3. Tabulate your answers obtained in the simulation.
Suppose you roll a set of n dice. Then the smallest sum is n and the largest sum is 6n. For example, if n = 10, then the smallest sum is 6 and the largest sum is 60. Let m be the desired sum of the numbers rolled. Then n = m = 6n. If n = 10, then 6 = m = 60. Write a program that uses the classDie, to roll 10 dice. (Use an array of size 10 to implement 10 dice.) The program prompts the user to enter the desired sum and the number of times the dice are to be rolled The program outputs the number of times the desired sum was rolled and the probability of rolling the desired sum. Test run your program to roll the 10 dice 10000, 100000, 1000000, 10000000, and 100000000 times with the desired sums 6, 25, 40, and 60. How many times was the sum 6 rolled? How many times was the sum 60 rolled?
// main.cpp //
#include "die.h" #include
using namespace std;
int main() { // Create array of die type objects Die die[10]; //declare required variables int desiredSum, rollSum, noOfTrails; int success = 0; //prompt the user to enter the sum of numbers //to be obtaines when dice are rolled cout cin >> desiredSum; //loop till a valid sum is entered by the user while (desiredSum{ cout cin >> desiredSum; } //promptand read the number of trails required cout cin >> noOfTrails; //Lopp till valid number of trails is entered while (noOfTrails{ cout cin >> noOfTrails; } //Simulate the dies rolling using a for loop and calling the function rollDie() for (int i= 0; i{ rollSum = 0; for (int j = 0; j{ die[j].rollDie(); rollSum += die[j].getNumber(); } //check the sum rolled is equal to the user //derired sum or not if (rollSum == desiredSum) success++; } //display the simulation results cout cout cout return 0; }
//////////////////////////////////////////////////////////////////////////////////////////
// die.h
#pragma once
class Die { private: int number; public: //default constructor Die(); //Function to roll die. void rollDie(); //Function to return the number on die's top face int getNumber() const; };
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
die.cpp
// die.cpp /******************************************************************************** * This is the implementation file containing the implementation of the class die. * ******************************************************************************/ #include #include #include #include "die.h" using namespace std; //default constructor Die::Die() { number = 1; //seed the time for random number generations srand(time(0)); } //function to simulate the rolling the die it generates a random number //between q and 6 stores the value in the variable number void Die::rollDie() { //generate a random number between 1 and 6 //and store it in number number = rand() % 6 + 1; } //function returns the value stored in the variable number int Die::getNumber() const { return number; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started