Article: What I Hate About Your Programming Language Subject: C, C++, Java, maybe others - zeroth element arrays Date: 2003-05-15 17:24:19 From: anonymous2 Response to: C, C++, Java, maybe others - zeroth element arrays Short version: 1. Ask not whether to start at zero or one. Ask whether to place zero at the start of the array or the end. 2. Ask not whether to use zero or one based indexing. Ask whether to use [0, 1) or (0, 1] based indexing. Long version: Think of the indexes as *between* the elements. Like inches on a ruler. The first inch is between the zero mark and the one mark. So we call that inch [0, 1). If we want the second and third inches together we want [1, 3). That is starting at the 1 mark and ending at the 3 mark. (Note that 3 minus 1 is the number of inches as nice side effect.) You also see this effect in birthdays. Your first year of live is between your zero'th birthday and your first birthday and so on. Now programmer hate to have to write [n, n+1) (or [n - 1, n)) all the time so we need a shorthand. We can take either the first number (zero based) or the second number (one based). It usually wont matter unless your array can use negative numbers. The most common (but not only) case of this is a circular array where negative numbers index from the end of the array. Zero will have to be placed either at the end or the begining of the array (we're *not* going to number them like we do our years). So the real question is whether zero goes at the begining or the end of the array. (Keep in mind where talking about shorthand notation. They are really elements [0, 1) and [-1, 0).) Note that 0 is an element of [0, 1) but 1 isn't so we should prefer calling it 0 instead of 1. This may be a bias because we could also use [0, 1], (0, 1), or (0, 1]. Let's examine these. I shouldn't have to explain why [0, 1] and (0, 1) are completely out of the question (ask any Math professor). So that brings it down to [0, 1) vs. (0, 1]. In a certain sence which we choose is arbitrary, but Mathematicians have generally settled on [0, 1). Maybe it's because they are more intrested in the set of non-negative numbers, [0, INF), than the set of non-positive numbers, (-INF, 0]. I don't know why but it *is* the convention to prefer [x, y) (read "x upto but not including y") over (x, y] (I have no idea how to read that).

1 to 1 of 1
1 to 1 of 1