# Created a matrix but I found it has extra rows than intended

here is how I created my matrix
int mapp[len][wid];

through this command I made elements in the matrix zeroes
for(int i = 0; i < len; i++) for(int j = 0; j < wid; j++) mapp*[j] = 0;*
When I tried to have another loop to end when I have made all the elements zeroes, it actually ran until row = 4, and column = 6, meaning that I have 5 rows and 7 columns when I set the values of len = 3 and wid to 5.
I’m not sure why it does this
I first wrote this program in python and just transferred it into Arduino ide
were using numpy and worked perfectly fine.

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//user input
int len = 3; //row
int wid = 5; //col

//create map
int mapp[len][wid];

//define initial position
int row, col;
row = 0;
col = 0;

//initializing
for(int i = 0; i < len; i++) for(int j = 0; j < wid; j++) mapp*[j] = 0;*

• Serial.print(mapp);*
}
heres what I have, the last line of serial print just for me to check where my matrix ends intead of using a loop, I guess the Arduino doesn’t return error when I go out of intended size. not sure why, please explain

First of all, you should use code tags to place your Arduino Code. Makes it easy to understand.

Moreover, in your for loops, you are using mapp[j] = 0; instead, it should be mapp[i ] [j] = 0; and use curly brackets for for loops. Here’s the correct code, I think it will work, I haven’t tested it though:

``````void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//user input
int len = 3; //row
int wid = 5; //col

//create map
int mapp[len][wid];

//define initial position
int row, col;
row = 0;
col = 0;

//initializing
for(int i = 0; i < len; i++) {
for(int j = 0; j < wid; j++){
mapp[i][j] = 0;
}
}

Serial.print(mapp);
}
``````

I'm a little surprised that the compiler didn't notice that you were indexing way outside your array and warn you about it.