Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - Array numbering starting from 0 instead of 1

 
Thread Tools Search this Thread
Old 11-08-2008, 06:40 AM   #1
Default Array numbering starting from 0 instead of 1


Hi!

I teach C++ in schools in India. I don't have a good answer when
students ask me why arrays in C++ are numbered from 0 to n-1 for an
array of n elements. I hope somebody can tell me.

Thanks
Binoy


bintom
  Reply With Quote
Old 11-08-2008, 06:52 AM   #2
Ian Collins
 
Posts: n/a
Default Re: Array numbering starting from 0 instead of 1
bintom wrote:
> Hi!
>
> I teach C++ in schools in India. I don't have a good answer when
> students ask me why arrays in C++ are numbered from 0 to n-1 for an
> array of n elements. I hope somebody can tell me.
>

I guess one answer is because C does.

Having started many years ago as an assembler programmer, I've always
thought of arrays as pointers to memory. The offset of the first
element is zero, which is the array index zero. Thus given

char n[4];

n+0 is equivalent to n[0];

--
Ian Collins


Ian Collins
  Reply With Quote
Old 11-08-2008, 08:03 AM   #3
Salt_Peter
 
Posts: n/a
Default Re: Array numbering starting from 0 instead of 1
On Nov 8, 1:40 am, bintom <binoythomas1...@gmail.com> wrote:
> Hi!
>
> I teach C++ in schools in India. I don't have a good answer when
> students ask me why arrays in C++ are numbered from 0 to n-1 for an
> array of n elements. I hope somebody can tell me.
>
> Thanks
> Binoy


You have a stack of books on a table. The book at the bottom of the
pile is 0 books away from the table, its offset is zero. The next book
is a book away from the table, offset of 1.

Humans invented the decimal system because we have 10 fingers, label
each finger using a digit only. 10 is not a single digit.

The best answer is one involving range. If you know you have 10 books
then 10 is an upper limit:

const int n = 10;
books stack[n];

for( int i = 0; i < n; ++i ) { /*do stuff*/ }

Cover binary arithmetic, where a computer only has 0's and 1's. After
all, a 0 has just as much weight that a binary 1 does.
In the decimal system isn't the first decade 0 -> 9 ? How weird would
it be to suggest that the first decade is 1 -> 10 and the second 11 ->
20. If you would exclude 0 why not exclude 10 and 20 as well?

Those languages that do use index 1->10 in an array[10] allocate 11
elements and the first one is ignored / waisted.



Salt_Peter
  Reply With Quote
Old 11-08-2008, 08:47 AM   #4
Rolf Magnus
 
Posts: n/a
Default Re: Array numbering starting from 0 instead of 1
bintom wrote:

> Hi!
>
> I teach C++ in schools in India. I don't have a good answer when
> students ask me why arrays in C++ are numbered from 0 to n-1 for an
> array of n elements. I hope somebody can tell me.


The reason why it's like that in C++: The array notion a[b] is equivalent to *(a+b), where usually, a is the address of the array's first element, and b is the index. So if you want the first element, b must be 0. Basically, that's how all computers do it. So if you have a language that starts at 1, like e.g. Matlab does, the interpreter has to subtract 1 from every index or alternatively leave the first element blank.

Generally, I'd rather ask why we tend to start counting at 1 instead of 0, but my guess is that this has historical reasons (for quite a long time, there was no number 0). Sometimes, however, we do count from 0, and sometimes, we even mx it up, like e.g. time. A day starts at hour 0, but a month starts at day 1. Kind of strange, isn't it?



Rolf Magnus
  Reply With Quote
Old 11-08-2008, 07:40 PM   #5
gw7rib@aol.com
 
Posts: n/a
Default Re: Array numbering starting from 0 instead of 1
On 8 Nov, 06:52, Ian Collins <ian-n...@hotmail.com> wrote:
> bintom wrote:
> > Hi!

>
> > I teach C++ in schools in India. I don't have a good answer when
> > students ask me why arrays in C++ are numbered from 0 to n-1 for an
> > array of n elements. I hope somebody can tell me.

>
> I guess one answer is because C does.
>
> Having started many years ago as an assembler programmer, I've always
> thought of arrays as pointers to memory. *The offset of the first
> element is zero, which is the array index zero. *Thus given
>
> char n[4];
>
> n+0 is equivalent to n[0];


To go back further - C is derived from BCPL. In BCPL, you can create
an array as follows:

LET V = VEC 5

which would actually reserve an array of six members, from V!0 to V!5.
(It uses "!" instead of "[]".) So you could start from 1, and ignore
the first one; or start at 0 and ignore the last one; or start at 0
and use a number in the VEC that was one less than the number you
actually wanted. Presumably C and C++'s arrangement was supposed to be
an improvement on this.


gw7rib@aol.com
  Reply With Quote
Old 11-08-2008, 08:17 PM   #6
Stefan Ram
 
