You are viewing a single comment's thread from:

RE: 2 + 2 = 5 And I Can Prove It

in #math5 years ago

In modern programming languages, array indices usually begin at zero. This kind of threw me off when I first started coding as I expected the array to start at index 1.

Here is a relevant wiki covering zero-based numbering.

https://en.m.wikipedia.org/wiki/Zero-based_numbering

I'm struggling to see the logic; however, in how this applies to arithmetic operations as you suggest.

Let's suppose the following example in Python:

arr = ['thing1', 'thing2']
arr2 = ['thing3', 'thing4']

In this, I instantiate two list variables. If I print index 0 of arr, it will return 'thing1'. If I perform len(arr), it will return a count of 2.

Let's say I create a new list (arr3) and use a couple for loops to add the items in both arr and arr2 to it.

If I perform a len(arr3), the result will be 4 even though the index starts at 0. The last element of arr3 would then be arr3[3] = 'thing4'.

See how that can be confusing? The last index is 3 but the count is 4. However, this does not change the laws of arithmetic. 2+2 still equals 4.

Posted using Partiko Android

Sort:  

But what about 'thing0'? In order for counting from 0 to work, you need to begin the count at 0, otherwise you're just converting one counting method to the other.

Indexes aren't counting, they are offsets added to the array's starting memory location. The first object starts at the array's first memory location, the 2nd object's location is after the first object, so we indicate the offset index of 1, move 1 entry away from start of array, 2nd object is here.

Coin Marketplace

STEEM 0.29
TRX 0.12
JST 0.032
BTC 63701.36
ETH 3076.90
USDT 1.00
SBD 3.81