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.
  • P PIEBALDconsult

    EDT

    C Offline
    C Offline
    cabowaboaddict
    wrote on last edited by
    #45

    PIEBALDconsult wrote:

    EDT

    :thumbsup::thumbsup::cool:

    B 1 Reply Last reply
    0
    • E englebart

      In C you could do this:

      int arrayMemory[2020-1960+1];
      int *arrayAccess = arrayMemory - 1960; // int* arithmetic

      int i;
      for (i=1960; i<=2020; ++i) {
      arrayAccess[i] = ...;
      }

      C is high level assembler. If indexing something by 1 makes sense for a piece of code you are writing, then just allocate one extra slot of memory and ignore index 0. I have done that with date based logic before. P.S. The fact that January is considered month 0 in Java still irks me. Whatever happened to encapsulation?

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

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

      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!

        E Offline
        E Offline
        englebart
        wrote on last edited by
        #47

        Zero based and One based work great for talking about arrays. To me the real issue is in talking about the indices. How do you talk about the first element/index? 1 based array - first 0 based array - ? How do you talk about the second element/index? 1 based array - second 0 based array - ? When I used to teach C programming, I would just append "-eth" to the ordinal number of the index. How do you talk about the first element/index? 1 based array - first 0 based array - zero-eth How do you talk about the second element/index? 1 based array - second 0 based array - one-eth The students (usually with a pre-dotNet Visual Basic background) picked up on this device quickly.

        1 Reply Last reply
        0
        • K kalberts

          Sentenryu wrote:

          If you hardcode that, you're doing it wrong.

          We all (I assume) have laughed at that old Xerox Fortran manual (yes, Xerox did make computers, long time ago), describing how the PARAMETER statement (roughly equivalent to a C #define) could be used to give a symbolic name to a numeric constant, so that you can write PI instead of 3.14159.... This will reduce typing, improve readability, and ease program maintenance, according to the manual, in case the value of PI changes. Making software general is a good principle, but it can go too far. 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, you could ride your principles so that this program can be adapted to changing conditions, such as starting in 1952 and lasting until 1966. Sure, from an academic point of view, you gain a lot of flexibility; you then have a program system that can handle a lot of different WW2s, not only the real one, but any number of counterfactual ones. Which is valuable from an (information technology) academic point of view. I guess the historican who uses this program to manage information from the factual WW2 could care less about the counterfactual ones. He would rather relate to "1939" than to "FirstYearOfWar" - is it the first year the US was involved? Or the first year of buildup of the military forces? Or the first full year of war, not counting years which were peaceful for the first months? Super-generality may be destructive both to readability and understanding. And it might complicate code, both for the programmer and compiler, as the generality opens for a large number of exceptional (and non-exceptional) cases possible in the generalized model, but which will never occur in the factual domain that you are modelling.

          S Offline
          S Offline
          Sentenryu
          wrote on last edited by
          #48

          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 1 Reply Last reply
          0
          • N Nagy Vilmos

            Meh! Real men code with edlin.

            speramus in juniperus

            G Offline
            G Offline
            gwojan
            wrote on last edited by
            #49

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

            R 1 Reply Last reply
            0
            • R Ravi Bhavnani

              Emacs. /ravi

              My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

              E Offline
              E Offline
              etkid84
              wrote on last edited by
              #50

              rules!

              David

              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!

                E Offline
                E Offline
                etkid84
                wrote on last edited by
                #51

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

                David

                M 1 Reply Last reply
                0
                • 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
                                          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