473,473 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Nov 8 '08 #1
9 9446
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
Nov 8 '08 #2
On Nov 8, 1:40 am, bintom <binoythomas1...@gmail.comwrote:
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.

Nov 8 '08 #3
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?

Nov 8 '08 #4
On 8 Nov, 06:52, Ian Collins <ian-n...@hotmail.comwrote:
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.
Nov 8 '08 #5
On Fri, 7 Nov 2008 22:40:51 -0800 (PST), bintom
<bi*************@gmail.comwrote:
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.
Tell them it's because 0 was invented in India, that might at least
make them feel proud :)

Tony
Nov 8 '08 #6
On Nov 8, 9:47*am, Rolf Magnus <ramag...@t-online.dewrote:
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:ja*********@gmail.com
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

Nov 9 '08 #7
bintom <bi*************@gmail.comwrites:
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__
Nov 10 '08 #8
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" <bi*************@gmail.comwrote in message
news:5d**********************************@b2g2000p rf.googlegroups.com...
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

Nov 10 '08 #9
On 8 Nov., 07:52, Ian Collins <ian-n...@hotmail.comwrote:
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
Nov 10 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Wayne Aprato | last post by:
I have a report that shows the results of a query. One of the fields is an autonumber field from the query which shows for instance: 120, 121 , 122 for 3 records. Is there a way to have another...
2
by: Wayne Aprato | last post by:
I posted this yesterday and it seems like a moderator has thrown it in another thread. This is a totally different question to the one asked in that thread, so I'm posting it again. It is not a...
1
by: Jack | last post by:
Hi there, I have an multi-dimensional array (5,i) (5 'columns', i 'rows') where i is the number of records in the recordset that I use to populate the array and hence the number of 'rows' in the...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
5
by: Robin Tucker | last post by:
I need to marshal an IntPtr (which I've got from GlobalLock of an HGLOBAL) into a byte array. I know the size of the array required and I've got a pointer to the blob, but I can't see how to copy...
11
by: benben | last post by:
Recently I have come across a number of struct's that I think is quite interesting: typedef struct whatever_tag { int member1; char member2; // ... char last_member; // interesting line
8
by: Jim | last post by:
In a C# project I'm working on for an iterative design application, I need to dispose of a large arrray of a struct object and reinitialize the array between iterations. That is, the user starts...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
2
by: awaisworld13 | last post by:
I want to write a program which take input and save the poistions of the array elements in another array. let if i enter an array A= 4 A=1 A=2 A=3 now the output array will hold 1 2 3
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.