Friday, 2 January 2015

Why array index start with zero?

Here I gathered
Ans1:-
1) In my opinion, 0 is the lowest positive value that is why it starts from 0.
Reasons
1.In very earlier languages like pascal, c, logic computer memory was very limited hardly in bytes, also their not os like windows or linux
so programmers them selves had to locate memory loc to variables as we done even in 8085 kit
2.all computer data is digitally organized(ie in binary) even today
0- 0000
1-0001
2 0010
3-0011
...
3.so their was a physical existance of zeroth location in computers
4.for better memory loc calculation it was a trend to have arrays starting with zeroth loc, so is now

Ans2:-
Simple for computers, but not for humans... We don't, or shouldn't care how the computer maps memory. The compiler should handle this.
Programming languages should make it simple to model every day objects in life. As humans we don't use zero as a base. For example:
January is the first month of the year, not the zero month.
"A" is the first letter of the alphabet, not the zero letter.
Other programming languages, like Cobol use 1 as the starting index of an array. So it can be done.

Ans3:-
u see in general the architecture of memory in computers has the memory starting from 0 for xample if there are 5 bit memory the first location would be 00000 then the next would b 00001 and so on
in general the compiler allocates memory to contents of program while creating exe file in the memory
so when contiguous locations are allocated the first one will b identified just as the original memory
thats the reason why the indexing starts from 0

Ans4:-
this is because all computers start counting from 0, not 1.
let's take 3 binary bits-
0 0 0
which can be used to count. Those 3 bits represent 8 possible states. Now we start counting:
Binary - Decimal
000 - 0
001 - 1
010 - 2
011 - 3
100 - 4
101 - 5
110 - 6
111 - 7

computers will always start at 000, because thinking logically, there is no reason to start at 001 until you've done it once.
In loops and such you can start your sentinel value at 1, but that's just because you define it that way. Computers count from 0, always.

No comments:

Post a Comment