Hello. I am new to C++ and arrays. My program needs to ask the user how many times a pair of dice will be thrown and simulate that many rolls. Then it needs to record and output the frequency of each value and the percentage of total occurences for each value. I have two problems and a question:
1. There are never any 12's rolled.
2. I can't seem to calculate the percentages.
3. What is the difference between initializing an array with a loop or just saying " array[50] = {0}" ?
Any help is appreciated. I really wanna get this. Here is my code:
int main()
{
// declare variable to hold user input
int numberOfRolls;
// declare counter array to hold frequencies of dice results
int counter[11] = {0};
int i, j, m;
int value;
// declare percentage array to hold percentages for each vaue 2-12
float percentage[10] = {0};
// declare array for dice values
int diceValues [5000];
// initialize array that will hold values
for (i=0; i < 4999; i++)
diceValues[i]= 0;
// ask user how many times the dice should be rolled
cout << "How many times will the dice be rolled?" << endl;
// user input # of dice rolls
cin >> numberOfRolls;
// variable to hold number of rolls (for output)
j= numberOfRolls;
// loop to count frequency of each value
for (i=0; numberOfRolls >0; numberOfRolls--)
{
// activate diceRoll function
diceValues[i] = diceRoll();
for (value=2 ; value<=12 ; value++)
{
if (diceValues[i] == value)
counter[value-2]++;
}
i++;
}
// loop to calculate percentage of the time each value occurs
for (m= 0; m <11; m++)
percentage [m]= (counter[m]/j)*100;
cout << "The dice were rolled " << j << " times." << endl;
cout << "The value 2 came up " << counter [0] << " times or " << percentage[0] << "% of the time." << endl;
cout << "The value 3 came up " << counter [1] << " times or " << percentage[1] << "% of the time." << endl;
cout << "The value 4 came up " << counter [2] << " times or " << percentage[2] << "% of the time." << endl;
cout << "The value 5 came up " << counter [3] << " times or " << percentage[3] << "% of the time." << endl;
cout << "The value 6 came up " << counter [4] << " times or " << percentage[4] << "% of the time." << endl;
cout << "The value 7 came up " << counter [5] << " times or " << percentage[5] << "% of the time." << endl;
cout << "The value 8 came up " << counter [6] << " times or " << percentage[6] << "% of the time." << endl;
cout << "The value 9 came up " << counter [7] << " times or " << percentage[7] << "% of the time." << endl;
cout << "The value 10 came up " << counter [8] << " times or " << percentage[8] << "% of the time." << endl;
cout << "The value 11 came up " << counter [9] << " times or " << percentage[9] << "% of the time." << endl;
cout << "The value 12 came up " << counter [10] << " times or " << percentage[10] << "% of the time." << endl;
// loop to determine percentage
return 0;
}
int diceRoll ()
{
int dice1, dice2;
int total;
// loop to simulate dice roll
dice1 = rand()%6+1;
dice2 = rand()%6+1;
total=dice1+dice2;
return total;
}
percentage[10] does not exist - you made the array percentage too small. When you write to percentage[10], you're stomping over something else.
That aside, the value 12 does come up. maybe you're not rolling the dice enough times.
counter[m]/j will not do what you want; when you divide an int by another int, the answer is an int. No decimal places. Just an int. You need to make the values float, or double; something that can handle decimal places.
1 2 3 4 5
// user input # of dice rolls
cin >> numberOfRolls;
// variable to hold number of rolls (for output)
j= numberOfRolls;
This is insane. You take a nice, easy to understand variable with a sensible name (numberOfRolls) and then you stop using it and use the value j instead. Might I suggest that you keep numberOfRolls and for the value you have that you deduct from in the loop, change that to something sensible like numberOfRollsRemaining. Sensible names for variables makes life so much easier.
Gosh! Thanks. Right on both counts. Seems to work well now.
I guess 'j' isn't a very telling identifier :) I just needed to hold the original value before I started decrementing. I'll take your advice to heart. I need all I can get!
That's a good question. I just chose an arbitrary number to get the program going. Why can't I say " int diceValues [numberOfRolls] " to declare the array after user input. I tried, but I get an error.
Not quite. The problem is that C++ does not allow variable sized array on the stack; that's any array you make by code like:
int diceValues [someVariable]
The fact that numberOfRolls isn't initialised is also a problem, but only because it just means it will have some random value in it.
You can, however, make an array of any size you like dynamically, like this:
int* diceValues = newint[numberOfRolls];
and you can then use diceValues exactly the same way as before. At the end of the program, you should tidy up any memory you got through the new[] command using delete[], as follows:
numberOfRolls isn't a constant that can be evaluated at runtime, so the compiler doesn't know how to allocate memory for the array. Either dynamically allocate diceValues or use a std::vector.