Arrays are 0 indexed. x
and y
are the size of the arrays. You can't use the size value as an index value.
For instance:
a
is of size 5. "a"
is at index 0, "e"
is at index 4. "e"
is the 5th element in the list. When you try to use the size of 5 as the index, well there is no element at index 5, as you can see. The last element would be accessed a[size - 1]
. Though remember, size of 0 results in an index of -1, which is an invalid index.