Problem in bidimensional arrays

Hi, I have a problem in my program and the origin is that it won't do what I exactly said. I put 0 on every array cell thet atribute the number 1 on ONE cell but, in the output, shows me 2 cell with the number 1! What's wrong? :/

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
#include <iostream>

using namespace std;

int main()
{
    int d[3][3];
    for (int n=0; n<4; n++) //for atribute the valor 0 to every cell
    {
        for (int m=0;m<4;m++) 
        {
         d[m][n] = 0;
        }
}

    d[1][0]=1;

    cout << d[0][0] << " " << d[1][0] << " " << d[2][0] << " " << d[3][0] << endl;
    cout << d[0][1] << " " << d[1][1] << " " << d[2][1] << " " << d[3][1] << endl;
    cout << d[0][2] << " " << d[1][2] << " " << d[2][2] << " " << d[3][2] << endl;
    cout << d[0][3] << " " << d[1][3] << " " << d[2][3] << " " << d[3][3] << endl;
           
cin.get();
cin.get();
return 0;

}



0 1 0 0
0 0 0 0
0 0 0 0
1 0 0 0



You're getting trash values in your array, you allocate your array with:
 
int d[3][3];


So this means that the maximum index you should be accessing in this array is:
 
d[2][2];


Arrays start at 0, so if you allocate an array with size 3, it will go 0, 1, 2. If you try and access 3, you are likely to get 'trash' values in that memory address.
Thanks, it solves the problem just doing int d[4][4]; :)
Topic archived. No new replies allowed.