in reply to why the array index has to start at 0??
That's actually a very good question. The most common explanation you'll find is because the index represents an offset from some memory address, so naturally the address of the first element will be at offset 0. But there's another reason which I came across some time ago and was very well explained by some mathematician. It was something to do with boudary checks and the argument was that i<10 is better than i<=10. Unfortunately, I can't remember the details nor can I find the web page.
There are some situations where having the index start at 1 is needed like in some matrix computations if I am not mistaken. But that's not a problem in any language.
Update: It was really bugging me that I couldn't recall who put the really good range argument, so I kept looking for it and here it is:
When dealing with a sequence of length N, the elements of which we wish to distinguish by subscript, the next vexing question is what subscript value to assign to its starting element. Adhering to convention a) yields, when starting with subscript 1, the subscript range 1 ≤ i < N+1; starting with 0, however, gives the nicer range 0 ≤ i < N. So let us let our ordinals start at zero: an element's ordinal (subscript) equals the number of elements preceding it in the sequence. And the moral of the story is that we had better regard —after all those centuries!— zero as a most natural number.
The argument was put by none other but E.W. Dijkstra
Taken from http://lambda-the-ultimate.org/node/1950
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: why the array index has to start at 0??
by Marshall (Canon) on Jun 23, 2009 at 13:20 UTC | |
Re^2: why the array index has to start at 0??
by ikegami (Patriarch) on Jun 23, 2009 at 15:31 UTC | |
Re^2: why the array index has to start at 0??
by Zen (Deacon) on Jun 23, 2009 at 14:47 UTC | |
by roboticus (Chancellor) on Dec 21, 2012 at 16:32 UTC |