Access violation?
<p>I was trying to write a simple Monte Carlo simulation program. To be exact, I wanted to analyze the combat results depending on the varying army sizes on both offense and defense - something in tune of this: <a href="http://en.wikipedia.org/wiki/Risk_(game)#Dice_probabilities" rel="nofollow">http://en.wikipedia.org/wiki/Risk_(game)#Dice_probabilities</a></p>

<p>Now... Risk II Same time rule offers different kind of challenge: varying army size means different color of dice (which means different distribution function for the numbers) In short, the smaller the size of your army is, the more likely you'll end up with 1s, while the larger the size of your army is, the more likely you'll end up with higher number of rolls. </p>

<p>Since using if statements for all the possible condition was a colossal stupidity at its finest, I tabulated all the possible rolling results in 5x12 array. (12 sides in all of the dice, and 5 varying strength, so you get 5x12) </p>

<p>I thought of carrying out 10000 simulations for each offense/defense combinations but once I realized that would mean over 9 million calculations, I decided to cut it short at 100 per combination. </p>

<p>The following is the code; once I run it, it gives me the Access Violation error. I don't know where I made an error. If there is any advice you could offer, I'd appreciate that too. Thanks in advance. </p>

<pre><code>/* Risk II Combat Result Table 

For starter, we shall consider one-direction attack in RISK II
and generate the combat table for use.

Machine: Pentium Dual Core E6600 
Ram: 6G
OS: Windows 7
Compiler: Visual Studio 2010

Jimmy Smith, 24-March-2012

*/

#include &lt;cstdio&gt;
#include &lt;cstdlib&gt;
#include &lt;cmath&gt;

#include &lt;iostream&gt;
#include &lt;algorithm&gt;
using namespace std;

/* Initializing: 

    Range legend:

White  = 1 ~ 6
Yellow = 7 ~ 12
Orange = 13 ~ 20
Red    = 21 ~ 30
Black  = 31 ~

First row of Dice array corresponds to white dice, and so on. 

*/

int Dice[5][12] = { {1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5, 6}, 
                    {1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 6}, 
                    {1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6}, 
                    {1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6}, 
                    {1, 2, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6} };

int Roll_Index [30]= {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4};


int main() {

float Table[30][30];

for (int i = 0; i &lt; 30; i ++)
    for (int j = 0; j &lt; 30; j ++)
        Table [i][j] = 0.0;

int Result[100];

for (int i = 0; i &lt; 100; i++) Result[i] = 0;    

float prob = 0.0;

int Atk = 0;
int Def = 0;    //Number of attackers and defenders


int A_Ind = 0;
int D_Ind = 0;  //Dice side index

int A_Roll_Index = 0;   
int D_Roll_Index = 0; //Roll index on both sides

int A_Dice = 0;
int D_Dice = 0; //Roll result

int Damage = 0;

int Sum = 0; //Internal sum

FILE* fp;


//Time for hard core Monte-Carlo shit! 100 simulation for each situation

for (Atk = 0; Atk&lt;30; Atk++) {

    for (Def = 0; Def &lt; 30; Def++) {

        for (int i = 0; i &lt; 100; i++) {

            int Attacker = Atk +1;
            int Defender = Def +1;

            while((Attacker&gt;0)&amp;&amp;(Defender&gt;0)) {

                A_Ind = (int)(rand()*12);
                D_Ind = (int)(rand()*12); //The die is cast!

                A_Roll_Index = Roll_Index[Attacker-1];
                D_Roll_Index = Roll_Index[Defender-1];

                A_Dice = Dice[A_Roll_Index][A_Ind];
                D_Dice = Dice[D_Roll_Index][D_Ind];

                Damage = min(A_Roll_Index, D_Roll_Index) + 1;

                if (A_Dice &gt;= D_Dice) {
                    Defender -= Damage;
                    if (Defender == 0)  Result[i] = 1;
                }
                else {
                    Attacker -= Damage;
                    if (Attacker == 0)  Result[i] = 0;
                }
            }
        }

    for (int i = 0; i &lt; 100; i++)   Sum+=Result[i];

    prob = (float)(Sum/100);

    Table[Atk][Def] = prob;

    }
}
/* open new file for output and write a title */

fp = fopen( "Combat.dat", "w+");

if( NULL == fp ) {
    printf( "cannot open file\n" );
    return( 0 );
}

for (Atk = 0; Atk &lt; 30; Atk++){
    for (Def = 0; Def &lt; 30; Def++)
        fprintf(fp, "%16.8f", Table[Atk][Def]);
    fprintf (fp, "\n");
}
fclose( fp );

return(EXIT_SUCCESS);
</code></pre>

<p>}</p>
