What to call array indexing starting at 0 vs. 1 ?
-
Emacs. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
Is there a technical term to say if an array starts at index 0 (e.g. C/C++) or 1 (e.g. pascal) ? Is it simply "0-based array" and "1 based array" ? Thanks.
I'd rather be phishing!
-
Member 7989122 wrote:
Say, if you create a highly specialized program for processing some data related to WW2, and your array of events is indexed by years 1939 to 1945,
And what would be your events? sub arrays? I said alread, the structure for this kind of data is a hastable. You don't have an event every day, so why allocate 2190+ spaces for events? Most of the time people want to index arrays by any value that doesn't start at zero or one, they're trying to index sparse data. That's a bad idea. We have high level languages, but the computer is still a computer, if we forget how he deals with our instructions, we start to write code that wastes resources and is slow. Also, how would your program deal with a classified file that got public revealing really important events that occured between years 1939 to 1945 but wasn't public? because that never happened before, you know...
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241 "'Sophisticated platform' typically means 'I have no idea how it works.'"
Hey, I didn't intend this to describe some real-world archive where the information to be handled is defined in a requirements specification... It could be anything. So the array elements could be anything. My only assumption was that the year is the top level discriminator; nothing more. I can imagine a lot of information that is grouped by year! The reason for using WW2 as an example was to illustrate a case where the start and end years are definite. Appearently, "1960 to 2020" made people want to generalize for the sake of generalizing, so I had to pull something off the top of my head where such generalization would be completely misplaced. A continuos range of index values isn't "sparse" by not starting from 0 (or 1)! The range 1960 to 2020 is as dense as 0 to 60; it gives no reason to employ hashing. Hashing is generally a bad idea if you need to traverse your data in sequential order in the most efficient way. Maybe I would be able to relate to your question about classified files etc. if I actually had been working on such a project, but I haven't. My example was only for illustrating the concept of arbitrary index limits. Considering classified information and access control is a completely different question.
-
PIEBALDconsult wrote:
EDT
:thumbsup::thumbsup::cool:
-
EDT
Heh. But only after they upgrade from Teco and SOS. :-D /ravi PS: Frightening thought: I still remember my EDT shortcuts. :omg:
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
EDT
TECO$$
-
Is there a technical term to say if an array starts at index 0 (e.g. C/C++) or 1 (e.g. pascal) ? Is it simply "0-based array" and "1 based array" ? Thanks.
I'd rather be phishing!
I try to use the word 'index' when referring to something that starts at zero, and 'position' when referring to something that starts at one.
-
TECO$$
In high school(1983) on a PDP-11 the nerds go-getters used TECO. :cool: Everyone else was using EDT in line mode. :wtf: In college on a VAX we used EDT in screen mode, I did when OpenVMS was paying the bills; and I still do on my hobbyist OpenVMS systems.
-
Meh! Real men code with edlin.
speramus in juniperus
-
I believe the correct terms are right and wrong.
speramus in juniperus
Well, you can extend 'wrong' a little. I believe 1-based indexing is also referred to as Visual Basic.
-
In high school(1983) on a PDP-11 the nerds go-getters used TECO. :cool: Everyone else was using EDT in line mode. :wtf: In college on a VAX we used EDT in screen mode, I did when OpenVMS was paying the bills; and I still do on my hobbyist OpenVMS systems.
When I worked in DEC's consulting division, I actually wrote some code for a customer in LSE to allow them to view report files in a manner similar to how they had previously done it on the IBM system the VAX was replacing.
-
I remember toggling in the bootstrap loader on the front panel of many a PDP-11. One time I must have toggled in the bootstrap loader on a particular PDP-11 at least a half dozen times with it failing to boot each time before I realized the system disk was still sitting on the table next to me.
-
In Pascal, January would neither be 0 or 1, it would be January. That's what we got high level languages for.
Think globally; act locally.
-
So you have never been programming Pascal. As others have pointed out, in Pascal you set both the lower and upper index limits. (Furthermore, indexes may be any scalar type, such as an enumeration value. Enums are NOT names of integers in Pascal, but distinct value domains.) You probably confused Pascal with Fortran, where array indexes start at 1. I never understood this fascination for low-level programming in high-level languages! Say, if my table contains data for the years 1960 to 2020, why shouldn't I address (index) the elements with values from 1960 to 2020? Why would I want to define an index base as a separate constant and for every indexing operation subtract this base from the "real" selector value to get to the right index? Calculating addresses is the job of the compiler (and linker), isn't it? That's why we use a high level language compiler! Having to subtract some base index value is like abandoning struct (/record) mechanisms and decalre the variable as a byte array, field names being offsets into the array. That gives you full control over packing and that kind of things, doesn't it? Great! Well, we do that in assembler, and it works. We can even do it in machine independent assembler, aka "C". That's what we called C when it appeared, "machine independent assembler". I had my first university level programming education in Pascal, and we saw C as a great step backwards for modelling problem domain concepts. I still do. Some languages, such as C#, allows you to define the semantics of the [] acessor for your own data types, so that the user of your class may index by values from 1960 to 2020. But you have to do a lot of programming to achieve this, and although I haven't checked, I am quite sure that the compiler isn't smart enough to reduce your accessor function to a simple subtraction of a base index value, the way the Pascal compiler could (not requiring any sort of programming from you). Technology isn't always moving in the forwards direction!
Fortran can define both lower and upper bounds for arrays. Useful in matrix or tensor math.
-
in Ada you can have -index values, for example an array with indexes in the range (-5..5) :cool:
David
Fortran as well.
-
Fortran can define both lower and upper bounds for arrays. Useful in matrix or tensor math.
I guess that nowadays it can. Who was it (it could have been Dijkstra) who during the Fortan-77 design discussions, after reviewing the proposed extensions (some of them rather extensive!) commented: "I have no idea what languages are going to look like in year 2000, but they will be called FORTRAN" ? I came across Fortran 2003 a while ago. I think he hit the nail on the head. (Most of my Fortran programming was done in Fortran IV. I don't miss it, except as a source of anecdotes about programming and languages.)
-
I try to use the word 'index' when referring to something that starts at zero, and 'position' when referring to something that starts at one.
-
Fortran as well.
Do you know when Fortran got the option to specify the lower index limit? In "classical" Fortran, arrays indexes always started at 1. I am quite sure that didn't change in Fortran-77; I never read the Fortran 90 standard (and Wikipedia doesn't give enough detail). Nostalgia: The MIX3 instruction on the 16-bit NORD minicomputers, which subtracted 1 from the accumulator and multiplied by 3. It was made especially made for FORTRAN. NORD used a 48 bit floating point format (3 16 bit words). So MIX3 converted from a FORTRAN logical index to the word offset (the machine was word, not byte, adressable) of the floating point array element. The "multiply by 3" part was done by shifting one left and adding, so it was a lot faster than using the general multiply instruction.
-
What's an index card? :) I jokingly tell people I got into writing software because I'm left handed and can't write for beans.
-
I guess that nowadays it can. Who was it (it could have been Dijkstra) who during the Fortan-77 design discussions, after reviewing the proposed extensions (some of them rather extensive!) commented: "I have no idea what languages are going to look like in year 2000, but they will be called FORTRAN" ? I came across Fortran 2003 a while ago. I think he hit the nail on the head. (Most of my Fortran programming was done in Fortran IV. I don't miss it, except as a source of anecdotes about programming and languages.)
Fortran 2003 is a mess. It has a few good ideas, but in trying to be everything for everyone, I think it stopped being what it was. The ability to specify array bounds was present in the WATFOR 77/87 compiler, but I don't know how far back it goes before that. I didn't use it when I was writing code for NASA with f77, so I don't know if that suppored it or not. And since then, I've tried to avoid it. I can write Fortran in any language, and I choose not to.