Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: why the array index has to start at 0??

by pKai (Priest)
on Jun 23, 2009 at 12:50 UTC ( #774001=note: print w/ replies, xml ) Need Help??


in reply to why the array index has to start at 0??

When you were in your 1st year of life, you could say that was year "0".

Things get confusing (for many people) when numbers start to get bigger: The 20th century were the years 19xx (mostly). Did the 2nd millenium end on New Year's Eve 1999 or 2000?

For mathematicans or sientists in general (as mathematics is nowadays a big part of every sience) it is quite natural to count from zero.

Programming languages were developed by sientists mostly. So there is the historic background.

About the only language coming to my mind with 1-based arrays is BASIC. But looking at the BASIC mainline nowadays (VB.NET that is), Microsoft has also evened that out. While you have the possibility to declare arrays with arbitrary lower and upper bounds, a standard array will index its elements from zero.

Also going for the "natural" argument again. As an example in Perl look at the possibility to access the last array element via $array[-1]. This seems just right, concidering that the first element is $array[0]. Conversely with $array[1] as the first element. By allowing round-robin-indexing you end up with $array[0] as the last element. Which looks weird, in my eyes at least.


Update: Some minor typoes fixed.


Comment on Re: why the array index has to start at 0??
Select or Download Code
Re^2: why the array index has to start at 0??
by Porculus (Hermit) on Jun 23, 2009 at 13:43 UTC
    Things get confusing (for many people) when numbers start to get bigger: The 20th century were the years 19xx (mostly). Did the 2 millenium end on New Year's Eve 1999 or 2000?

    The latter, logically speaking. The mass media managed to convince the ignorant masses otherwise, but there you go.

    It is ironic in this context to note that people would have got this right intuitively if years, and centuries, had been zero-based rather than the supposedly "natural" one-based system we have. If we'd used a zero-based system, with century 0 starting in the year 0, then the year 2000 would have represented the start of century 20, rather than the end of the 20th century. Problem solved.

    Instead we have a silly situation where technically centuries are 1-based, but people constantly try to press for a 0-based system that seems more natural to them. People want the 21st century to begin with the year 00, not the year 01!

Re^2: why the array index has to start at 0??
by CountZero (Bishop) on Nov 15, 2013 at 19:26 UTC
    When you were in your 1st year of life, you could say that was year "0".
    Actually that is very culturally determined. The daughter of my Thai girlfriend was born in 2005. This year she turned 8 years old, but got 9 candles on her birthday cake! In Thailand one gets best wishes for the next year of your life, not for the life that just passed.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://774001]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2014-12-29 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (193 votes), past polls