Do you know why the C++ array is indexed from zero and not one?
Suppose we have an Array A where the size of data type is W (2 or 4 bytes for int depends on the compiler) then the logical address of element at ith index is calculated as:-
Logical Address(A[i]) = Base Address(L0) + ( i - 1 ) * W
in this formulae, we have to perform 3 arithmetic operations +, -, and * but when we take the zero as the starting index then the formula becomes:-
Logical Address(A[i]) = Base Address(L0) + ( i ) * W
which have only 2 arithmetic operations + and *
so if the size of an array is n then the formula with starting index one will execute slower due to one extra operation overhead. thus, affecting the time taken by the program for larger values of n.
The above discussion was for the 1-D array but when it comes to 2-D or multidimensional arrays the case is even worse, suppose i and j are respectively the ith row and jth column in a 2-D matrix then the address when considered 0 as the first index is given as:-
Logical Address(A[i][j]) = Base Address(L0) + ( i* n +j ) * W
having 4 arithmetic operations, but when taken ONE as the starting index we have
Logical Address(A[i][j]) = Base Address(L0) + ( (i-1)* n +(j-1)) * W
this is having 6 arithmetic operations so it keeps on increasing as the dimension of the array increases too.
And hence, we use ZERO as the Starting index rather than ONE.