Unfortunately not working. Can someone guide me in the right direction
Requirements:
(1) Create a function that simulates a throw of two dice. Hint: Create a function that simulates a throw of a die, then invoke it twice and add them.
(2) The number of throw will be one million. Count the sum of two dice. Hint: Use a loop and a one dimensional array.
(3) The result must be well formatted. The number, and histogram associated with the number of throws need to be shown. One * represents 10 thousand in the example. Hint: Use formatting and loops.
#include <iostream>
#include <iomanip>
using namespace std;
int rolldice()
{
int dice ;
dice = rand () % 6;
dice++;
return dice;
}
void OneDice()
{
int i, roll;
for (i = 1000000; i > 0; i--)
{
roll = rolldice();
OneDice();
}
}
void TwoDice()
{
int i, dice1, dice2, roll;
for (i = 1000000; i > 0; i--)
{
dice1 = rolldice();
dice2 = rolldice();
roll = dice1 + dice2;
TwoDice();
}
}
inline void star(int num)
{
int mod = num;
while (mod > 0)
{
cout << "*";
mod -= 10000;
}
}
int main()
{
int i;
srand(1000000);
TwoDice();
for (i = 1; i <= 12; i++)
{
cout << setw(2) << i << " : " << setw(6) << twodice[i] << " : ";
star(twodice[i]); cout << endl;
}
The thing that jumps out at me, is in the OneDice and the TwoDice functions. You have for loops to count down from the one million, BUT you call the functions again in the each functions loop. The for loops will start again at one million, and get called again. Endlessly. Remove the function calls, and see if it helps.
Also, your srand() call is being seeded, so the rand numbers never change, run to run.
Since your assignment only mentioned 'one dimensional array' I've been cheeky and used std::array rather than C-style array as the container gives you greater flexibility in using range loops as used for printing the results (does require C++11 compliant compiler). The empirical probability distribution is in line with theoretical and 1,000,000,000 (billion, not million) took 37 secs.