Why does array index start with zero




















Rather than subtracting 1, you should use the address of the array-1 as the base address. That eliminates the runtime subtraction. When you're writing a compiler, those extra instructions matter a lot. The compiler will be used to generate thousands of programs, each of which may be used thousands of times, and that extra 1 instruction may occur in several lines inside an n squared loop. It can add up to billions of wasted cycles. However, E. Dijkstra later wrote a pertinent note, why numbering should start at zero in To denote the subsequence of natural numbers 1, 2, Dijkstra 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. Longest subsequence possible that starts and ends with 1 and filled with 0 in the middle. Rearrange array such that even index elements are smaller and odd index elements are greater. Length of longest subarray for each index in Array where element at that index is largest. Queries for number of distinct elements from a given index till last index in an array. Sort Array such that smallest is at 0th index and next smallest it at last index and so on.

Farthest index that can be reached from the Kth index of given array by given operations. Queries to calculate sum of array elements present at every Yth index starting from the index X. Minimum count of indices to be skipped for every index of Array to keep sum till that index at most T. Construct array with sum of product of same indexed elements in the given array equal to zero.

Check if every index i has an index j such that sum of elements in both directions are equal. Number of permutations such that sum of elements at odd index and even index are equal.

Count of contiguous subarrays possible for every index by including the element at that index. Check if it is possible to reach to the index with value K when start index is given. Don't you think what we have today is a far better solution? Years later my 2 cnt worth. This crops up in math and programming quite often. Maybe it is just me or the field in which I work. Even today you can still write 2[a] instead of a[2]. This would have required 2 additions on CPUs of the time instead of 0, meaning half the speed.

Clearly not desirable. Just because you want 8 states, it does not mean you must have the number 8 in them. The light switches in my house are happy to represent "light on", "light off" states, without ever wondering why they don't represent the number 2.

Show 4 more comments. Because 0 is how far from the pointer to the head of the array to the array's first element. Doug T. Because 0-based index allows Branko Dimitrijevic Branko Dimitrijevic You bring up an interesting point. It can hurt performance.

But will the performance hit be significant to justify use of 0 as starting index? I doubt it. FirstNameLastName 1-based indexes offer no advantage over 0-based indexes yet they perform slightly worse.

That justifies 0-based indexes no matter how "small" the gain is. Yes, i understand that small things can add up and sometimes become a big thing. But, if 2 billion people donate it, then we can do a lot of good for humanity.

I am looking for a similar example in coding which could cause poor performance. Rather than subtracting 1, you should use the address of the array-1 as the base address. That what we did in a compiler I once worked on. That eliminates the runtime subtraction. When you're writing a compiler, those extra instructions matter a lot. The compiler will be used to generate thousands of programs, each of which may be used thousands of times, and that extra 1 instruction may occur in several lines inside an n squared loop.

It can add up to billions of wasted cycles. No it wont hurt the performance once it is compiled, it will only add a small build time because in the end it will be translated to machine code. It will only hurt the compiler designers. Show 1 more comment. Because it made the compiler and linker simpler easier to write. Reference : " Referencing memory by an address and an offset is represented directly in hardware on virtually all computer architectures, so this design detail in C makes compilation easier" and " While it doesn't directly answer the question, 0-based indexing is not natural for people or mathematicians - the only reason it's done is because the implementation is logically consistent simple.

Consider the clock, the first minute is written as , not isn't it? C predate Djikistras paper and was one of the earliest "start at 0" languages.

I thought the question was why 0 based was used, not which is better. I won't downvote but as progrmr commented above the base can be taken care of by adjusting the arrays address so regardless of base execution time is same and this is trivial to implement in the compiler or interpreter so it does not really make for simpler implementation.

Amit Prakash Amit Prakash 61 1 1 silver badge 1 1 bronze badge. Waldir Leoncio 9, 16 16 gold badges 70 70 silver badges 98 98 bronze badges. Your answer seems just a matter of opinion. In arrays, this phenomenon happens whenever you want to consider a subrange of an array as an array in its own right. Calling a collection of 3 things "3 things" and numbering them 1,2,3 is not a deficiency. Numbering them with an offset from the first one is not natural even in mathematics.

The only time you index from zero in math is when you want to include something like the zero-th power constant term in a polynomial. Re: "Numbering arrays starting with 1 rather than 0 is for people with a severe deficiency of mathematical thinking.

No, I would say the seventh one is at index 6, or 6 positions away from the first one. I Have presented answer to this question in the diagram below which i have written in a piece of paper which is self explanatory Main Steps: Creating Reference Instantiation of Array Allocation of Data to array Also note when array is just instantiated Devrath Devrath Thalaivar Thalaivar Rob Rob Gianluca Ghettini Gianluca Ghettini 9, 15 15 gold badges 73 73 silver badges bronze badges.

Niks Niks 1.



0コメント

  • 1000 / 1000