Re: should every thing be zero indexed?




Jim Rogers wrote:
> "Arthur J. O'Dwyer" <ajo@xxxxxxxxxxxxxxxxxxxxx> wrote in
> news:Pine.LNX.4.60-041.0505021603400.8307@xxxxxxxxxxxxxxxxxxxxx:
>
> >
> > In other words: C-family languages count from 0 because C does,
and
> > that's for historical reasons (involving, as you guessed, pointer
> > arithmetic). Other families of languages count from 1. You should
use
> > the idiom of the language you're programming in, because it will
help
> > your readers understand what you're doing.
>
> I understand the frustration of the OP. Many problem domains simply
do
> not count items starting at 0. As Arthur points out above, the C
family
> of languages inherited the tendency to count from 0 because of C
array
> indexing. This has even been carried over to Java, which does not
provide
> explicit pointer arithmetic.

It is a sign of mathematical sophistication, taking the long view, to
start counting at zero, although it might get you fired at WalMart
during inventory time.

"0 items, boss." "Whaddya mean, zero? There's still one on the shelf!"
"Right boss, its number is zero: I always start counting at zero".
"Yeah, well you count right on over to payroll and pick up your check,
willya? You're outa here."

To start a count at zero is to acknowledge the contribution Islam's
mathematicians made to the West, for their invention of the zero
allowed Florentine bankers and Venetian merchants to carry customers
with zero balances.

The early IBM mainframes, however, consistently started at one which is
more "businesslike".

Lao T'se reminds us that "in the beginning there was the zero, the Tao,
the way, then there was the one, and then two, and then the myriad (ten
thousand) things". This may be an argument to start counting at zero.

It might actually be a good strategy in relations with the fair sex to
count from zero. Zero is feminine and to start at zero graciously
acknowledges the superiority of the zero to the phallic one.



>
> 0 indexing can be very efficient to execute, but is not the only way

Many architectures have a single instruction for Clear to Zero but not
so many for load one. Of course, as another poster has pointed out,
behind the scenes compiler optimizations can get around this.

> counting and indexing is done. Ada, for instance, allows arrays to be
> indexed starting at any discrete value. Ada provides the concept of a

> range
> as a fundamental concept in both the definition of array indices and
in
> the
> definition of scalar types and subtypes.
>
This feature summarily dropped from Visual Basic as of .Net which also
enforces strict indexing from zero, in part to maintain the
verifiability of .Net CLR code: if an array index can start anywhere
and is assigned (for example) by a by-reference call to a procedure,
the references the object code makes to memory are unbounded and
unpredictable.

> For instance, if you are using an array to record the frequency of
data
> around a central data point you can define the array indices to
precisely
> express that problem. You can even normalize those values so that the

> central data point is set to 0 and variations from that data point
are
> either positive or negative indices.
>
> type Centralized_Index is range -100..100;
>
> type Frequence_Counts is array(Centralized_Index) of Integer;
>
> The type Centralized_Index is an integer type with 201 valid values
from
> -100 through 100. The type Frequency_Counts is an array type
containing
> integer elements. The index type for Frequency_Counts is
> Centralized_Index.
> The range of index values includes all possible values of its index
type.
>
> In this case the code is much closer to the domain problem to use
index
> values from -100 through 100 rather than 0 through 200, as would be
> required in the C family of languages.

This support is restricted to problems where the index is a number but
not bounded on the left by zero or 1 and this is a rather restricted
range. Therefore, it might be said that the facility Ada gives is
trivial in comparision to the idea of addressing an array
associatively.

>
> Of course, Ada provides a simple looping notation for dealing with
the
> range of indices in an array, even though the values may begin and
end
> at any arbitrary value:
>
> foo : Frequency_Counts;
>
> for I in foo'range loop
> Put_Line(Integer'Image(I) & ":" & Integer'Image(Foo(I)));
> end loop;
>
> The 'for' loop above iterates through all the elements in the
> array foo, starting at the lowest index and ending at the highest
> index. For each element it prints the string representation of the
> index followed by a colon, ":", followed by the string representation
> of the integer element at that index.

Again, this feature might be overridden, in conservative code, by code
that starts at a known, fixed place and ends at a limit, and in
advanced code by an iterator which does "something" for all elements on
demand.

Such a construct, in fact, could in 90%-odd cases schedule the n
"something" actions in an optimal fashion as long as, as is usually the
case, there are no dependencies on the results of anyone something.

Such a construct would NOT guarantee the programmer that the iterations
will be executed from n by 1 to m: the programmer would have to specify
this as a requirement.

Asking for trouble, of course. But the idea is in the spirit of
Dijkstra's "guard" which follows Spinoza in eschewing one unnecessary
assumption and allowing the underlying runtime maximum freedom to
schedule the actions of the for loop.

This for loop would have the structure of a quantification: for all x,
do something. Our "not caring" in what order the xs are done unto would
free us from a Fortran era annoyance, which is the fact that arrays,
which want to be sets, have to follow the rules for, and be understood
as, arrays.

I am fooling around with an abstract VB .Net or C# iterator which would
by default schedule each cycle through a for loop as a new thread to
see if the concept flies.
>
> Jim Rogers

.



Relevant Pages

  • Re: Arrays of zero length
    ... The minor strangeness is that when you allocate an array to zero size, ... saying something like that allocating an array makes it undefined ... In f77, zero-size was disallowed, ...
    (comp.lang.fortran)
  • Re: question about data movement
    ... positions were previously initialized to zero, ... The obvious answer is to reverse the "sense" of your array so rather ... Messing with your loop counter inside the loop body is one. ... int current = new_value; ...
    (rec.games.roguelike.development)
  • Re: Subset Sum problem (w/ limited scope)
    ... The "Subset Sum" problem (stated various ways depending on what you ... a subset of those integers that sums up to zero. ... Are there going to be duplicate values in your original array? ...
    (comp.lang.fortran)
  • Re: Need someone very familiar with arrays
    ... Also, it would *probably* be faster to include "start < finish" checks in each loop, rather than have your start and finish loops scan through the entire array once the array is cleared out. ... ReDim Preserve a ... It would in effect move until it reached the last non zero item in the list and then write that value repeatedly whenever a zero was encountered. ...
    (microsoft.public.vb.general.discussion)
  • Re: addEvent - The late entry :)
    ... take any number of arguments and append them to an array. ... argument, which is fine on everything but Firefox, where it makes the ... 20 arguments Windows Safari 3 executes that expression in 48% of the ... and at zero arguments 47%. ...
    (comp.lang.javascript)