Understanding 2D arrays of ints

I have a program that requires a 2D array of ints to create a grid to be used for AI pathing. However when I output the grid with the below code excerpt, it creates 12 rows as opposed to the 11 that the grid is initialized to.
The program worked perfectly well in an earlier iteration using chars instead of ints, but is this an "off-by-one" error on my part? Or is there some differentiation I need to make in the code between using chars and ints?

Code snippet:
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
#include <iostream>
#include <iomanip>
using namespace std;

void outputMap();

    const int nMatrixSizeX = 11;
    const int nMatrixSizeY = 11;
    int nScenario[nMatrixSizeX][nMatrixSizeY] = 
    {
    {0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0}
    };
    

int main()
{
    outputMap();
    
    system("PAUSE");
    return 0;
}

void outputMap()
{

    for(int i = nMatrixSizeX; i > -1; i--)
    {
            for(int n = 0; n < nMatrixSizeY ; n++)
            {
                    cout << setw(2) << nScenario[i][n] << setw(1);
            }
            cout << endl;
    }
    cout << endl;
}


0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0

Press any key to continue...
If you are going to initialize all elements of an array with zeroes you can do it much simpler then you do in your code.

int nScenario[nMatrixSizeX][nMatrixSizeY] = {};


As for your question then let consider your loop

1
2
3
4
5
6
7
8
    for(int i = nMatrixSizeX; i > -1; i--)
    {
            for(int n = 0; n < nMatrixSizeY ; n++)
            {
                    cout << setw(2) << nScenario[i][n] << setw(1);
            }
            cout << endl;
    }


You are outputing elements as nScenario[i][n] where initila values of i and n are nMatrixSizeX and nMatrixSizeY. However you have not a row with index equal to nMatrixSizeX. So your code is invalid. You could change it the following way

1
2
3
4
5
6
7
8
    for(int i = nMatrixSizeX; i != 0; i--)
    {
            for(int n = 0; n < nMatrixSizeY ; n++)
            {
                    cout << setw(2) << nScenario[i-1][n] << setw(1);
            }
            cout << endl;
    }

Ah thanks a lot! :) So it was just an off by one error on my part I couldn't spot! Those things are always catching me out.

And the 2D array is actually going to be used to practice AI pathing, so later on i'm going to implement "1"'s that will be impassable terrain so I need to be able to see all the data elements

In anycase thanks a lot :)
Topic archived. No new replies allowed.