Mar 27, 2016 at 8:42pm UTC
So this is my code so far and I took my array code and tried to incorporate a vector rather than an array. It's supposed to roll 36000 times and then tally up how many times you get which roll and display that vector. This runs but the program stops working as soon as it starts. Help please!
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
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
int main()
{
int n = 13;
int counter = 0;
vector<int > myVec;
int dice1, dice2;
for (int i = 0; i < n; i++) // I probably don't need this part for the vectors, right?
{
myVec[i] = 0;
}
for (int i = 0; i <= 36000; i++)
{
dice1 = rand()%6 + 1;
dice2 = rand()%6 + 1;
myVec.push_back(dice1 + dice2);
myVec[dice1 + dice2]++;
}
cout << "Sum of rolls\t" << "Tally" << endl;
for (int i = 2; i < n; i++)
{
cout << i << "\t\t" << myVec[i] << endl;
}
return 0;
}
Last edited on Mar 27, 2016 at 8:56pm UTC
Mar 27, 2016 at 9:07pm UTC
Yes, the first for-loop is not needed. I think you don't need line 24 either. Whack that line and the first for-loop and see if it runs better.
Mar 28, 2016 at 6:03am UTC
The possible values of dice1 + dice2 on line 16 range from 2 to 12. The valid indexes for counter are 0 through 10.
Change line 9 to vector<int > counter(13 );
Btw, dice are not coins.
Last edited on Mar 28, 2016 at 6:19am UTC