Saturday, 26 November 2011

WHY ARRAY INDEX STARTS FROM ZERO

Honestly speaking, the doubt is not mine. I rarely think about C language so much that I get such genuine doubts. Its a friend's doubt. The list of answers I got goes here.

In programming languages like C the array name is essentially a pointer, a reference to a memory location, so the expression a[n] refers to a memory location away from n elements in the array. The first element in the array is exactly contained in the memory location that the array refers (0 elements away) so it should be denoted as a[0].

Another mathematical solution I found out really took me nowhere. So instead of typing my interpretation, I'm copy-pasting it here, so if anyone understood what it means, enlighten me.


"Dijkstra explains why we should index from 0. This is a problem on how to denote a subsequence of natural numbers, say for example 1,2,3,...,10. We have four solutions available:
a. 0<i<11 
b. 1
<=i<11c. 0<i<=10d. 1<=i<=10Dijkstra argues that the proper notation should be able to denote naturally the two following cases:
1. The subsequence includes the smallest natural number, 0
2. The subsequence is empty


Requirement 1. leaves out a. and c. since they would have the form -1<i which uses a number not lying in the natural number set (Dijkstra says this is ugly). So we are left withb. and d. Now requirement 2. leaves out d. since for a set including 0 that is shrunk to the empty one, d. takes the form 0<=i<=-1, which is a little...well, messed up! Subtracting the ranges in b. we also get the sequence length, which is another plus. Hence we are left with b. which is by far the most widely used notation in programming now."




There's  another explanation, by convention. It's easier to count the numbers from zero it says. Even the eight bits in a computer start from zero.

There are many other explanations and may be all of them are correct, but only these three interested me.
The first one seems most correct. the second one is correct may be because a mathematician explained it. :-) Well, so that's why the array index starts with zero, if you can find only one reason. ;-)

8 comments:

  1. Great post. I was checking continuously this blog and I'm impressed! Extremely useful info specifically the last part :) I care for such information much. I was looking for this particular information for a very long time. Thank you and good luck.

    Feel free to visit my weblog: hardwood floors

    ReplyDelete
  2. Great post. I was checking continuously this blog and I'm impressed! Extremely useful info specifically the last part :) I care for such information much. I was looking for this particular information for a very long time. Thank you and good luck.

    Feel free to visit my blog ... hardwood floors
    My web page - hardwood floors

    ReplyDelete
  3. Hey, I think your blog might be having browser compatibility
    issues. When I look at your blog in Ie, it looks
    fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that, terrific blog!


    Look at my blog post: just click the following website

    ReplyDelete
  4. Write more, thats all I have to say. Literally, it seems as
    though you relied on the video to make your point.
    You clearly know what youre talking about, why waste your intelligence on just posting videos to your site when
    you could be giving us something informative to read?


    hardwood floor

    ReplyDelete
  5. Write more, thats all I have to say. Literally, it seems as though you relied on the video
    to make your point. You clearly know what youre talking about, why waste your intelligence on just posting videos to your site when you could be giving us something informative to read?

    hardwood floor
    my web page > hardwood floor

    ReplyDelete
  6. Paragraph writing is also a fun, if you be familiar with after that you can
    write otherwise it is complex to write.

    Also visit my web page - zetaclear reviews

    ReplyDelete
  7. Hey! I could have sworn I've been to this blog before but after reading through some of the post I realized it's new to me.
    Anyhow, I'm definitely delighted I found it and I'll be bookmarking and checking back often!


    my web-site; housekeeper seattle

    ReplyDelete
  8. May I simply say what a comfort to discover somebody that
    genuinely knows what they're discussing over the internet. You certainly realize how to bring an issue to light and make it important. More people must look at this and understand this side of the story. It's surprising you're not more popular given that you surely have the gift.

    Here is my weblog - hair treatment

    ReplyDelete

I love comments!!