Posts: n/a
Default Re: Array numbering starting from 0 instead of 1
bintom <> writes:
>I teach C++ in schools in India. I don't have a good answer when
>students ask me why arrays in C++ are numbered from 0 to n-1 for an
>array of n elements. I hope somebody can tell me.


http://www.purl.org/stefan_ram/pub/zero



Stefan Ram
  Reply With Quote
Old 11-09-2008, 12:15 PM   #7
James Kanze
 
Posts: n/a
Default Re: Array numbering starting from 0 instead of 1
On Nov 8, 9:47*am, Rolf Magnus <ramag...@t-online.de> wrote:
> bintom wrote:


> > I teach C++ in schools in India. I don't have a good answer
> > when students ask me why arrays in C++ are numbered from 0
> > to n-1 for an array of n elements. I hope somebody can tell
> > me.


> The reason why it's like that in C++: The array notion a[b] is
> equivalent to *(a+b), where usually, a is the address of the
> array's first element, and b is the index. So if you want the
> first element, b must be 0. Basically, that's how all
> computers do it. So if you have a language that starts at 1,
> like e.g. Matlab does, the interpreter has to subtract 1 from
> every index or alternatively leave the first element blank.


That's basically it. If you really want to make it clear,
however, without going into the nitty-gritty of transforming []
into * and +, consider "flatting" multidimensional arrays.
Given an array with dimension [10][10], for example, map the
indexes into those of an array [100]. With arrays based at 0,
it's simple 10*i+j. With arrays based at 1, you have to
subtract 1 from each index, then do the calcule, then add 1,
e.g. 10*(i-1)+j (with more dimensions, the difference becomes
even more apparent).

> Generally, I'd rather ask why we tend to start counting at 1
> instead of 0, but my guess is that this has historical reasons
> (for quite a long time, there was no number 0).


Counting makes sense: if you have one book, you have one, and
not zero. It's indexing that doesn't, or rather ordinal
numbers in general.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34



James Kanze
  Reply With Quote
Old 11-10-2008, 09:49 AM   #8
Pascal J. Bourguignon
 
Posts: n/a
Default Re: Array numbering starting from 0 instead of 1
bintom <> writes:

> Hi!
>
> I teach C++ in schools in India. I don't have a good answer when
> students ask me why arrays in C++ are numbered from 0 to n-1 for an
> array of n elements. I hope somebody can tell me.



http://en.wikipedia.org/wiki/Array

Why numbering should start at zero:
http://www.cs.utexas.edu/users/EWD/t...xx/EWD831.html

--
__Pascal Bourguignon__


Pascal J. Bourguignon
  Reply With Quote
Old 11-10-2008, 11:03 AM   #9
Bill Davy
 
Posts: n/a
Default Re: Array numbering starting from 0 instead of 1
It might make the pill easier to swallow if you mention that zero is one of
India's great contribution to mathematics, along with a slew of famous
mathematicians.


"bintom" <> wrote in message
news:5d18ac9a-d826-4743-bd46-...
> Hi!
>
> I teach C++ in schools in India. I don't have a good answer when
> students ask me why arrays in C++ are numbered from 0 to n-1 for an
> array of n elements. I hope somebody can tell me.
>
> Thanks
> Binoy





Bill Davy
  Reply With Quote
Old 11-10-2008, 03:18 PM   #10
.rhavin grobert
 
Posts: n/a
Default Re: Array numbering starting from 0 instead of 1
On 8 Nov., 07:52, Ian Collins <ian-n...@hotmail.com> wrote:
> bintom wrote:
> > Hi!

>
> > I teach C++ in schools in India. I don't have a good answer when
> > students ask me why arrays in C++ are numbered from 0 to n-1 for an
> > array of n elements. I hope somebody can tell me.

>
> I guess one answer is because C does.
>
> Having started many years ago as an assembler programmer, I've always
> thought of arrays as pointers to memory. *The offset of the first
> element is zero, which is the array index zero. *Thus given


As computers generally start counting at zero and for consistency, i
allway to prefer to say that the first array element in array x is
x[1]. x[0] is the zeroth element.

>
> char n[4];
>
> n+0 is equivalent to n[0];
>
> --
> Ian Collins




.rhavin grobert
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
VHDL and EDK: Custom IP core containing an array as a port using EDK allsey_1987 Hardware 0 10-27-2009 02:26 PM
Array Programme rits Software 2 03-04-2009 05:18 PM
Starting your own business Tom Miller A+ Certification 46 10-22-2006 09:40 PM
DVD Verdict reviews: STARTING OVER, BOOK OF LOVE, PUT THE CAMERA ON ME, and more! DVD Verdict DVD Video 0 08-05-2005 09:15 AM
Are USED Dvd titles starting to make an impact? tabernacle2002@hotmail.com DVD Video 1 01-08-2005 09:19 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46