Wednesday, March 6, 2013

why does array index start from zero?



  • why does array index start from zero?

To use a data item, you must calculate its correct address


Why do indexes of arrays start with zero?
The first location of an array is addressed by the pointer to the array.
Subsequent locations in the array are indicated by an offset from that pointer.
http://www.linkedin.com/groupItem?view=&gid=70526&type=member&item=218956807&commentID=123192360&report.success=8ULbKyXO6NDvmoK7o030UNOYGZKrvdhBhypZ_w8EpQrrQI-BBjkmxwkEOwBjLE28YyDIxcyEO7_TA_giuRN#commentID_123192360



  • Here's some C code to explain the offsets a little better:

Code:

int array[3];

int* parray = &array;

int val0 = *(parray + 0);   // 1st element (note the +0 is not required)
int val1 = *(parray + 1);   // 2nd element
int val2 = *(parray + 2);   // 3rd element

http://ubuntuforums.org/showthread.php?t=1275797


  • Zero-based numbering

Advantages
One advantage of this convention is in the use of modular arithmetic as implemented in modern computers. Usually, the modulo function maps any integer modulo N to one of the numbers 0, 1, 2, ..., N - 1, where N = 1. Because of this, many formulas in algorithms (such as that for calculating hash table indices) can be elegantly expressed in code using the modulo operation when array indices start at zero.

A second advantage of zero-based array indexes is that this can improve efficiency under certain circumstances. To illustrate, suppose a is the memory address of the first element of an array, and i is the index of the desired element. In this fairly typical scenario, it is quite common to want the address of the desired element. If the index numbers count from 1, the desired address is computed by this expression:

    a + s × (i - 1)

where s is the size of each element. In contrast, if the index numbers count from 0, the expression becomes this:

    a + s × i

This simpler expression can be more efficient to compute in certain situations.

A third advantage is that ranges are more elegantly expressed as the half-open interval, [0,n), as opposed to the closed interval, [1,n], because empty ranges often occur as input to algorithms (which would be tricky to express with the closed interval without resorting to obtuse conventions like [1,0])

http://en.wikipedia.org/wiki/Zero-based_numbering




  • It starts at 0 because the index value tells the computer how far it needs to move away from the starting point in the array.

if you use an array (without giving an index number in brackets), you are really only referring to the memory address of the starting point of the array
When you reference a specific value in the array, you are telling the programming language to start at the memory address of the beginning of the array and then move up the memory address as needed
lets say a program allocates space in the memory between address numbers 1024 and 2048 and it starts at 1024 and each item in the array is 8 bytes long
arrayName[0] is the same as telling it to look at the data stored at the memory address of (1024 + (0 * 8)), or 1024
If you want the value stored at arrayName[1], then it looks at the value held at (1024 + (1*8)), which is 1032
If you wanted the value held at arrayName[10], then it would look at the data stored in the memory address of 1024 + (10 * 8), which is 1104.
http://answers.yahoo.com/question/index?qid=20090528223815AACZDDY



  • The discussion over why the array index should start at zero is not a trivial one and relates to interesting concepts from computer science. 

First of all, it has strong relation to language design. For example in C, the name of an array is essentially a pointer, a reference to a memory location, and so the expression array[n] refers to a memory location n-elements away from the starting element.
This means that the index is used as an offset.
The first element of the array is exactly contained in the memory location that array refers (0 elements away), so it should be denoted as array[0]
http://developeronline.blogspot.com/2008/04/why-array-index-should-start-from-0.html

No comments:

Post a Comment