Simulation - Stepping out of bounds?

I am writing a simulation program. In the code below I am using cout << action << endl; to test. I am getting some action results to be outside of the array. I cannot find, in my code, where the subscript is being blown.

Here is the entire program for reference:
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// Name: Jordan Fleming Wright
// Assignment: Random Walk
//

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;

void fillArray (int ary[][14]);
void getMove (int step, int &r, int &c);
int getResult (int ary[][14], int r, int c, int action, int &complete, int &rescue);

int main()
{
    int island[13][14];
    int step, completed = 0, rescued = 0;
    int rPos = 6, cPos = 0, action = 0;
    int totalCost = 0;
    
    unsigned seed = time(0);
    srand (seed);
    
    fillArray (island);
    
    step = rand() % (100 + 1);
    getMove (step, rPos, cPos);
    action = island[rPos][cPos];
    while (action != 0)
    {
        step = rand() % (100 + 1);
        getMove (step, rPos, cPos);
        action = island[rPos][cPos];
    }
    
    //Start the simulation.
    for (int sim = 0; sim < 5000; sim++)
    {
        int cost = 0, action = 0;
        
        while (action >= 0 && action <= 2)
        {
            step = rand() % (100 + 1);
            //cout << step << endl;
            getMove (step, rPos, cPos);
            action = island[rPos][cPos];
            cout << action << endl;
            cost += getResult (island, rPos, cPos, action, completed, rescued);
        }
        
        totalCost += cost;
        
    }
    cout << totalCost << endl;
    
    cout << "The average cost of Harvey's walk: $" << (totalCost / 5000.0) << endl;
    cout << "The number of times Harvey made it to a bridge: " << completed << endl;
    cout << "The number of times Harvey had to be rescued: " << rescued << endl;
}

void fillArray (int ary[][14])
{
    //Water-------------------------//
    for (int c = 0; c < 14; c++)    //
    {                               //
        ary[0][c] = -1;             //
        ary[13][c] = -1;            //
    }                               //
                                    //
    for (int r = 1; r < 6; r++)     //
    {                               //
        ary[r][0] = -1;             //
        ary[r][13] = -1;            //
    }                               //
                                    //
    for (int r = 7; r < 12; r++)    //
    {                               //
        ary[r][0] = -1;             //
        ary[r][13] = -1;            //
    }                               //
    //------------------------------//
    
    //Footpath
    for (int c = 1; c < 13; c++)
    {
        ary[6][c] = 0;
    }
    
    //Flowers-----------------------//
    for (int r = 1; r < 6; r++)     //
    {                               //
        for (int c = 1; c < 13; c++)//
        {                           //
            ary[r][c] = 2;          //
        }                           //
    }                               //
                                    //
    for (int r = 7; r < 12; r++)    //
    {                               //
        for (int c = 1; c < 13; c++)//
        {                           //
            ary[r][c] = 2;          //
        }                           //
    }                               //
    //------------------------------//
    
    //Bridges
    ary[6][0] = 3;
    ary[6][13] = 3;
}

void getMove (int step, int &r, int &c)
{
    //Step forward
    if (c < 13)
    {
        if (step >= 0 && step <= 45)
        {
            c++;
        }
    }
    
    //Step right
    if (r < 12)
    {
        if (step > 45 && step <= 65)
        {
            r++;
        }
    }
    
    //Step left
    if (r > 0)
    {
        if (step > 65 && step <= 90)
        {
            r--;
        }
    }
    
    //Step back
    if (c > 0)
    {
        if (step > 90 && step <= 100)
        {
            c--;
        }
    }
}

int getResult (int ary[][14], int r, int c, int action, int &complete, int &rescue)
{
    int cost = 0;
    //Fell in water. Must be rescued.
    if (action == -1)
    {
        rescue++;
    }
    
    //Made it to one of the two bridges.
    if (action == 3)
    {
        complete++;
    }
    
    //Stepped on one of two flowers in a cell. Owes $5.
    if (action == 2)
    {
        cost = 5;
        ary[r][c]--;
    }
    
    //Stepped on the last flower in a cell. Owes $5.
    if (action == 1)
    {
        cost = 5;
        ary[r][c]--;
    }
    
    return cost;
}
So... anyone?
Here is one place:
ary[13][c] = -1;
You defined your array with a size of [13][14], remember arrays start at zero and stop at size - 1.

You also have a lot of "magic numbers" in your code, like the 13 in the above snippet, you should consider using constants instead.

And is there a reason you're using raw arrays instead of std::array or std::vector, or even a structure or a class?

Yep, I missed that one. Thank you.

The reason I hard coded so many numbers was just the nature of filling the array was very specific and I felt like there would be a ton of constants, but I may reconsider.

As far as the arrays, it's for a class and I'm just going by what my instructor has told us to do.
Topic archived. No new replies allowed.