English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I had a couple questions that were on my mind for a while that I would like an answer to.

1)In for loops, why is the "standard" sentinals i and j? I had heard this had something to do with an older programming language, but why were i and j chosen

2)Now this one has been bothering me for the longest time and no one I have ever met has been able to give me an answer on it, if I declare an array (doesnt matter how many dimentions) why does the first element ALWAYS START AT ZERO instead of one??

2007-02-06 10:48:44 · 4 answers · asked by D 4 in Computers & Internet Programming & Design

4 answers

1) It is an artifact from FORTRAN.
Variables that started with i through n (or o, I forget which) defaulted to integer type. All others defaulted to floating point (real).
2) Some languages start at one, some start at zero, some allow either, some allow you to specify any origin. (I think PL/1 does this).
The advantage of starting at zero is that when an array index is converted to a pointer the machine does not have to do an extra decrement to the label's value to normalize the required address.

2007-02-06 11:30:39 · answer #1 · answered by J C 5 · 0 0

1) i is commonly used for iterating through loops. It stands for "iterator" or "index" or whatever. After about 1990 hungarian notation began to become popular and it is useful as standing for "integer" After that, j was the next letter in the alphabet.

2) Some languages actually used 1 as the first element in an array. C is designed for programmers who understand the internal workings of a computer and it is much easier to deal with 0 as the first, since that is also the offset. It is also more efficient because it saves 1 instruction for every index look up. Since any array is also a pointer, this way p[1] is the same as *(p+1) and p[0] is the same as *p.

Consider a two dimensional array. How far away is the element at [3][3]? With zero based math the answer is 3*COLUMNS + 3, but with 1 based math it is 3*COLUMNS + 3 - COLUMNS - 1. Those two extra instructions cannot be done at compile time unless you are using literals, which is rarely the case.

2007-02-06 12:23:44 · answer #2 · answered by sspade30 5 · 0 0

1) This is actually mathematics speaking here. i is used in vectors for the x-plane, and j is used for the y-plane. If you have a 3-dimensional array, it's i, j and k, just like in physics/mathematics.

2) The first element starts at 0 because when you have an array, you are actually POINTING to a block of memory. When you use Array[0], then you point at MEMORY + 0. When you use Array[1], you are doing MEMORY + 1 * Data_Type_Size. Plus, programmatically, it makes sense to use 0 as the starting point 99.9% of the time.

2007-02-06 13:32:39 · answer #3 · answered by Anonymous · 0 0

Answer to the second question can be found here:
http://www.geekinterview.com/question_details/40505

For question one I don't know the exact answer... However, i and j are frequently used to signify counters in Statistics as well.

2007-02-06 11:11:55 · answer #4 · answered by Jonas E 2 · 0 0

fedest.com, questions and answers