[comp.lang.modula2] 0-based/1-based arrays

pattis@june.cs.washington.edu (Richard Pattis) (03/03/88)

I too detest 0-based arrays (and think 1-based arrays are better).  In trying
to rationalize this choice, I came up the the following argument: When we
process arrays, we often need to know the size of the array and/or the
position of the last element.  In 0-based arrays, these quantites are off by
one; in 1-based arrays they are the same.  Hence, I think that people will
make fewer errors with 1-based arrays.

Rich Pattis

karl@haddock.ISC.COM (Karl Heuer) (03/03/88)

In article <4343@june.cs.washington.edu> pattis@june.cs.washington.edu (Richard Pattis) writes:
>I too detest 0-based arrays (and think 1-based arrays are better).  In trying
>to rationalize this choice, I came up the the following argument: When we
>process arrays, we often need to know the size of the array and/or the
>position of the last element.  In 0-based arrays, these quantites are off by
>one; in 1-based arrays they are the same.  Hence, I think that people will
>make fewer errors with 1-based arrays.

When dealing with 0-based arrays, the appropriate concept is a half-open
interval: the index for an N-element array comes from [0,N).  I find that this
makes things simpler than with 1-based arrays, because half-open intervals can
be abutted without having to adjust by one.  (I.e., [0,N) U [N,P) = [0,P).)

I very seldom need to know the position of the last element.  Instead, I use
the position of the first element beyond the array.  Always use postincrement
and/or predecrement to traverse it; everything works out fine.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

pds@quintus.UUCP (Peter Schachte) (03/04/88)

In article <2851@haddock.ISC.COM>, karl@haddock.ISC.COM (Karl Heuer) writes:
> When dealing with 0-based arrays, the appropriate concept is a half-open
> interval: the index for an N-element array comes from [0,N).  I find that this
> makes things simpler than with 1-based arrays, because half-open intervals can
> be abutted without having to adjust by one.  (I.e., [0,N) U [N,P) = [0,P).)

How is that any different than for 1-based arrays?

	[1,N) U [N,P) = [1,P), non?

In both of the rebuttals to the pro-1-based-array postings, I didn't see
anything that said where 1-based arrays fall down.  The only problem I
see with them is efficiency:  you have to subtract 1 (or 2 or 4 or
sizeof(element)) when you index into the array.  Or do you?  There's
nothing to say that the compiler can't actually allocate an array one
element bigger than you ask for, and just waste the first element, is
there?  If you've got the memory to burn.  C, of course would be in
trouble with this approach, since arrays in C are so often traversed by
pointer manipulation.  But in languages that don't allow this (and, I
hope, their compilers produce similar code anyway), there shouldn't be a
problem.

As someone said, one-based arrays often seem more natural.  I want to
store 10 of these things.  So I allocate a ten element array.  Now,
quick, which is the last element?  Number NINE?  Huh?
-- 
-Peter Schachte
pds@quintus.uucp
...!sun!quintus!pds

jk3k+@andrew.cmu.edu (Joe Keane) (03/05/88)

I find 0-based arrays to be more elegant.  The formulas for computing the 
position of an element are simpler, especially in a mulitdimensional array.  
The element which corresponds to size of the array is the beginning of the 
next array, which may be more useful than the last element in the array.  
However, i do like freedom of choice to sometimes pick a different base.

--Joe

kjepo@amnesix.liu.se (Kjell Post) (03/07/88)

As long as mod returns 0 I will have 0-based arrays!
-- 
-------------------------------------------------------------------------------
       (Y F) = (F (Y F))           |Dept of Comp&Info Science, Linkoping Sweden
"This super-amazing, clever thing" |          kjepo@majestix.liu.se
	- G.J. Sussman             |     ...liuida!majestix.liu.se!kjepo

csnjr@its63b.ed.ac.uk (Nick Rothwell) (03/07/88)

