Thread: Array Index: A simple question?

  1. #1
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52

    Smile Array Index: A simple question?

    The array index for an array with n elements varies from 0 to n-1.
    We are forced to use this indexing.

    As we know indexing form 1 to n is quite simpler when analysing working programs and on output side.

    I would like to know your views on how the position in an array must be entered by a user and what output position should be produced as output.

    Whether to follow 0 to n-1 based input/output
    OR
    follow 1 to n (pseudo masking) style?

    I know that it is the program requirement that decides style.

    But rather I would like to know the each of your style as a programmer.
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Using 0 to n is simpler because it is easier to visualise an offset. For instance int foo[x] is offset (sizeof(int) * x) bytes from foo itself. So foo[1] would be sizeof(int) bytes away from foo, and foo[0] would be 0 bytes away from foo.

  3. #3
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    At that point you are correct. But that is not the case at the input/output. For example in a searching algorithm which return the position of searched element, the output is not excat(except in the eyes of a programmer).
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  4. #4
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    I've always preferred 0 based indexing. It may be my mathematics background, where 0 is the origin of a coordinate space for any particular axis.

    Users, on the other hand, may be more comfortable with the first element that they deal with being numbered 1 rather than 0, as to them, 0 means nothing (as in an absence of anything, not as in being meaningless).

    You, as the program designer, have to determine who your audience is and what system they're likely to be more comfortable with. When writing program documentation (and user documentation), it's best to specify the decision made, and (at least in the code comments) explain why you made the decision that you made.
    Insert obnoxious but pithy remark here

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    there is nothing wrong with starting the index value from 1 or 0. the only resaon if you start from 0 is that the compiler internally starts the indexing from the 0 and more over you not wasting memory. let me give an example lets take aand set of numbers and store that in a array
    10 20 30 40 50
    letd insert these values inside and arrat starting the index value from 0
    Code:
    int array[5];
    
    [10] [20] [30] [40] [50]
    0      1      2     3     4     -------->  while(i<5)
    starting the index value from 1
    Code:
    [ ] [10] [20] [30] [40] [50]
    0  1      2       3    4      5 ----------> while(i<=5);
    can u now find out the differece between the two indexing style, in the former one u starting at 0 and u goign up to 4, but with the later you start with the 1 and go up to 5

    with the latter u are just wasting one element that index 0. but still be programmer use the later style as the real work speaks from 1 not from 0

    ssharish2005

  6. #6
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    This is not the point. I am concerned about accepting position inputs and displaying position outputs. I always use the correct method of indexing 0 to n-1. But when it comes to input/output what about giving a pseudo index (i.e position+1)?
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    i prefer starting from 0

    ssharish2005

  8. #8
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by logicwonder
    This is not the point. I am concerned about accepting position inputs and displaying position outputs. I always use the correct method of indexing 0 to n-1. But when it comes to input/output what about giving a pseudo index (i.e position+1)?

    As long as you are consistent, and as long as you keep the "guts" hidden, then in my opinion it shouldn't matter.

    As I understand it, you're basically talking about representing the position to the user as postion + 1. As long as you ALWAYS do this, and don't get confused (believe me, it can happen), then it's my opinion that it is fine.

    Nothing wrong with hiding implementation details from your interface. In fact, in most cases, it is highly desirable.

  9. #9
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    Thank you harish for your view.

  10. #10
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    Thank you fgw_three for your view.

    Anymore suggestions?
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Hello ,

    I would prefer 0 to n-1 based indexing.

    The user will be most concerned about the position and would not be concentrating on index that represents the position.
    So while getting the input and displaying the same we can apply pseudo masking (implementation of program).

    so here it lies in the hands of programmer to apply the pseudo masking and thus it become user friendly and more flexible.

  12. #12
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    if you're writing a program that lets users create array's and then allows the user to enter content into the array, I'd certainly make it look like 1-n. It'd be more confusing for him if he had to enter a given value to the 0th space in the array he's working on. for a user it makes much more sense to start in the 1st space and finish on the nth...

  13. #13
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    Thank You Mako.
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  14. #14
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Hello,

    I don't understand ??

    even if u making users to create arrays then for users it should reflect from 1 to n-1 and the actual implementation will be in the program implementation...So what will happen if do pseudo masking for end users and 0 to n in coding..???

  15. #15
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Quote Originally Posted by geajith
    Hello,

    I don't understand ??

    even if u making users to create arrays then for users it should reflect from 1 to n-1 and the actual implementation will be in the program implementation...So what will happen if do pseudo masking for end users and 0 to n in coding..???

    wrong, for users it'll be 1 to n, not n-1

    All you're doing is hiding what actually happens from the user to make it easier for them to understand...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  2. simple i/o question
    By n3v in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2006, 08:13 AM
  3. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  4. simple array question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2002, 10:43 PM
  5. simple array question
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 04:46 PM