Question
In my Computer Science 221 class we are focusing on C++ and more specifically classes. I am having trouble finding out what is wrong with
In my Computer Science 221 class we are focusing on C++ and more specifically classes. I am having trouble finding out what is wrong with his problem, any help would be greatly appreciated!
Problem:
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 10 and the largest sum is 60. Let m be the desired sum of the numbers rolled. Then n m 6n. If n = 10, then 10 m 60. Write a program that uses the class die, (Example 10-9 in the book), 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.
This is the following code I have:
rollDie.cpp
#include
#include "die.h"
using namespace std;
const int roll = 10;
int main() {
Die die[10]; // Using the die class to create an object and array of 10 for the rolls
int sumWanted, sumRoll, numTrails;
int done = 0;
cout << "Enter the sum you want of numbers when 10 dies are rolled: ";
cin >> sumWanted;
// This is to check if they are in the range for the desiredSum
while (sumWanted < 10 || sumWanted > 60)
{
cout << "The sum MUST be between 10 and 60!" << endl;
cout << "Please enter a desired sum again: ";
cin >> sumWanted;
}
// Enter how many times the die will be rolled
cout << "Enter the number of times you want the dice to be rolled: ";
cin >> numTrails;
while (numTrails < 0)
{
cout << "The number is invalid! Please enter a number larger than 0: ";
cin >> numTrails;
}
// Rolls begin now!
for (int i = 0; i < roll; i++)
{
sumRoll = 0;
for (int j = 0; j < numTrails; j++)
{
die[j].rollDie();
sumRoll = die[j].getNum() + sumRoll;
}
}
// Check if the desired sum matches
if (sumRoll == sumWanted)
{
done++;
}
cout << "You have a desired sum of " << sumWanted << ". You have matched it " << done << " times!" << endl;
cout << "Your chances were: " << (double)done / numTrails << "%" << endl;
system("pause");
return 0;
}
dieImp.cpp
#include
#include
#include "die.h"
#include
using namespace std;
Die::Die()
{
num = 1;
srand(time(0));
}
void Die::rollDie()
{
num = rand() % 6 + 1; // Establish the die faces
}
int Die::getNum() const
{
return num;
}
die.h
#pragma once
class Die {
public:
Die(); // Our default constructor
void rollDie(); // Function to roll the die
int getNum() const; // Return # of the die roll
private:
int num;
};
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