- 1
Arrary starts at zero
As far as I know.. array starts at zero. so a[5] would be six places to store.. right?
9 ответов
+ 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].
+ 1
If you have the authority to edit it, please do so..
+ 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
+ 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.
+ 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.!!
+ 1
You're welcome 😀
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
0
I didn't know that. Thanks for the information.!!
0
I am confused. Can you please elaborate?