In article <726@sandino.quintus.UUCP> pds@quintus.UUCP (Peter Schachte) writes:
>As someone said, one-based arrays often seem more natural.  I want to
>store 10 of these things.  So I allocate a ten element array.  Now,
>quick, which is the last element?  Number NINE?  Huh?

Yes. 'fraid so! The "9" popped into my head straight away.
I'm an ML hacker. I'm used to traversing lists. If I have a list of 10
elements, I have to take its tail 9 times. If I want the first element,
I do 0 operations. So, I think from 0 upwards...
-- 
Nick Rothwell,	Laboratory for Foundations of Computer Science, Edinburgh.
		nick%lfcs.ed.ac.uk@nss.cs.ucl.ac.uk
		<Atlantic Ocean>!mcvax!ukc!lfcs!nick
~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~
...while the builders of the cages sleep with bullets bars and stone,
they do not see your road to freedom that you build with flesh and bone.

ray@micomvax.UUCP (Ray Dunn) (03/16/88)

In article <4343@june.cs.washington.edu> pattis@june.cs.washington.edu (Richard Pattis) writes:
>I too detest 0-based arrays (and think 1-based arrays are better).  In trying
>to rationalize this choice, I came up the the following argument: When we
>process arrays, we often need to know the size of the array and/or the
>position of the last element.  In 0-based arrays, these quantites are off by
>one; in 1-based arrays they are the same.  Hence, I think that people will
>make fewer errors with 1-based arrays.
>

The question is, I believe, does the 'structure' fit naturally with the problem
you are trying to solve when using the array.

Sometimes 0-based is what you want, sometime 1-based is what you want,
sometimes n, or -n or etc is what you want!

How does the Algol declaration go??  (thinks back about 15 years)
array[x:y]??    (I probably have the syntax wrong!)
where x and y are positive or negative integers defining the lower and upper
bounds of the array's index!

Thus i[-20:50] is a 71 element array indexed from -20 through 50.

Ray Dunn.  {philabs, mnetor, musocs}!micomvax!ray

uday@mips.COM (Uday Kurkure) (03/18/88)

In article <942@micomvax.UUCP>, ray@micomvax.UUCP (Ray Dunn) writes:
> The question is, I believe, does the 'structure' fit naturally with the problem
> you are trying to solve when using the array.
> 
> Sometimes 0-based is what you want, sometime 1-based is what you want,
> sometimes n, or -n or etc is what you want!


   In Mathematics, zero means nothing, void. In computer science, zero
   has a meaning depending on the the context. There exists a memory location
   at zero address. As long as there are addresses starting from zero, 0-based/
   1-based arrays would be a matter of taste.


                                               ..Uday

   
   
   

darryl@ism780c.UUCP (Darryl Richman) (03/19/88)

In article <1886@gumby.mips.COM> uday@mips.COM (Uday Kurkure) writes:
>   In Mathematics, zero means nothing, void. In computer science, zero

I'm not a math major, but 0 is not void.  Void is null (e.g., the slashed
capital O).  This is a very important distinction.

		--Darryl Richman
-- 
Copyright (c) 1988 Darryl Richman	INTERACTIVE Systems Corporation
	"Curiouser and curiouser"	An Eastman Kodak Company
    The views expressed above are	...!cca!ima!ism780c!darryl
	     those of the author.	...!sdcrdcf!

douglas@reed.UUCP (P Douglas Reeder) (03/19/88)

I like being able to make the lower bound of an array  whichever I prefer
and is the sensible one.  Allowing this requires only a trivial addition to
array element evaluation:  the lower bound is subtracted from the index first.

-- 
Doug Reeder                    USENET: ...!tektronix!reed!douglas
Box 502 Reed College      from BITNET: douglas@reed.UUCP
3203 S.E. Woodstock        from  ARPA: !tektronix!reed!douglas@Berkley
Portland, OR 97202     NGC4503: Local Group,Milky Way,Sol III,122 38'W 45 28'N

