# Tag Info

222

None of the reasons you suggest really get to the heart of why we use zero-indexing in CS. Dijkstra's EWD831 explains why this convention works out the best. It comes down to the fact that we want to represent sequences of integers as half-open intervals that are inclusive on the start side. To denote the subsequence of natural numbers 2, 3, ..., 12 ...

114

If I can modify the question, I can answer what I believe you are looking for. The question is "Why do we count starting from zero?" The answer is "we don't" Not even in computer science do we "count" from zero. A list with 15 items in it from outside CS still has 15 items in it inside CS realms. Many languages include a count type function for arrays, and ...

62

I'm surprised that the following hasn't been stated yet. All of the answers given so far seem to be "after the fact" explanations of something that is really based on the way people built most (not all) early computers as binary machines. To make things a bit more compact here, I'll assume we are creating a 4 bit (nibble) based machine. We don't want to ...

43

You can get at this concept very intuitively in strings before you ever get to arrays. Take a string like "hello world" and ask them a subtle-sounding point: does the string begin here: "*hello world", or here: "h*ello world". They'll certainly be able to identify the correct answer. Then ask, "how far from the start of the string do we have to go to get ...

40

You have already gotten several good answers about why zero-based indexing is useful, and you have been explained the difference between indexing and counting. I now want to challenge the basic premise of your question: it is not, in fact, universally agreed-upon that indexing starts at zero. In Excel, which is arguably the most widely-used programming ...

38

