Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a program that calls a method RollDie that simulates the rolling of a pair of dice 1000 times.  Use an array of size 11 to keep track of the rolls. After the 1000 rolls, output a list of the Rolls (2-12) and the frequency (count out of 1000) of each roll.  


What I have tried:

#include <cstdlib>
#include <ctime>
using namespace std;
void RollDie(int counts[]) 
{
    for (int i = 0; i < 13; ++i)
    {
        counts[i] = 0;
    }
    for (int i = 0; i < 1000; ++i)
    {
        counts[(1 + rand() % 6) + (1 + rand() % 6)]++;
    }
}

int main()
{
    srand(time(NULL));
    int counts[13];
    RollDie(counts);
    cout << "Roll Time\tFrequency" << endl;
    for (int i = 2; i <= 12; ++i)
    {
        cout << i << "\t\t" << counts[i] << endl;
    }
    return 0;
}
Posted
Updated 23-Nov-20 15:26pm
Comments
Sandeep Mewara 23-Nov-20 12:58pm    
And your query is?
The Other John Ingram 23-Nov-20 13:44pm    
did you run your program?
does your program fulfill the requirements?
jeron1 23-Nov-20 16:00pm    
Requirement: "Use an array of size 11 to keep track of the rolls."
Yours:
int counts[13];

Read the instructions again, paying careful attention to the details.
Hint: Read the first eight words and then look at your code.
 
Share this answer
 
Another point to consider - and this makes things both clear and more sensible:

The instructions say write a program that calls a function RollDice() that simulates 1000 dice rolls.

It does not say the function should simulate 1000 dice rolls - only that your program does. You have an opportunity here to learn how to create functions that do specific jobs in a manner that allows you to reuse them.

IF you are clever then you'll let the dice rolls sort themselves into your array.


Now you have a much cleaner process to work with - get to work.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900