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. From where should the index start?

From where should the index start?

Scheduled Pinned Locked Moved The Lounge
questionphpdatabasedesignhelp
114 Posts 31 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.
  • D Dr Walt Fair PE

    No, I wouldn't be so stupid as to suggest a blanket system wide change. Note that Basic allows that with its OPTION BASE -- another reason not to use BASIC! I'd let it work on an array by array basis, like Algol. For example, when you declare an array, you specify its base array, with a default of 0. Sort of like A[1:10] or B[-2:15] or C[0:19] which would be equivalent to C[20] or some such syntax. I'd have to give it more thought before actually proposing a specification. Requiring that an array start with a specific index in all cases is just an unnecessary limitation. Why can't I make an array that goes from -22 to +46, if it makes my code more intelligible? Obviously it's possible, since we can map indices in most any language right now or use an associative array in the languages that support that structure. The index limitation, in my opinion, is a throw back to earlier times when compilers were more limited. It sounds like your applications and mine are much different. I find a lot of cases where it's convenient to use arrays with indices that don't start at either 1 or 0 and end up having to map them back to 0 or 1 based indices or use a different data structure. It's no big deal, but the question was asked what I would do, and that's what I would do. Pretty much just like Algol. I just don't care for arbitrary, compiler imposed limitations when the compiler can automatically do the mapping and the programmer/developer could use whatever base is most convenient, clear, and logical for the algorithm description.

    CQ de W5ALT

    Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #69

    I understand your logic but it seems that it makes it less legible. If you have numerous algorithms all using different index base, the single algorothm may be easier to understand but the integration of them would be difficult (except for the creator). The reason being is 0 is like your starting point. If I I have a need for -22 to +46 I will use a -22 START constant for its offset and an END. Not doing this causes the reader to still do the math because that is also not human language. So you gained nothing it seems, other than your algorithm was easier to write (at first. I say at first because it may need to be adjusted and it is now hard to maintain). If this algorithm passes data to another algorithm that is better suited for 17232 thru 22222 I have to figure out what the heck that means and where they line up. If 0 is my universal starting point they all line up at 0. Back to the 1 index, yes more human readable but many algorithms work happily at 0 based index. Since they will still remain why not keep them as the true base and use offsets for where you need the -22 and +46?

    Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

    D 1 Reply Last reply
    0
    • B BobJanova

      This makes no sense. Counting and indexing are completely different things – even the 0 based computing world accepts that a collection of things A, B and C has 3 items, even if it calls them '0', '1' and '2'. The fact is if a person is counting objects they do use 0, it is just omitted. That doesn't mean anything either. A count of 0 is when there are no things to count. But if there are 6 apples, it doesn't matter what indexing system you use, or how many you count at once, there are always 6. What 'indexing' is about it labelling. Ask anyone to label those apples with numbers and everybody (unless they're being deliberately silly) will call them '1', '2', '3', '4', '5' and '6'. Look at things which are labelled in the real world: bases on a rounders/baseball field, quarters or halves in a sports match, aisle numbers in the supermarket. Even in the computing world this is often true; look at the MRU on Excel or Word, it numbers your recently used files from the first (1) up. Well starting the index at 1 for computers is inefficient As I'm discussing with Nagy above, technically true in a small number of cases (many of which are due to language deficiencies regarding array operations in C family languages) but so small as to be irrelevant as a consideration in the modern world. The fundamental question is really: do you want a language for the computer, or one for the people who write it? I would prefer to write one for people and let the compiler or interpreter deal with the translation.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #70

      Also I wasn using 'Counting" because that was what was posted in whome I responded to. They used Counting as the argument of why it should be 1,2,3... I pointed out we do count 0, implicityly. Computers count it explicitly upon definition.

      Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

      1 Reply Last reply
      0
      • L Lost User

        From Wiki[^] Advantages [Using 0 based Index] One advantage of this convention is in the use of modular arithmetic as implemented in modern computers. Every integer is congruent modulo N to one of the numbers 0, 1, 2, ..., N − 1, where N ≥ 1. Because of this, many arithmetic concepts (such as hash tables) are more elegantly expressed in code when the array starts at zero. A second advantage of zero-based array indexes is that this can improve efficiency under certain circumstances. To illustrate, suppose a is the memory address of the first element of an array, and i is the index of the desired element. In this fairly typical scenario, it is quite common to want the address of the desired element. If the index numbers count from 1, the desired address is computed by this expression: where s is the size of each element. In contrast, if the index numbers count from 0, the expression becomes this: This simpler expression can be more efficient to compute in certain situations. Note, however, that a language wishing to index arrays from 1 could simply adopt the convention that every "array address" is represented by a′ = a – s; that is, rather than using the address of the first array element, such a language would use the address of an imaginary element located immediately before the first actual element. The indexing expression for a 1-based index would be the following: Hence, the efficiency benefit of zero-based indexing is not inherent, but is an artifact of the decision to represent an array by the address of its first element. A third advantage is that ranges are more elegantly expressed as the half-open interval, [0,n), as opposed to the closed interval, [1,n], because empty ranges often occur as input to algorithms (which would be tricky to express with the closed interval without resorting to obtuse conventions like [1,0]). On the other hand, closed intervals occur in mathematics because it is often necessary to calculate the terminating condition (which would be impossible in some cases because the half-open interval isn't always a closed set) which would have a subtraction by 1 everywhere. This situation can lead to some confusion in terminology. In a zero-based indexing scheme, the first element is "element number zero"; likewise, the twelfth element is "element number eleven". Therefore, an analogy f

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #71

        I fully agree with your second advantage, however the third one puzzles me a bit, as you can turn the closed interval [1,n] into a half-open one if you so wish, here is one way of doing that: (0, n], which is very similar to [0, n). :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        L 1 Reply Last reply
        0
        • L Luc Pattyn

          I fully agree with your second advantage, however the third one puzzles me a bit, as you can turn the closed interval [1,n] into a half-open one if you so wish, here is one way of doing that: (0, n], which is very similar to [0, n). :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #72

          Similar but not the same. Convention is to use [0, n) [Edit.. I put the wrong convention in. HA! The irony!] Yes it can be done. But I think the point of the thirda advantage is purely based on conventions. Which is IMO a critical argument as the argument is by definition stating 1 is more 'conventional' to start your index at. Math proofs don't. Been that way well before computers.

          Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

          1 Reply Last reply
          0
          • L Lost User

            I understand your logic but it seems that it makes it less legible. If you have numerous algorithms all using different index base, the single algorothm may be easier to understand but the integration of them would be difficult (except for the creator). The reason being is 0 is like your starting point. If I I have a need for -22 to +46 I will use a -22 START constant for its offset and an END. Not doing this causes the reader to still do the math because that is also not human language. So you gained nothing it seems, other than your algorithm was easier to write (at first. I say at first because it may need to be adjusted and it is now hard to maintain). If this algorithm passes data to another algorithm that is better suited for 17232 thru 22222 I have to figure out what the heck that means and where they line up. If 0 is my universal starting point they all line up at 0. Back to the 1 index, yes more human readable but many algorithms work happily at 0 based index. Since they will still remain why not keep them as the true base and use offsets for where you need the -22 and +46?

            Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

            D Offline
            D Offline
            Dr Walt Fair PE
            wrote on last edited by
            #73

            Collin Jasnoch wrote:

            I understand your logic but it seems that it makes it less legible.

            No, it gives the programmer the option of how to write it in the most legible way.

            Collin Jasnoch wrote:

            If this algorithm passes data to another algorithm that is better suited for 17232 thru 22222 I have to figure out what the heck that means and where they line up. If 0 is my universal starting point they all line up at 0.

            I certainly wouldn't write the compiler to force that!

            Collin Jasnoch wrote:

            Back to the 1 index, yes more human readable but many algorithms work happily at 0 based index. Since they will still remain why not keep them as the true base and use offsets for where you need the -22 and +46?

            I would submit that most all algorithms that use arrays can be written to use an arbitrary base and offset. However, if the compiler can figure out how to do the base and offset, then why should the programmer be forced to do it manually if it is more natural to use a particular base and offset? To me the question boils down to: Do you want to do higher or lower level coding? In my opinion, the compiler should do what ever it can, or I'd go back to coding in C or ASM or something.

            CQ de W5ALT

            Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

            L 1 Reply Last reply
            0
            • D Dr Walt Fair PE

              Collin Jasnoch wrote:

              I understand your logic but it seems that it makes it less legible.

              No, it gives the programmer the option of how to write it in the most legible way.

              Collin Jasnoch wrote:

              If this algorithm passes data to another algorithm that is better suited for 17232 thru 22222 I have to figure out what the heck that means and where they line up. If 0 is my universal starting point they all line up at 0.

              I certainly wouldn't write the compiler to force that!

              Collin Jasnoch wrote:

              Back to the 1 index, yes more human readable but many algorithms work happily at 0 based index. Since they will still remain why not keep them as the true base and use offsets for where you need the -22 and +46?

              I would submit that most all algorithms that use arrays can be written to use an arbitrary base and offset. However, if the compiler can figure out how to do the base and offset, then why should the programmer be forced to do it manually if it is more natural to use a particular base and offset? To me the question boils down to: Do you want to do higher or lower level coding? In my opinion, the compiler should do what ever it can, or I'd go back to coding in C or ASM or something.

              CQ de W5ALT

              Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #74

              I think you missed my point. And I did not understand your second remark... Maybe you have not worked on large teams or done any integration with other systems. Say I have 2 algorithms dealing with 2 different (yet coupled) objects. In algorithm 1, the logic makes senc to go from -22 thru +46. And in Algorithm 2 the logic makes sence to go grom 17232 thru 22222. If you define the algorithm as such the integration of these collections and objects is hidden from the developer having to integrate. The develop has no idea that 17232 thru 18345 refers to -22 and that 18345 thru 19246 refers to -21 (obviously I am just throwing out numbers... Thats the point). If however they are sharing the same base talk (0 base index), the developer will clearly see the reasoning of the the one whome wrote the algorithm. Because you would have something like this

              const int ALGO1_OFFSET = -22;
              const int ALGO1_END_OFFSET = 46;

              const int ALGO1_TO_ALGO2_MOD = 31;//Making it up again

              These values would have real world meaning. They bring the algorithm back to the same starting point. Yes, I understand you can do that manually everytime you look at the algorithms. But the more you have and the more they pass data (which they often do), the worse it gets. For a simple application dealing with one or 2 heavy computation it would be fine. But most applications deal with a lot more than one or 2 algorithms.

              Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

              D 1 Reply Last reply
              0
              • G gumi_r msn com

                That I honestly would not like at all. I think a bad standard is better than no standard at all. It would be a nightmare if everytime you had to get your hands dirty with some codebase you had to factor in the index base whoever wrote the code decided to use on that given day/project/etc.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #75

                You and I are on the same page here.

                Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                1 Reply Last reply
                0
                • B BobJanova

                  Indices start from 1. (When did you ever hear someone talking about the 'zeroth' anything, outside of certain computing circles?) Offsets start from 0. The confusion comes because the C family really uses offsets (you can see this in the original C, with the identity a[i] = a + i), but they've always been called 'indices', and that has fed forward into '0 based indexing' (which means subscripts are offsets, really) in its modern siblings. Once you know whether you want to base your language on indices or offsets, the mechanics become obvious. Index: start from 1. ("What's the first/second/etc character of a string?" -> "s[1/2/etc]".) Offsets start from 0. ("What's the character 0/1/etc away from the start?" -> "s[0/1/etc]".) Since I come from the real world and not a computer science course I naturally think in terms of index origin 1 – particularly as I started out programming in APL with that setting. I've written conventional languages enough that I can deal with the offset-based approach but if I was choosing I'd still start at 1.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #76

                  BobJanova wrote:

                  outside of certain computing circles?

                  Actually you need to grow the group significantly. It is math circles which is the basis for all sciences. We use non-zero base index when it comes to physical stuff that exists in the world. Because we imply the 0th element (nothing) by talking about the object. In math and sciences you can not imply this, and must therefore define it. The 0th base index dates back to before computers. It just so happens that computers made it more common knowledge.

                  Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                  1 Reply Last reply
                  0
                  • M Marc Clifton

                    nikunjbhatt84 wrote:

                    Then from where would start the index number?

                    I wouldn't allow indexing. At least, I wouldn't expose it to the programming language. Indexing is an archaic left-over from the days for for-next loops and arrays. Even for strings, it's absurd. Folks should take a page from assembly language, where you could map memory into a structure and reference specific elements of the structure with meaningful symbols. This includes handling strings dynamically as well, if people were just to put there minds to it. Marc

                    My Blog

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #77

                    You have a very good point. I don't use for loops at all, unless the algorithm requires it. If I am iterating through a collection there is rarely a need to know if I am at 0 or 1. I just care about the order, and that is why we have sorting algorithms out of the box. It is all hidden and makes it very easy. When there is a need your loop takes that into account.

                    Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                    1 Reply Last reply
                    0
                    • N Nikunj_Bhatt

                      In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #78

                      if you try to page within an array, list or any other indexed structure, everything that is not zero based is a pain.

                      1 Reply Last reply
                      0
                      • L Lost User

                        I think you missed my point. And I did not understand your second remark... Maybe you have not worked on large teams or done any integration with other systems. Say I have 2 algorithms dealing with 2 different (yet coupled) objects. In algorithm 1, the logic makes senc to go from -22 thru +46. And in Algorithm 2 the logic makes sence to go grom 17232 thru 22222. If you define the algorithm as such the integration of these collections and objects is hidden from the developer having to integrate. The develop has no idea that 17232 thru 18345 refers to -22 and that 18345 thru 19246 refers to -21 (obviously I am just throwing out numbers... Thats the point). If however they are sharing the same base talk (0 base index), the developer will clearly see the reasoning of the the one whome wrote the algorithm. Because you would have something like this

                        const int ALGO1_OFFSET = -22;
                        const int ALGO1_END_OFFSET = 46;

                        const int ALGO1_TO_ALGO2_MOD = 31;//Making it up again

                        These values would have real world meaning. They bring the algorithm back to the same starting point. Yes, I understand you can do that manually everytime you look at the algorithms. But the more you have and the more they pass data (which they often do), the worse it gets. For a simple application dealing with one or 2 heavy computation it would be fine. But most applications deal with a lot more than one or 2 algorithms.

                        Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                        D Offline
                        D Offline
                        Dr Walt Fair PE
                        wrote on last edited by
                        #79

                        So why do you think a compiler couldn't figure that out for you? If we're going to write a compiler and define how we set up indices, I don't see any problem. If your algorithm depends on some constants, that could be changed outside the interface, it doesn't sound like a good algorithm design to me. You're right, I don't work on large teams. But I was always told that encapsulation is a good thing in a team environment. In that case, the algorithms are basically a black box to the rest of the team and it shouldn't matter how they are written internally. For example, I have some simultaneous PDE's to solve with sufficient boundary conditions that my natural (physically meaningful) indices go from 2 to N-2. But when we solve the resulting difference equations, the algorithms always start with index 1 in FORTRAN or index 0 in C. It really doesn't matter much, since those details are hidden. The mathematicians use index = 1 as the base, so sticking with that is much more readable from a numerical methods point of view. In fact when translating the algorithms, I usually get it working with a base 1 array, realizing I'm wasting the memory for the 0 entries. Then I shift the loop indices and increment the values and test everything again, then finally replace the indices and reduce the array size by 1. Doing it that way allows me to implement complicated array manipulations without introducing errors that can't be detected. I see no reason whatsoever that the compiler couldn't do those rote manipulations.

                        CQ de W5ALT

                        Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

                        L 1 Reply Last reply
                        0
                        • D Dr Walt Fair PE

                          So why do you think a compiler couldn't figure that out for you? If we're going to write a compiler and define how we set up indices, I don't see any problem. If your algorithm depends on some constants, that could be changed outside the interface, it doesn't sound like a good algorithm design to me. You're right, I don't work on large teams. But I was always told that encapsulation is a good thing in a team environment. In that case, the algorithms are basically a black box to the rest of the team and it shouldn't matter how they are written internally. For example, I have some simultaneous PDE's to solve with sufficient boundary conditions that my natural (physically meaningful) indices go from 2 to N-2. But when we solve the resulting difference equations, the algorithms always start with index 1 in FORTRAN or index 0 in C. It really doesn't matter much, since those details are hidden. The mathematicians use index = 1 as the base, so sticking with that is much more readable from a numerical methods point of view. In fact when translating the algorithms, I usually get it working with a base 1 array, realizing I'm wasting the memory for the 0 entries. Then I shift the loop indices and increment the values and test everything again, then finally replace the indices and reduce the array size by 1. Doing it that way allows me to implement complicated array manipulations without introducing errors that can't be detected. I see no reason whatsoever that the compiler couldn't do those rote manipulations.

                          CQ de W5ALT

                          Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #80

                          I am starting to wonder if you are actually reading what I am posting. I never said the compiler can't figure it out. I am saying it shouldn't have to. If you change the base index variably to what one specific algorithm needs integration is a nightmare. Yes Black box is great. BUT you need a consistent API. Imagine if everyone defined their own integers. i.e. One system said its integers can go out to 32 bit and another said 33 and still another said 34. The complexity in integrating the systems would get ridiculous real fast. The same thing goes for your starting index. If I pass you a collection and also have to pass you the starting 'label (as some are refering to index) I have added a complexity to the API. If that alone is not enough, when I string 3 or 3 dozen algorithms together and have to do the integration testing I can't find my data. Encapsulation is important but you still have to test integration of the components. Now if everything miraculously works from the get go and you need not ever touch again (Wake up Alice your dreaming!!!), you are fine. But more likely something will not work during integration testing or when an upgrade occurs. To narrow down which component is failing you will have to follow the data. If you are using different based indecies you will spend an enormous amount of time calculating back to a single base (beit 1 or 0)

                          Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                          D 1 Reply Last reply
                          0
                          • L Lost User

                            I am starting to wonder if you are actually reading what I am posting. I never said the compiler can't figure it out. I am saying it shouldn't have to. If you change the base index variably to what one specific algorithm needs integration is a nightmare. Yes Black box is great. BUT you need a consistent API. Imagine if everyone defined their own integers. i.e. One system said its integers can go out to 32 bit and another said 33 and still another said 34. The complexity in integrating the systems would get ridiculous real fast. The same thing goes for your starting index. If I pass you a collection and also have to pass you the starting 'label (as some are refering to index) I have added a complexity to the API. If that alone is not enough, when I string 3 or 3 dozen algorithms together and have to do the integration testing I can't find my data. Encapsulation is important but you still have to test integration of the components. Now if everything miraculously works from the get go and you need not ever touch again (Wake up Alice your dreaming!!!), you are fine. But more likely something will not work during integration testing or when an upgrade occurs. To narrow down which component is failing you will have to follow the data. If you are using different based indecies you will spend an enormous amount of time calculating back to a single base (beit 1 or 0)

                            Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                            D Offline
                            D Offline
                            Dr Walt Fair PE
                            wrote on last edited by
                            #81

                            Collin Jasnoch wrote:

                            I am starting to wonder if you are actually reading what I am posting.

                            Same here.

                            Collin Jasnoch wrote:

                            I never said the compiler can't figure it out. I am saying it shouldn't have to.

                            And that's where we disagree. Peace. That's why there are a multiplicity of languages to choose from and why we don't all work on the same things.

                            CQ de W5ALT

                            Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

                            1 Reply Last reply
                            0
                            • N Nikunj_Bhatt

                              I think you are still thinking ONLY about pointers. Suppose I have a listbox and I want to loop through all of its items (elements). I can start a loop like this in VB.NET "For i=0 to ListBox1.Items.Count-1" in Zero based index. Here you can see that there is one more calculation performed (ListBox1.Items.Count-1). If the index is based on One, the loop can be transformed to "For i=1 to ListBox1.Items.Count" which is using one less calculation than Zero based (Actually, this also depends on number of items in the listbox. The loop will need to calculate "Count-1" up to the number of items in the listbox.) In common sense, if there is "nothing" it means Zero and everything starts with One. And programmers are not computers, programming languages can be designed to use starting index as Zero or One or anything most preferred. The compiled code can address anything using the zero based index but this must not be necessary for a programmer/programming language to use the same. More and more programming languages feature are added just to ease programming, otherwise programming can be done directly in Binary.

                              D Offline
                              D Offline
                              Dan Neely
                              wrote on last edited by
                              #82

                              ...and by starting at one instead of zero, each iteration of the loop will be doing *ListBox1.Items[p+(n-1)*s] (give or take the amount of C++ I've forgotten over the years) each time you wrote ListBox1.Items[i] instead of *ListBox1.Items[p+(n)*s]. If you really have a problem with the concept of zero based indexing invent a language that doesn't have pure arrays and only allows indexing collections via a foreach style construct. Don't forget to define behavior in which the body of the loop adds or removes an item from the collection. eg this isn't legal C#:

                              foreach (var item in myCollection)
                              {
                              if (item.readyToDelete)
                              myCollection.Remove(item);
                              }

                              Instead you have to write:

                              for (int i = myCollection.Count -1; i <= 0; i--)
                              {
                              if (myCollection[i].readyToDelete)
                              myCollection.Remove(myCollection[i]);
                              }

                              Don't forget that while the example I gave would be fairly trivial you also would need to implement code to handle DeleteSevenRandomItemsInCollectionAndAddThreeNewItemsAtRandomLocations(), DeleteItemIfIndexIsEvenAndAddANewItemBeforeIfItIsOdd() and all the other horrible cases that urgent programmers would write which result in psuedorandom results if the iteration order varies but which give no logical pattern in which the collection can be iterated. MS didn't allow adding/removing from the collection in a foreach loop for a reason.

                              Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt

                              1 Reply Last reply
                              0
                              • N Nagy Vilmos

                                Simply put, it makes sense to have it start at 0. The array variable will be a pointer to a memory address p and each item is an offset from there so, for item n where each element is size s the memory address is p+n*s. If you want 1 based arrays then the element would be found at p+(n-1)*s. Which is easier to compute?


                                Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                                R Offline
                                R Offline
                                Rob Grainger
                                wrote on last edited by
                                #83

                                I'm not so sure. The point of a language is to abstract away such technical details (which are getting close to C/assembly style here). There's no reason a language can't implicitly do this conversion at compile-time, making it a moot point. Designing language features so that it makes for more efficient compuation is a fallacy - languages should be designed to make it easier for programmers, then implemented to achieve the desired performance. That said, I still prefer 0-based indexing, this just seems the wrong reason to me, heavily steeped in the C tradition of having pointers and arrays be interchangeable.

                                1 Reply Last reply
                                0
                                • L Lost User

                                  gumi_r@msn.com wrote:

                                  How does a small kid learn his numbers? I find it hard to imagine some little boy counting 0, 1, 2, ...
                                  We start at 1, and do so even when we are grown ups.

                                  You eventually teach children the concept of 0. It is something you must teach them. However, we need not teach computers the concept of 0. The fact is if a person is counting objects they do use 0, it is just omitted. For example, I have 6 apples all lined up. I want to count them. Now I am a smart chap so I can just look and say there are 6 apples. Someone else may need to at the minimum count by 2's (omitting the odds). And yet another will count from 1 to 6 (omitting the 0). The fact is all counting of objects (which is what your comparison is about) start with 0, we just choose to omit for efficiency. Well starting the index at 1 for computers is inefficient because the fact is 0 exists, and the computer knows that. If you start at 1 you now have to proram at the base level speacial instructions for it to understand 0. Why would you do that? So you example of going to the grocery store. What are you going to do when the clerk hands you 1 Orange, 1 watermelon and 1 zuchini?? "Hey I didn't ask for these!" "Oh sorry, you forgot to say 0 of those items..."

                                  Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                                  R Offline
                                  R Offline
                                  Rob Grainger
                                  wrote on last edited by
                                  #84

                                  I fundamentally disagree. We have always begun counting at 1, we do not begin at zero but omit it. That's an incredibly wierd outlook. Get out more - you're spending too much time with programmers. Consider bananas - if I have three bananas I count them 1, 2, 3. Yet, if I have no bananas, I'm unlikely to begin counting them at all. I don't think we're omitting the zero - it was never there to begin with. Zero was introduced much later as a concept then counting, and is one of the most significant developments in mathematics, but its not involved in counting.

                                  L 1 Reply Last reply
                                  0
                                  • N Nikunj_Bhatt

                                    In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?

                                    S Offline
                                    S Offline
                                    Stuart Rubin
                                    wrote on last edited by
                                    #85

                                    In general, I try to make my variables "domain specific" and single purpose. So, if my function returns "milliamps" but can also return an "error code", I will usually have the function return an enumerated "error code" like

                                    typedef enum {ERR_NONE, ERR_BAD_READING, ERR_BUSY} AdcErrCode;

                                    and pass the returned "milliamps" value by reference. When I'm feeling really disciplined, I may even define the data type like "typedef uint16_t Milliamp". For your example, I would certainly return a separate error code like

                                    typedef enum {STR_SRCH_FOUND, STR_SRCH_NOT_FOUND, STR_SRCH_BAD_STR} StrSearchErr;

                                    or something and return the error code by reference. Typedefing your domain specific types add ZERO additional run time or memory overhead. Thoughtful, explicit typedefs also make your static code analyzers work a lot better. Note that these are examples from a C point-of-view. So, the point is not whether to use 0 or 1 as the first index, or what your error code should be, but to be extremely explicit in your data types (even in loosely typed languages) and variable names. An "index" in your search is not necessarily the same as a letter "count".

                                    1 Reply Last reply
                                    0
                                    • N Nikunj_Bhatt

                                      In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?

                                      M Offline
                                      M Offline
                                      Mark AJA
                                      wrote on last edited by
                                      #86

                                      Bit off subject but sometimes I want the HTML command <LI> to start at 0 and not 1, or allow numbers under 1. but <LI VALUE="0"> will not set it to 0 I ended up using a table and not <UL> <LI>'s.

                                      1 Reply Last reply
                                      0
                                      • R Rob Grainger

                                        I fundamentally disagree. We have always begun counting at 1, we do not begin at zero but omit it. That's an incredibly wierd outlook. Get out more - you're spending too much time with programmers. Consider bananas - if I have three bananas I count them 1, 2, 3. Yet, if I have no bananas, I'm unlikely to begin counting them at all. I don't think we're omitting the zero - it was never there to begin with. Zero was introduced much later as a concept then counting, and is one of the most significant developments in mathematics, but its not involved in counting.

                                        L Offline
                                        L Offline
                                        Lost User
                                        wrote on last edited by
                                        #87

                                        Rob Grainger wrote:

                                        We have always begun counting at 1, we do not begin at zero but omit it. That's an incredibly wierd outlook.

                                        Actually it is a scientific way of looking at it.

                                        Rob Grainger wrote:

                                        I don't think we're omitting the zero - it was never there to begin with. Zero was introduced much later as a concept then counting, and is one of the most significant developments in mathematics, but its not involved in counting.

                                        No it was not introduced at the same time as counting. "Hey Ugh. Get me 3 sticks for fire".... "1.. 2... 3" Counting has been around for a while, but math proofs and formulas which require enumeration did require it and still do (ohhh wait... Is that one computers are doing??? Math formulas? Hmmm I thought it was just for perrty pictures of Salma Hayek) If you are familiar with math proofs you should know the most common value that you are converging from or to is 0. If you are using a 1 base index (or counting and ommitting 0) you will make the algorithms more complicated. Here is another way to show we are indeed omitting in human language. If you write down "10", we know you mean "10". A computer however will use the data before it. If you did nothing to the data it will not be "10" but could be 26310 or 28510 and so on and so on. Now I know what you are thinking. Compiler handles that why can't it do this for us. Because wether you wrote "10" or "0010" has absolutly no effect on the algorithm. You starting your array from 1 does. If you do not grasp that you may need to study mathematics a little more.

                                        Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                                        R 1 Reply Last reply
                                        0
                                        • I Ian Shlasko

                                          Clearly, all indices should start from -2 and count in base 13.

                                          Proud to have finally moved to the A-Ark. Which one are you in?
                                          Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                                          C Offline
                                          C Offline
                                          C Grant Anderson
                                          wrote on last edited by
                                          #88

                                          Except on Tuesdays when it should start at a random number and use base 6 or 18 depending on if it is raining or not.

                                          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