Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[C++] DiceStats Program Help


SirBernard
 Share

Recommended Posts

/* Bernard Yeboah

January 03 2013

*/

```

#include

#include

#include

using namespace std;

int main(int argc, char *argv[])

{

//Declare Variables

int rolls[10];

int sim = 0, temp = 0;

cout.setf(ios::fixed);

cout.precision(4);

//Timer

time_t seconds;

time(&seconds);

srand((unsigned int)seconds);

//Initialize values for elements in the array

for (int i = 0; i < 11; i++)

rolls[i] = 0;

//Prompt user for the number of simulations

cout << "Enter the number of simulations: ";

cin >> sim;

//Display header(Table)

cout.setf(ios::left);

cout << setw(5) << "Value";

cout.unsetf(ios::left);

cout.setf(ios::right);

cout << setw(15) << "Frequency" << setw(15) << "Exp Prob" << setw(15) << "Theo Prob" << endl;

cout.unsetf(ios::right);

//Loop the simulation

for (int i = 1; i <= sim; i++)

{

temp = rand() % 6 + 1 + rand() % 6 + 1;

rolls[temp]++;

}

cout.setf(ios::right);

for (int i = 2; i < 13; i++)

cout << setw(5) << i << setw(15) << rolls[i] << setw(15) << float(rolls[i]) / sim << setw(15) << i / 36.0 << endl;

system("PAUSE");

return EXIT_SUCCESS;

}

My goal is for it to display the frequency of the die rolls, and then also why doing this get the exponential and theoretical probabilities.[/i][/i][/i]

```
Link to comment
Share on other sites

At the moment the displaying of the information is wrong and i don't see the solution. I get it to list all the dice roll possiblities for each.

Ex: if i run the program and choose to run it once. i get this

```

value frequency exp theo

2 1 1.000 0.0556

and so on...

```

it does that with correct frequencies and value until it gets to 12 then it start displaying random (Garbage, like the array that stores the 12th data is not being used…)
Link to comment
Share on other sites

```
int rolls[10];
```

```

//Initialize values for elements in the array

for (int i = 0; i < 11; i++)

rolls[i] = 0;

arrays start at 0 not one.

You are constantly going over the bounds of your array

[code]

for (int i = 2; i < 13; i++)

cout << setw(5) << i << setw(15) << rolls[i] << setw(15) << float(rolls[i]) / sim << setw(15) << i / 36.0 << endl;

[/i][/i][/code][/i]
```
Link to comment
Share on other sites

> ```
> int rolls[10];
> ```
>
> ```
>
> //Initialize values for elements in the array
>
> for (int i = 0; i < 11; i++)
>
> rolls[i] = 0;
>
> arrays start at 0 not one.
>
> what do you mean, i am lost. Do you mean the array it's self or do you mean the way i set it's values.[/i]
> ```
Link to comment
Share on other sites

When you initalized your array you did "int rolls[10]" this gave you an array that had 11 slots because your array starts at zero meaning you have spaces "0 1 2 3 4 5 6 7 8 9 10". What Marshy Dearest is trying to show you is that when you did this

```

for (int i = 2; i < 13; i++) cout << setw(5) << i << setw(15) << rolls[i] << setw(15) << float(rolls[i]) / sim << setw(15) << i / 36.0 << endl;

you have

int i = 2; i< 13;

So essentially you are trying to reference parts of the array that do not exist. It is looking for a rolls[11] rolls[12] and neither of those exist.[/i][/i]
```
Link to comment
Share on other sites

> When you initalized your array you did "int rolls[10]" this gave you an array that had 11 slots because your array starts at zero meaning you have spaces "0 1 2 3 4 5 6 7 8 9 10". What Marshy Dearest Dearest is trying to show you is that when you did this
>
> ```
>
> for (int i = 2; i < 13; i++) cout << setw(5) << i << setw(15) << rolls[i] << setw(15) << float(rolls[i]) / sim << setw(15) << i / 36.0 << endl;
>
> you have
>
> int i = 2; i< 13;
>
> So essentially you are trying to reference parts of the array that do not exist. It is looking for a rolls[11] rolls[12] and neither of those exist.
>
> oh thank you! wow i am stupid i didn't even notice i was doing that until you pointed it out.
>
> Thank you Marshy Dearest.[/i][/i]
> ```
Link to comment
Share on other sites

> When you initalized your array you did "int rolls[10]" this gave you an array that had 11 slots because your array starts at zero meaning you have spaces "0 1 2 3 4 5 6 7 8 9 10". What Marshy Dearest is trying to show you is that when you did this
>
> ```
>
> for (int i = 2; i < 13; i++) cout << setw(5) << i << setw(15) << rolls[i] << setw(15) << float(rolls[i]) / sim << setw(15) << i / 36.0 << endl;
>
> you have
>
> int i = 2; i< 13;
>
> So essentially you are trying to reference parts of the array that do not exist. It is looking for a rolls[11] rolls[12] and neither of those exist.
>
> Close
>
> You get 0 1 2 3 4 5 6 7 8 9
>
> 10 values. 0 to 9.
>
> Which is why you still get errors.
>
> So the first loop fails that i pointed out becouse it is reading 1 to high. The second one fails becouse you start at 2 not 0\. So you are trying to read 2 - 12\. nothing above 9 exists.[/i][/i]
> ```
Link to comment
Share on other sites

Here is the working code if you want it

```

#include

#include

#include

using namespace std;

int main(int argc, char *argv[])

{

//Declare Variables

int rolls[11];

int sim = 0, temp = 0;

cout.setf(ios::fixed);

cout.precision(4);

//Timer

time_t seconds = 0;

// time(&seconds);

srand((unsigned int)seconds);

//Initialize values for elements in the array

for (int i = 0; i < 10; i++)

rolls[i] = 0;

//Prompt user for the number of simulations

cout << "Enter the number of simulations: ";

cin >> sim;

//Display header(Table)

cout.setf(ios::left);

cout << setw(5) << "Value";

cout.unsetf(ios::left);

cout.setf(ios::right);

cout << setw(15) << "Frequency" << setw(15) << "Exp Prob" << setw(15) << "Theo Prob" << endl;

cout.unsetf(ios::right);

//Loop the simulation

for (int i = 1; i <= sim; i++)

{

temp = rand() % 6 + 1 + rand() % 6 + 1;

rolls[temp - 2]++;

}

cout.setf(ios::right);

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

cout << setw(5) << i << setw(15) << rolls[i] << setw(15) << float(rolls[i]) / sim << setw(15) << i / 36.0 << endl;

system("PAUSE");

return EXIT_SUCCESS;

}

temp - 2 so you can fit the 12 into the 10 slot and the 2 will go into the zero spot. I also expanded your array to 11 instead of 10.[/i][/i][/i]

```
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...