Announcement

Collapse
No announcement yet.

Arrays

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Arrays

    I'm a little confused about what is going on with arrays.

    If I:

    DIM Array(20, 20)

    "Array" should be from 0 to 19, 0 to 19. Is that correct?

    However;
    Code:
    DIM Array(20, 20)
    
    FOR I = 0 TO 20
        FOR J = 0 TO 20
           Array(I, J) = 2
        NEXT J
    NEXT I
    I am actually doing each loop 21 times and no runtime error is generated. Why?
    Walt Decker

  • #2
    Originally posted by Walt Decker View Post
    I'm a little confused about what is going on with arrays.

    If I:

    DIM Array(20, 20)

    "Array" should be from 0 to 19, 0 to 19. Is that correct?

    However;
    Code:
    DIM Array(20, 20)
    
    FOR I = 0 TO 20
        FOR J = 0 TO 20
           Array(I, J) = 2
        NEXT J
    NEXT I
    I am actually doing each loop 21 times and no runtime error is generated. Why?
    No,
    Code:
    DIM Array(20,20)
    is the same as doing
    Code:
    DIM Array(0 to 20, 0 to 20)
    Sincerely,

    Steve Rossell
    PowerBASIC Staff

    Comment


    • #3
      Thanks, Steve. That's different from what I learned years ago with other basics.
      Walt Decker

      Comment


      • #4
        It depends on zer0 based vs 1 based
        for details....as the rules depend on the application

        Generically though
        DIM Array(20, 20)
        would result in an array with 21 elements (1 to 20 based), but each element starts from a number one less than the count
        Engineer's Motto: If it aint broke take it apart and fix it

        "If at 1st you don't succeed... call it version 1.0"

        "Half of Programming is coding"....."The other 90% is DEBUGGING"

        "Document my code????" .... "WHYYY??? do you think they call it CODE? "

        Comment


        • #5
          It's not a bad discipline to use the full syntax when dimensioning arrays, for example, DIM SDFGHJKL(0 to 99).

          Regarding multidimensional arrays, for some reason I find it more natural to use a single dimension and multiply out the index. No idea what impact this has on performance, but it keeps my code looking simpler.

          Comment


          • #6
            Originally posted by Chris Holbrook View Post
            ...No idea what impact this has on performance, but it keeps my code looking simpler.
            Every test I've done shows it's faster too, in certain circumstances maybe even approaching 2x.

            Originally posted by Cliff Nichols
            DIM Array(20, 20)...would result in an array with 21 elements (1 to 20 based), but each element starts from a number one less than the count
            1 to 20 there gives only 20 elements, did you mean 1 to 21, based on "one less than the count" logic?

            Comment


            • #7
              Yes I did John, I just mis-spoke what I meant
              Engineer's Motto: If it aint broke take it apart and fix it

              "If at 1st you don't succeed... call it version 1.0"

              "Half of Programming is coding"....."The other 90% is DEBUGGING"

              "Document my code????" .... "WHYYY??? do you think they call it CODE? "

              Comment


              • #8
                >That's different from what I learned years ago with other basics

                Which BASICs were those? I can't remember any BASIC which did NOT use 0-based subscripts by default.

                MCM
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment

                Working...
                X