Ages in the United States (it's not the same around the world). For the first year of life, children are 0 years old. Only after completion of a year is the age changed to 1. By this logic, a child in his/her second year of life is 1 just as the second element of an array is found at index 1. Edit: another example is the counting of centuries. An event ...

34

An analogy that will work well in Europe, but not in North America: Image licensed under the CC BY-SA 3.0 by Bidgee of Wikimedia Commons. Floors (in European countries) are typically numbered with 0 (or G) as the ground floor, then 1 as the first floor above ground, etc. This could easily be compared to a list/array of floors, with floors[0] being ground, ...

23

The clock (24 hours system) If we look at the clock, we have two examples of counting hours. The 24 hour system starts at 0. The 12 hour system is interesting, neither starting at 0 or 1. The 12-hour clock In the 12-hour clock system we start at 12 and make our way up to 11. This needs some explanation. The 12 hour clock has a funny way of counting, that you ...

23

When I've explained this to beginning students, I don't stray far from your third reason, though I agree that the beginning of arrays is early to introduce the concept of memory addresses. Among other things, that invites about your variable, c, when the only variable they need to worry about is i. Instead of memory addresses, I talk about distances from ...

11

I don't see anything confusing in 0-based indexing. In fact, it seems that 0-based indexing is not less natural for humans than 1-based indexing. Humans use 1-based indexing just because of following some long-standing, but weird tradition. I remember that when I was a pre-school child (<7 years old) and has not been yet influenced by social traditions ...

11

You overestimate the complexity of 0-based indexing a lot. There is nothing complex in 0-based indexing. On the other side, the topic of the pointers is relatively complex. I don't think it has any sense to introduce pointers earlier, than at the time when a pupil would be ready to fully feel their usefulness and use them in practice. Especially in languages ...

10

This is a great explanation of why 0-indexing exists. As someone who barely knows what a pointer is, your explanation made perfect sense. If you wanted to dumb it down a little you could phrase it as: When you create an array, the variable you assign that array to is actually a pointer to the first element. Lets create an array num. When you want to ...

10

In computer science, we usually count starting from 0. In programming or in (theoretical) computer science? In Programming In C programming language you count from 0 to (N-1). And of course in languages which are influenced by C: Java, JavaScript, PHP, C#, C++... You already named the reason for this: In C, we can use *a to access the first element of ...

9

In languages such as C, the first item in an array has an offset of zero from the pointer. If the size of your objects are 4 bytes, the next item has an offset of 1 x 4 bytes. Index Size Offset 0 4 base + 0 1 4 base + 4 2 4 base + 8 3 4 base + 12 And so on. If the items were indexed on natural counting, ...

8

First, see this question on being pedantic. Second, consider the fact that you are teaching Computer Science and not Mathematics. The former is not a subset of the latter. There is some overlap, of course. Next, I see no issue with being precise about terminology when students might be studying both fields. If you want to clearly distinguish between vector ...

7

Exponents representing the values of the digits in positional number systems. e.g. the-arabic-system / denary / the-system-you-learnt-in-primary-school. 10³ 10² 10¹ 10⁰ 1000 100 10 1 or binary 2³ 2² 2¹ 2⁰ 8 4 2 1 The binary example in binary 10¹¹ 10¹⁰ 10¹ 10⁰ 1000 100 10 1 This was @GypsySpellweaver idea, see comment.

7

The Dutch National Flag problem is linear in running time. Essentially sort an array with only 3 distinct values each of which may appear 0 or more times. (not length 3). You are allowed only one pass over the array, so the solution is a single while loop with some prior initialization. It was probably originally posed by Dijkstra. It is mentioned in David ...

7

A variant on the ENIGMA machine encryption works well in a single loop, and is sufficiently complex to give students a real challenge. The core idea of the ENIGMA machine for this assignment is that (1) a number is given as an initial key, and (2) every prior letter used influences how the next letter will be encrypted. So, use a modular circle of ...

7

Simply put, we do not count from zero, we shift from zero You can think C as a neat way to not write different assembly for every architecture/machine/processor in existence. Instead, take a simple and short macro-like language for a abstracted machine, compile that, and brk() your way on abstracted memory. But abstracted memory is only a sequence of bytes,...

7

I believe that the CS notion of a vector is not related to the concept of a vector as a fixed size set of components, and rather comes from the idea of a rank 1 tensor (which is also called a vector). A rank 1 tensor is simply a list of elements that is indexed linearly, so you only need one coordinate to retrieve an element (as opposed to a matrix, which is ...

7

You are correct and incorrect at the same time. Nomenclature is important when teaching, and when programming. Being pedantic about nomenclature is a slipper slope and a morass. As noted by @Buffy in another answer, that's been addressed here as well. In the case of vector and matrix they have specific meaning, usage, and properties in mathematics. They ...

6

Rulers start at zero: At zero centimetres, you have nothing, whereas at one centimetres, you have one unit of length. Memory is similar: You start filling at the beginning (zero), hence, the first x-memory units contain x units of data, the next unit contains unit x+1.

6

Draw ongoing attention to the potentially confusing point by banning cardinal descriptions of an element's position. Avoid referring to the "first element" or "second element" and talk only about "element at index 0" or "at index 1." Insist that your students speak only of indices and not of position in a sequence. If you ever use cardinal numbers in human ...

6

From time to time I have had to do numerical work in MATLAB and the 1-based indexing always stuck out like a sore thumb, so I have a few examples I can provide where 0-based indexing is advantageous. Modular access This is the simplest example. If you want to "wrap around" an array, the modulus operator works like a charm. (And don't say "what if the ...

6

Rather than answer myself based off my personal preference, as all other posters have done, I would instead like to link the following very in-depth analysis instead: Arrays Indexed From One by Loki Patrick The conclusion it comes to, is as follows: In conclusion, I set out to prove by usage cases which scheme was better: indexing arrays from zero, or ...

6

When introducing terms that have different meanings in different fields, first, accept that both uses are correct. As others have alluded, this is a case of two different fields using the same term in two different ways based on a similarity to a root idea. Disparaging the use of the language to your students might make them feel worse about the usage, or ...

5

I will try to answer, without reference to low-level programming (or any programming language), without mention of history, or much maths. As already stated in some answers, we do not count from zero (using the value 0 to represent 1, and 1 to represent 2 (Usually)). So what do we do? We measure from zero. If I give you a ruler, and ask you to measure ...

4

A variant on the usual random drill and practice test would be to pre-populate with the questions and answers, then remove question and answer from each as they get answered correctly, allowing players to get more practice on the questions they get wrong. Here's an example for times tables. You could try something for an adventure game, building up an ...

4

I am not advocating teaching pointers to explain 0-indexing (see mine and other's answers on your other question for how to do that). However, if we have good reason to teach pointers, here is a tip that helped me when I was learning. I have also shared these ideas with other pupils, and they started to make progress. I find code involving pointers confusing:...

4

I think it's best to view addresses and indices not as identifying objects or elements, but rather the spaces between or on either side of them. Thus, an array with four elements would have the following indices: Addr: Base+0 Base+1 Base+2 Base+3 Base+4 . V V V V V Indices: 0 1 2 3 4 ...

Only top voted, non community-wiki answers of a minimum length are eligible