Why Start at Index 0 instead of Index 1?

◀ Multidimensional Array via new Operator▶ The Standard Template Library
Amazon Although an array starts at index 0, sometimes starting at 1 is a lot easier for programmers to visualize the running of the program in mind.

For example, I wrote a program that creates a maze and each spot in the maze has its own attributes such as whether its right wall is down and what its neighbors are. It is highly desirable that the first spot in the maze is represented as index 1 and identifier 1 and the second spot index 2 and identifier 2, and so on. Doing so eliminates confusion which often results in bugs.

Whenever I want to traverse all the spots in the maze, the index in the for loop starts at 1. Whenever I want to access a spot with a particular identifier, I can be rest assured that the spot can be accessed via the same index as the identifier. Another benefit is the number of spots equals the maximum index of the array.

If you start at index 0 then throughout the program you'll need to do subtraction and addition where necessary which can be error prone.
An oyster makes pearls when an irritant gets stuck between its mantle and shell, so at a pearl’s center lies the foreign substance.
◀ Multidimensional Array via new Operator▶ The Standard Template Library

fShare
Questions? Let me know!