Kevin Jones Kevin Jones - 13 days ago 6
C Question

Checking if an array is in bound or out of bound

So basically, I have this 2-D array that looks like this.

######...s
e......###


It suppose to print a * from s-e only if theres a "." , the "#" is like a wall.

So I check all the possible location but for some reason its not outputting a "*" to e.

I believe it because of something to do with it being out of bound.

I did some check for example like

if(array.x <=row && array.y<= col && array.x >=0 && array.y >=0)


Is this a proper way to check if I'm inside or out of bound?

Btw this is an 2d array

Answer

Assuming row is the number of rows, col is the number of columns, array.x and array.y are the indices, a proper check for indices in bounds is:

if(0 <= array.x && array.x < row && 0 <= array.y && array.y < col) {
   ...
}

Note the strict inequality with row and col.

Comments