First of all, I'm completely new to C++, having had no prior coding experience using the language. I'm slightly stumped and would appreciate a steer in the right direction.
We've been given the problem of writing a program to simulate the outcome of two random dice throws, which runs for 10000 throws. I've nailed that part, tested it and it functions as intended. The twist is that the values need to be displayed graphically, using asterisks. So instead of;
The dice rolls equalled a total of 2: 200 times
The dice rolls equalled a total of 3: 300 times
The dice rolls equalled a total of 4: 550 times
The dice rolls equalled a total of 5: 1000 times
etc,
It must be displayed as;
The dice rolls equalled a total of 2: ** times
The dice rolls equalled a total of 3: *** times
The dice rolls equalled a total of 4: ****** times
Where each '*' denotes, for example, 100 throws, and the total number of asterisks corresponds to the nearest multiple of 100.
I've tried using strings and for loops, to no avail. I've done a little research and it seems that using an array to store the value for each possible dice outcome would work best, but I'm uncertain of how to implement this.
Here is what I have so far. Thanks in advance for any advice that can be offered.
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
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main()
{
int count[13];
for (int i = 1; i < 13; i++)
count[i] = 0;
srand(static_cast<unsigned>(time(0)));
for (int i = 1; i <= 10000; i++)
{
int j = rand() % 6 + 1;
int k = rand() % 6 + 1;
count[j + k] = count[j + k] + 1;
}
for (int i = 1; i < 13; i++)
cout << "The dice rolls equalled a total of " << i << ": " << count[i]
<< " times" << endl;
return 0;
}
|