g-rh@cca.CCA.COM (Richard Harter) (03/20/88)

In article <9475@ism780c.UUCP> darryl@ism780c.UUCP (Darryl Richman) writes:
>In article <1886@gumby.mips.COM> uday@mips.COM (Uday Kurkure) writes:
>>   In Mathematics, zero means nothing, void. In computer science, zero

>I'm not a math major, but 0 is not void.  Void is null (e.g., the slashed
>capital O).  This is a very important distinction.

Please, let us not confuse things.  The word 'number' has different meanings
in different contexts.  This may be obscured by that fact one uses the same
symbols and the same 'things' so to speak, but it is nonetheless true.

A cardinal number is (or can be treated as) the measure of the size of a
set.  I.e. some sets have one element, some sets have two elements, and
so on.  [A set, for the totally non-mathematical, is a collection of things
considered as a whole.  The things in the collection are called elements
of the set.]  The void set, or empty set, is [the] set with no elements.
Zero (0) is measure of the size of the void set.  Cardinal numbers start
with 0 and go 0,1,2,...

An ordinal number is a specification of the position of an element in an
ordered sequence.  If I have a sequence, <a,b,c> a is the first element,
b is the second, and c is the third.  An ordinal number is related to
a cardinal number since the ordinal of the last element of the sequence
is the 'same' as the cardinal measuring the size of the sequence, considered
as a set.  I.e., in a sequence with n elements the last one is the n'th.
Ordinals start with 1, and go 1,2,3,...  

In arithmetic the numbers are primitive elements with arithmetic operations
defined on them.  Zero is the identity element, i.e. the number such that
for all x, x+0 = x.  It turns out that there is a direct correspondence
between the non-negative integers of arithmetic and the cardinal numbers.

And so on.  Sorry to be pedantic, but what a number is and what it means
depends on what you are talking about and what you mean.
-- 

In the fields of Hell where the grass grows high
Are the graves of dreams allowed to die.
	Richard Harter, SMDS  Inc.

alan@pdn.UUCP (Alan Lovejoy) (03/22/88)

In article <8496@reed.UUCP> douglas@reed.UUCP (P Douglas Reeder) writes:
>I like being able to make the lower bound of an array  whichever I prefer
>and is the sensible one.  Allowing this requires only a trivial addition to
>array element evaluation:  the lower bound is subtracted from the index first.

That's way too complicated.  The right way is to bias the base address
of the array so that (Rn, Dn*ElementSize) points to the first element of the 
array when Dn's value is the lower index bound.   

Thus for 'a: ARRAY [m..n] OF Element', the compiler's (linker's)
internal base address for the array 'a' would be 

  ADR(firstElement) - (m * TSIZE(Element))

so that for positive m the base address is less than the address of the
first element by m * TSIZE(Element) size units, and for negative m it
is greater than the address of the first element by that amount.

This is just removing an invariant computation from run-time to compile-time
where it belongs.

--alann@pdn

nevin1@ihlpf.ATT.COM (00704a-Liber) (03/22/88)

In article <8496@reed.UUCP> douglas@reed.UUCP (P Douglas Reeder) writes:
>I like being able to make the lower bound of an array  whichever I prefer
>and is the sensible one.  Allowing this requires only a trivial addition to
>array element evaluation:  the lower bound is subtracted from the index first.

Yes, but that must be done to EACH AND EVERY reference to the array!!
(Actually, there are other ways to implement arrays, but they all boil down
to more runtime overhead).  I think that Wirth is trying to get rid of as
much runtime overhead as he can without losing the notion of
'verifyability'.  And I think he has accomplished that (whether this is
good or bad is a matter of opinion).
-- 
 _ __			NEVIN J. LIBER	..!ihnp4!ihlpf!nevin1	(312) 510-6194
