Arrary starts at zero | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Arrary starts at zero

As far as I know.. array starts at zero. so a[5] would be six places to store.. right?

9th Oct 2018, 11:21 AM
Vedant Roy
Vedant Roy - avatar
9 Answers
+ 3
I'd say that's not correct. a[5] is the sixth element of an array. Indices are zero-based. However if you define an array like this => int a[5], it can store five integers, not six. In this case, 5 is not an index and not zero-based. The indices would be a[0] to a[4].
9th Oct 2018, 12:43 PM
Anna
Anna - avatar
+ 1
If you have the authority to edit it, please do so..
9th Oct 2018, 11:51 AM
Vedant Roy
Vedant Roy - avatar
+ 1
And that's very useful because if you use a for loop array = new int[length] for (i=0; i<length i++) array[i] =5 In this pseudocode you can see that it's easier with array with 0
9th Oct 2018, 1:00 PM
Daniele Bonomi
Daniele Bonomi - avatar
+ 1
When it comes to counting things (for example the number of items in an array), computers count just like humans. {50, 68, 17, 25} is an array with 4 elements. Its length/count/whatever it is caĺled in the programming language you're working with is 4. So if you want to initialize an array 'a' that can hold 5 integers, you can initialize it with int a[5]. Only when you use indices you start counting from 0. The first item of an array has the index 0, the last item has the index (number of items - 1). That's because in languages like C/C++, the index shows the offset from the memory location of the first item in the array. Let's say the array is stored at the memory location #0. That's also the memory location of the first item of the array, so there is no offset. The second element will be stored at the memory location #0 + 1, hence it can be addressed with the index 1. The third element has an offset/index of 2 etc.
9th Oct 2018, 1:19 PM
Anna
Anna - avatar
+ 1
Now I got it.. I ran a. example on this.... Thanks a lot. You gave an tremendous effort.. Thanks a lot for that.. Really appreciate it.!!
9th Oct 2018, 1:30 PM
Vedant Roy
Vedant Roy - avatar
+ 1
You're welcome 😀
9th Oct 2018, 1:34 PM
Anna
Anna - avatar
0
Nope! This is a coding convention in almost every coding languages it works like this. You may find it useful after a bit. I think in very old programming languages like Basic you could choose it but it's not very useful actually
9th Oct 2018, 12:33 PM
Daniele Bonomi
Daniele Bonomi - avatar
0
I didn't know that. Thanks for the information.!!
9th Oct 2018, 12:35 PM
Vedant Roy
Vedant Roy - avatar
0
I am confused. Can you please elaborate?
9th Oct 2018, 1:03 PM
Vedant Roy
Vedant Roy - avatar