Index of array start at 0 problem 05-26-2014, 12:41 PM
#1
Due to my country education system, I had used Pascal for programming all the time. After changing to other language, everything work like a charm but one exception (it's been following me from years):
The array in Python, C, Java,... always start its index at 0.
In Pascal, you can design where the index should start, and in many case, I used to declare it at 1.
For example: the problem require finding the shortest path from City[1->5], the array is prototype as City[1..n] = [1,2,3,4,5]. In this case, it became pretty easy to imply the algorithm because the value i is also the value of City[i] (0<i<6).
In other language, since index of array start at 0, I must declare the array City[0->5] = [0,1,2,3,4,5] and abandon City[0] through all the program, City[0] became garbage and cost system memory.
Another way is prototype City[0->4] = [1,2,3,4,5]. This won't cost any garbage memory, but it is more difficult to imply the algorithm since City[i] = i+1.
The question is, which way is better to use? Which way will you choose? Is there any other way to solve this problem?
The array in Python, C, Java,... always start its index at 0.
In Pascal, you can design where the index should start, and in many case, I used to declare it at 1.
For example: the problem require finding the shortest path from City[1->5], the array is prototype as City[1..n] = [1,2,3,4,5]. In this case, it became pretty easy to imply the algorithm because the value i is also the value of City[i] (0<i<6).
In other language, since index of array start at 0, I must declare the array City[0->5] = [0,1,2,3,4,5] and abandon City[0] through all the program, City[0] became garbage and cost system memory.
Another way is prototype City[0->4] = [1,2,3,4,5]. This won't cost any garbage memory, but it is more difficult to imply the algorithm since City[i] = i+1.
The question is, which way is better to use? Which way will you choose? Is there any other way to solve this problem?
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)