Reply/comments embedded...
> -----Original Message-----
> From: darkshadow_x21 [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, February 09, 2005 9:16 AM
> To: [email protected]
> Subject: [C-Paradise] Dice Program problem
>
>
>
> i was trying out this dice problem and i could not figure out how to
> get the percentages of each number to print out. this what i have so
> far and i was wondering if any of u could give me a hint as to what
> to do. thx in advance.
>
> #include <iostream.h>
^^^^^^^^^^
Should be <iostream>
> #include <stdlib.h>
^^^^^^^^
Should be <cstdlib>
And C++ Standard Library's names are in 'std' namespace. For trivial
program, you can use:
using namespace std;
> void main ()
^^^^
main() should return 'int'.
> {
> randomize ();
^^^^^^^^^^^^
Not standard function. The standard function is srand(). Typical use is:
srand((unsigned int)time(0));
time() required <ctime>
> int c,a,b;
> int total[12];
^^^^
Don't use magic number. In C++, use const int.
> for (int j=2;j<=12;j++)
^^^^^
Wrong. In C/C++, array is zero-based. So for:
T Ary[N]
Valid elements are Ary[0] to Ary[N-1].
> {
> total[c]=0;
> }
You can drop the for-loop and initialized the array at declaration:
const int MAX_TOTAL = 13;
...
int total[MAX_TOTAL] = { 0 };
MAX_TOTAL is 13 so that valid subscript for 'total' is [0, 12], corresponds
to the actual dice number.
> for (int i=0;i<=10;i++)
^^
Don't use magic number.
const int MAX_ROLL_COUNT = 10;
10 roll is probably good just to debug your code. You need a much bigger
value to make this simulation realistic.
> {
> a=random (6)+1;
> b=random (6)+1;
random() is not standard function. The standard function is rand() and it
returns pseudo random number between [0, RAND_MAX]. The simplest method to
get random number in range [1, 6] is:
a = rand() % 6 + 1;
Read here about using modulus and rand():
< http://www.eskimo.com/~scs/C-faq/q13.18.html >
< http://www.eskimo.com/~scs/C-faq/q13.16.html >
> c=a+b;
Or simply:
c = rand() % 6 + 1 +
rand() % 6 + 1;
And you do want to increment the corresponding 'total' element here:
++total[c];
> cout<<"Number rolled = "<<c<<endl;
>
> }
> for (j=2;j=12;j++)
> {
> total[c]=total[c]+1;
> cout<<j<<" "<<total[c]<<endl;
> }
To print the percentage of the outcome, you take the
for (int n = 1; n < MAX_TOTAL; ++n)
{
cout << n << ": "
<< static_cast<double>(total[c]) / MAX_ROLL_COUNT
<< endl;
}
static_cast is used to make one of the operands a double type, so that the
compiler will generated code for floating-point division.
Note that you cannot:
static_cast<double>(total[c] / MAX_ROLL_COUNT);
Because both operands of the division operator are integer type, only
integer division is performed. The result is an integer and then cast to
double type.
HTH
Shyan
>-----------------------------------------~-~>
CHECK THE ARCHIVE BEFORE POSTING!!!! Archive is available at
http://www.eScribe.com/software/C-Paradise/
>------------------------------------------_->
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/C-Paradise/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/