• Re: “Why do arrays start at 0?"

    From Charlie Roberts@21:1/5 to lkrupp@invalid.pssw.com.invalid on Sun Aug 28 10:28:07 2022
    On Fri, 26 Aug 2022 16:08:20 -0600, Louis Krupp <lkrupp@invalid.pssw.com.invalid> wrote:

    On 8/26/2022 2:49 PM, Lynn McGuire wrote:
    “Why do arrays start at 0?"
    https://buttondown.email/hillelwayne/archive/why-do-arrays-start-at-0/

    "It's not the reason you think. No, it's not that reason either.”

    My Fortran starts at one.  My C++ starts at zero.  This has made my
    life hell.


    Here's a crude outline of a solution:

    If you want a C++ array index to start at 1, make the array one element >bigger than necessary, and start indexing at 1, just like in Fortran.
    Element 0 will be unused.

    If you want a C++ array to map exactly to a Fortran array, but with an
    index starting at 1, wrap the C++ array in a class and overload the >subscripting operator(s) to subtract 1 from the index.

    Neither of these approaches seems elegant, but one of them should be
    less hellish than the other.

    Louis

    The authors of "Numerical Recipes" started out with F77 and so all the
    code in their first book has arrays starting at 1. When they brought
    out the C version, they resorted to just this trick!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Charlie Roberts@21:1/5 to garylscott@sbcglobal.net on Sun Aug 28 10:25:35 2022
    On Fri, 26 Aug 2022 16:56:47 -0500, Gary Scott
    <garylscott@sbcglobal.net> wrote:

    On 8/26/2022 3:49 PM, Lynn McGuire wrote:
    “Why do arrays start at 0?"
       https://buttondown.email/hillelwayne/archive/why-do-arrays-start-at-0/ >>
    "It's not the reason you think. No, it's not that reason either.”

    My Fortran starts at one.  My C++ starts at zero.  This has made my life
    hell.

    Lynn

    Fortran was there first...and got it right...you can change Fortran to
    start at other index values for many cases.

    Not sure which universe the OP lives in, but from Fortran 77 onwards
    arrays could be indexed as desired by the user. As a starting graduate
    students in condensed matter physics, my cohort and myself were
    very thankful for this as many quantities are in solid state physics
    indexed from -(N/2-1) to N/2. We did not have to do all the
    shifting that F66 users did.

    Also useful in relativity if you just want to call time the zero-th
    dimension and leave 1, 2 and 3 for x, y and z.

    It was surprsing when I came across FFT codes that were written
    with the shifting of the negavite frequencies to the positive side.
    Not a show stopper, but it nice to see code conform to the
    formulae when one is a beginner to the field and computing.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Koenig@21:1/5 to Charlie Roberts on Sun Aug 28 16:30:04 2022
    Charlie Roberts <croberts@gmail.com> schrieb:
    On Fri, 26 Aug 2022 16:08:20 -0600, Louis Krupp
    <lkrupp@invalid.pssw.com.invalid> wrote:

    On 8/26/2022 2:49 PM, Lynn McGuire wrote:
    “Why do arrays start at 0?"
    https://buttondown.email/hillelwayne/archive/why-do-arrays-start-at-0/

    "It's not the reason you think. No, it's not that reason either.”

    My Fortran starts at one.  My C++ starts at zero.  This has made my
    life hell.


    Here's a crude outline of a solution:

    If you want a C++ array index to start at 1, make the array one element >>bigger than necessary, and start indexing at 1, just like in Fortran. >>Element 0 will be unused.

    If you want a C++ array to map exactly to a Fortran array, but with an >>index starting at 1, wrap the C++ array in a class and overload the >>subscripting operator(s) to subtract 1 from the index.

    Neither of these approaches seems elegant, but one of them should be
    less hellish than the other.

    Louis

    The authors of "Numerical Recipes" started out with F77 and so all the
    code in their first book has arrays starting at 1. When they brought
    out the C version, they resorted to just this trick!

    Ugh.

    I never read the C (or C++) versions, the latest one I used is
    the 2nd edition in Fortran 77 (which I still have within reach at
    my desk).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)