Maze and mole game help

I am working on a maze and mole game. I keep getting "Run-time check failure #2- stack around 'row' was corrupted" and I cannot get the percentage equation working can anyone help. below is my coding.

// Steve Hayton
// CIS 150-01
// Mole and Maze Game

# include <iostream>
#include <math.h>
using namespace std;
int main()
{
int row[200]={0}, col[200]={0};
int c=0;
int f=0
int n;
int p;


for (int i=0; i<500; i++)
{
row[0] = 3;
col[0] = 2;

while(int j=1; j<=100 ; j++)
{
row[j] = row[j-1];
col[j] = col[j-1];
n=rand()%4+1;

switch (n)
{ case 1: row [j] ++;
break;
case 2: col [j] ++;
break;
case 3: row [j] --;
break;
case 4: col [j] --;
break;
}
if (row[j] ==3 && col[j] ==9)
{
c++;
cout << "Solution Found" << endl;


for(int k =0; k <j+1; k++)
{
cout << "(" << row[k] <<","<<col[k]<<")";

if (f==12)
{
cout <<endl;

}
}

j=100;
row[j] =10;
col[j] =10;
cout << endl;
}
}

}
p = c*100/500;

cout << "the pecentage of escapes is: " <<p <<endl;
system ("pause");
return 0;
}
Please use code tags:
http://www.cplusplus.com/articles/jEywvCM9/

In your first for loop, you iterate through 500 indexes, but your arrays only have 200 indexes, so 300 of the indexes you assign to are invalid and thus result in you corrupting memory.
I don't know how you would get any such error, since this code will not compile (and therefore won't run.)

I suspect your code didn't actually compile and you're running a previously compiled version of your program.

Rewritten to be readable (with syntax errors corrected:)

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    // srand(time(0));

    const unsigned trials = 100 ;
    unsigned solutionsFound = 0;

    for (unsigned i = 0; i < trials; ++i)
    {
        const unsigned size = 101;

        int row[size];
        int col[size];

        row[0] = 3;
        col[0] = 2;

        for (unsigned j = 1; j < size; ++j)
        {
            row[j] = row[j - 1];
            col[j] = col[j - 1];

            switch (rand() % 4)
            {
            case 0: ++row[j]; break;
            case 1: ++col[j]; break;
            case 2: --row[j]; break;
            case 3: --col[j]; break;
            }

            if (row[j] == 3 && col[j] == 9)
            {
                ++solutionsFound;

                for (unsigned k = 0; k < j + 1; ++k)
                {
                    cout << '(' << row[k] << ',' << col[k] << ") ";

                    if ((k + 1) % 12 == 0 || k == j)
                        cout << '\n';
                }
                cout << endl;
                break;
            }
        }
    }

    cout << "Escapes: " << (solutionsFound * 100.0 / trials) << "%\n";
}


http://ideone.com/uCaVet
Last edited on
Thanks for the help I greatly appreciate it.
Topic archived. No new replies allowed.