In my Computer Science 221 class we are focusing on C++ and more specifically cl
ID: 3724936 • Letter: I
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 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 <iostream>
#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 <iostream>
#include <string>
#include "die.h"
#include <time.h>
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;
};
Explanation / Answer
Mistake is in rollDie.cpp.Here's the corrected code.
#include <iostream>
#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 < numTrails; i++)
{
sumRoll = 0;
for (int j = 0; j < roll; j++)
{
die[j].rollDie();
sumRoll = die[j].getNum() + sumRoll;
}
if (sumRoll == sumWanted)
{
done++;
}
}
// Check if the desired sum matches
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.