' )  )				"The secret compartment of my ring I fill
 /  / _ , __o  ____		 with an Underdog super-energy pill."
/  (_</_\/ <__/ / <_	These are solely MY opinions, not AT&T's, blah blah blah

uday@mips.COM (Robert Redford) (03/22/88)

In article <6276@ames.arpa>, eugene@pioneer.arpa (Eugene N. Miya) writes:
> In article <1886@gumby.mips.COM> uday@mips.COM (Uday Kurkure) writes:
> >   In Mathematics, zero means nothing, void. In computer science, zero
> >   has a meaning depending on the the context. There exists a memory location
> >   at zero address. As long as there are addresses starting from zero, 0-based/
> >   1-based arrays would be a matter of taste.
> 
> Sorry, nope.  Zero is a place holder, the additive identity, it does not
> mean void, etc.  This is why the null set, $phi$, and {} were created
> by Cantor(?).  Zero based starts are excellent for initial conditions,
> and negative subscripts are useful on occasion.


      Sorry for using the terms nothing, void rather loosely. 

      My statement was based on the very common, day to day interpretation 
      given to zero. ( i.e. cardinality of a null set )

      When someone  has zero apples, it means that he has no
      apples. If there are three apples on the table, one would start 
      refering to them as first apple, second apple, third apple.
      If there are 3 memory cells in the computer, one starts refering 
      to them as zeroth memory cell, first memory cell and second memory 
      cell. 
      


                                             ..Uday

nevin1@ihlpf.ATT.COM (00704a-Liber) (03/23/88)

In article <1915@mips.mips.COM> uday@mips.COM (Robert Redford) writes:
>      When someone  has zero apples, it means that he has no
>      apples. If there are three apples on the table, one would start 
>      refering to them as first apple, second apple, third apple.

This is assuming, of course, that he wants to put the apples in some kind
of ORDER.  He arbituarily assigns names to them (ie, first, second, third).
He could have just as easily assigned them names like Apple, Apple II,
Apple II+, Apple IIe, Apple IIGS, Lisa, MacIntosh, Granny, etc.  :-)  He is
just making up names so he can distinguish them.

>      If there are 3 memory cells in the computer, one starts refering 
>      to them as zeroth memory cell, first memory cell and second memory 
>      cell. 

Ah, but memory cells have ADDRESSES; ie, a quantity that can distinguish
different memory cells (ex:  memory location 0 and memory location 1 need
different voltages on the address pins of the chip to access them).  I
don't know about where you live, but where I live addresses don't start at
1 and go up consecutively. :-) :-)

They are given a numbering system so that they can be distinguished and
referred to distinctly (do people really talk about the nth memory cell??
:-)).  Choosing a system that is related to the physical voltages needed is
a good choice, not bad.

BTW, what does this have to do with 0/1 based arrays???  :-) :-)
-- 
 _ __			NEVIN J. LIBER	..!ihnp4!ihlpf!nevin1	(312) 510-6194
' )  )				"The secret compartment of my ring I fill
 /  / _ , __o  ____		 with an Underdog super-energy pill."
/  (_</_\/ <__/ / <_	These are solely MY opinions, not AT&T's, blah blah blah

uday@mips.COM (Robert Redford) (03/24/88)

In article <4089@ihlpf.ATT.COM>, nevin1@ihlpf.ATT.COM (00704a-Liber) writes:
> [ .. lines deleted ]
>
> They are given a numbering system so that they can be distinguished and
> referred to distinctly (do people really talk about the nth memory cell??
> :-)).  


   YES. People REALLY talk about the nth memory cell.


> Choosing a system that is related to the physical voltages needed is
> a good choice, not bad.


   Nobody said it was a bad choice.


> BTW, what does this have to do with 0/1 based arrays???  :-) :-)


    It has probably something to do with how one accesses the array elements.

    Perhaps the array specification should include both the upper and the lower
    bound. 
    

 
                                               ..Uday