Keep getting zero

I'm trying to write a program that simulates the rolling of dice. The program should print out the percentages of each time a 2-12 was rolled. However the issue I'm having is I'm getting nothing but zero for everything when it prints out.Any help would be great.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <ctime>
#include <iomanip>
#include <cstdlib>

using namespace std;


/************************************Rolling Dice Function***************************-*/
int rollDice ( )
{
	int die1;                 // The number after die 1 is rolled
	int die2;                 // The number after die 2 is rolled
	int totalDieCount;         // The sum of the two die


	die1 = rand() % 6 +1;
	
	die2= rand() % 6 + 1;
	
	totalDieCount = die1 + die2;
	
	return totalDieCount;
}
/*-----------------------------------Main Function-------------------------------------*/

int main ()
{

//Declaration statements

int numberOfRolls;                  // This is the number of rolls of the dice as assigned by the user
int count;                          // Keeps track of the number of times the die have been rolled in the for loop
int freqTable [13];                 // Keeps track of the frequency of die sums
int i;                              // Keeps track of where we are in the array freqTable
int totalDieCount;                  // This is the sum of both the die


// Ask the user how many times the dice should be rolled and assign that number to
// the correct variable declaration

cout << "How many times shall we roll the dice? " << endl;
cin >> numberOfRolls;

//Make sure to zero out the array

for (i = 0; i <= 12 ; i++)
{
	freqTable [i] = 0;
}

// Start the sequence off at a random value

srand( time(NULL));

// Begin for loop to roll the dice and add the result to the array

for (count = 0; count < numberOfRolls; count++)
{

	// Call the function to roll the dice and assign the retun value
	totalDieCount = rollDice();

	
	// Increment the result in the frequency table
	freqTable [totalDieCount]++;


}

for (int count = 2; count <=12; count++)

{
	cout << "A " << count << " occurs " << (float)((freqTable[count] / numberOfRolls) * 100) << "% of the time." << endl;
}

return 0;
}
(freqTable[count] / numberOfRolls)
This is integer division, and numberOfRolls is almost always greater than freqTable[count], thus you get zero. Convert to float before this division, not after.
Last edited on
Wow that fixed everything. Thank you very much.
Check line 74. That's where the flaw is.
Topic archived. No new replies allowed.