Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. What to call array indexing starting at 0 vs. 1 ?

What to call array indexing starting at 0 vs. 1 ?

Scheduled Pinned Locked Moved The Lounge
c++delphidatabasevisual-studiodata-structures
70 Posts 36 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Sentenryu

    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.'"

    K Offline
    K Offline
    kalberts
    wrote on last edited by
    #52

    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.

    1 Reply Last reply
    0
    • C cabowaboaddict

      PIEBALDconsult wrote:

      EDT

      :thumbsup::thumbsup::cool:

      B Offline
      B Offline
      BrainiacV
      wrote on last edited by
      #53

      1's and 0's.

      Psychosis at 10 Film at 11 Those who do not remember the past, are doomed to repeat it. Those who do not remember the past, cannot build upon it.

      1 Reply Last reply
      0
      • P PIEBALDconsult

        EDT

        R Offline
        R Offline
        Ravi Bhavnani
        wrote on last edited by
        #54

        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

        1 Reply Last reply
        0
        • P PIEBALDconsult

          EDT

          R Offline
          R Offline
          RefugeeFromSlashDot
          wrote on last edited by
          #55

          TECO$$

          P 1 Reply Last reply
          0
          • M Maximilien

            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!

            R Offline
            R Offline
            RefugeeFromSlashDot
            wrote on last edited by
            #56

            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.

            K 1 Reply Last reply
            0
            • R RefugeeFromSlashDot

              TECO$$

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #57

              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.

              R 1 Reply Last reply
              0
              • N Nagy Vilmos

                Meh! Real men code with edlin.

                speramus in juniperus

                O Offline
                O Offline
                obermd
                wrote on last edited by
                #58

                Punch cards.

                1 Reply Last reply
                0
                • N Nagy Vilmos

                  I believe the correct terms are right and wrong.

                  speramus in juniperus

                  L Offline
                  L Offline
                  Les Ferguson
                  wrote on last edited by
                  #59

                  Well, you can extend 'wrong' a little. I believe 1-based indexing is also referred to as Visual Basic.

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    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.

                    R Offline
                    R Offline
                    RefugeeFromSlashDot
                    wrote on last edited by
                    #60

                    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.

                    1 Reply Last reply
                    0
                    • G gwojan

                      No, real mean code in binary with toggle switches...

                      R Offline
                      R Offline
                      RefugeeFromSlashDot
                      wrote on last edited by
                      #61

                      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.

                      1 Reply Last reply
                      0
                      • K kalberts

                        In Pascal, January would neither be 0 or 1, it would be January. That's what we got high level languages for.

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #62

                        Think globally; act locally.

                        1 Reply Last reply
                        0
                        • K kalberts

                          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!

                          M Offline
                          M Offline
                          Michael Waters
                          wrote on last edited by
                          #63

                          Fortran can define both lower and upper bounds for arrays. Useful in matrix or tensor math.

                          K 1 Reply Last reply
                          0
                          • E etkid84

                            in Ada you can have -index values, for example an array with indexes in the range (-5..5) :cool:

                            David

                            M Offline
                            M Offline
                            Michael Waters
                            wrote on last edited by
                            #64

                            Fortran as well.

                            K 1 Reply Last reply
                            0
                            • M Michael Waters

                              Fortran can define both lower and upper bounds for arrays. Useful in matrix or tensor math.

                              K Offline
                              K Offline
                              kalberts
                              wrote on last edited by
                              #65

                              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.)

                              M 1 Reply Last reply
                              0
                              • R RefugeeFromSlashDot

                                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.

                                K Offline
                                K Offline
                                kalberts
                                wrote on last edited by
                                #66

                                How would you number index cards?

                                R 1 Reply Last reply
                                0
                                • M Michael Waters

                                  Fortran as well.

                                  K Offline
                                  K Offline
                                  kalberts
                                  wrote on last edited by
                                  #67

                                  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.

                                  M 1 Reply Last reply
                                  0
                                  • K kalberts

                                    How would you number index cards?

                                    R Offline
                                    R Offline
                                    RefugeeFromSlashDot
                                    wrote on last edited by
                                    #68

                                    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.

                                    1 Reply Last reply
                                    0
                                    • K kalberts

                                      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.)

                                      M Offline
                                      M Offline
                                      Michael Waters
                                      wrote on last edited by
                                      #69

                                      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.

                                      1 Reply Last reply
                                      0
                                      • K kalberts

                                        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.

                                        M Offline
                                        M Offline
                                        Michael Waters
                                        wrote on last edited by
                                        #70

                                        I made use of it in the WATFOR 77/87 compiler in grad school, but I don't know how far back the capabilty goes before that. I don't know about f77, as I didn't need to use that feature in the code I was writing for it. I'm afraid the NORD minicomp is a bit before my time :) But then, integer math is always easier than floating point. I've tried to write a continued fraction integer based representation of floating point arithmetic in C++, but set it aside when I realized that taking the reciprocal was introducing floating point roundoff into the caluclations, and didn't have the patience to figure out how to fix it. I wasn't doing it for size or speed, but to try to eliminate roundoff error, so my efforts had come full circle at that point. Maybe I should have written it in Fortran instead?

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups