Are there other common "c-like" or non "c-like" languages with non zero index array position?
ArraysProgramming LanguagesIndexingArrays Problem Overview
C programming language is known as a zero index array language. The first item in an array is accessible using 0
. For example double arr[2] = {1.5,2.5}
The first item in array arr
is at position 0. arr[0] === 1.5
What programming languages are 1 based indexes?
I've heard of the these languages start at 1 instead of 0 for array access: Algol, Matlab, Action!, Pascal, Fortran, Cobol. Is this complete?
Specificially, a 1 based array would access the first item with 1, not zero.
Arrays Solutions
Solution 1 - Arrays
A list can be found on wikipedia.
ALGOL 68
APL
AWK
CFML
COBOL
Fortran
FoxPro
Julia
Lua
Mathematica
MATLAB
PL/I
Ring
RPG
Sass
Smalltalk
Wolfram Language
XPath/XQuery
Solution 2 - Arrays
Fortran starts at 1. I know that because my Dad used to program Fortran before I was born (I am 33 now) and he really criticizes modern programming languages for starting at 0, saying it's unnatural, not how humans think, unlike maths, and so on.
However, I find things starting at 0 quite natural; my first real programming language was C and *(ptr+n) wouldn't have worked so nicely if n hadn't started at zero!
Solution 3 - Arrays
A pretty big list of languages is on Wikipedia under Comparison of Programming Languages (array) under "Array system cross-reference list" table (Default base index column)
This has a good discussion of 1- vs. 0- indexed and subscriptions in general
To quote from the blog:
> EWD831 by E.W. Dijkstra, 1982. > > When dealing with a sequence of length N, the elements of which we > wish to distinguish by subscript, the > next vexing question is what subscript > value to assign to its starting > element. Adhering to convention a) > yields, when starting with subscript > 1, the subscript range 1 ≤ i < N+1; > starting with 0, however, gives the > nicer range 0 ≤ i < N. So let us let > our ordinals start at zero: an > element's ordinal (subscript) equals > the number of elements preceding it in > the sequence. And the moral of the > story is that we had better regard > —after all those centuries!— zero as a > most natural number. > > Remark:: Many programming languages have been designed without due > attention to this detail. In FORTRAN > subscripts always start at 1; in ALGOL > 60 and in PASCAL, convention c) has > been adopted; the more recent SASL has > fallen back on the FORTRAN convention: > a sequence in SASL is at the same time > a function on the positive integers. > Pity! (End of Remark.)
Solution 4 - Arrays
Fortran, Matlab, Pascal, Algol, Smalltalk, and many many others.
Solution 5 - Arrays
You can do it in Perl
$[ = 1; # set the base array index to 1
You can also make it start with 42 if you feel like that. This also affects string indexes.
Actually using this feature is highly discouraged.
Solution 6 - Arrays
Also in Ada you can define your array indices as required:
A : array(-5..5) of Integer; -- defines an array with 11 elements
B : array(-1..1, -1..1) of Float; -- defines a 3x3 matrix
Someone might argue that user-defined array index ranges will lead to maintenance problems. However, it is normal to write Ada code in a way which does not depend on the array indices. For this purpose, the language provides element attributes, which are automatically defined for all defined types:
A'first -- this has the value -5
A'last -- this has the value +5
A'range -- returns the range -5..+5 which can be used e.g. in for loops
Solution 7 - Arrays
JDBC (not a language, but an API)
String x = resultSet.getString(1); // the first column
Solution 8 - Arrays
Solution 9 - Arrays
Lua - disappointingly
Solution 10 - Arrays
Found one - Lua (programming language)
Check Arrays section which says -
"Lua arrays are 1-based: the first index is 1 rather than 0 as it is for many other programming languages (though an explicit index of 0 is allowed)"
Solution 11 - Arrays
VB Classic, at least through
Option Base 1
Solution 12 - Arrays
Strings in Delphi start at 1.
(Static arrays must have lower bound specified explicitly. Dynamic arrays always start at 0.)
Solution 13 - Arrays
ColdFusion - even though it is Java under the hood
Solution 14 - Arrays
Ada and Pascal.
Solution 15 - Arrays
PL/SQL. An upshot of this is when using languages that start from 0 and interacting with Oracle you need to handle the 0-1 conversions yourself for array access by index. In practice if you use a construct like foreach
over rows or access columns by name, it's not much of an issue, but you might want the leftmost column, for example, which will be column 1.
Solution 16 - Arrays
Indexes start at one in CFML.
Solution 17 - Arrays
The entire Wirthian line of languages including Pascal, Object Pascal, Modula-2, Modula-3, Oberon, Oberon-2 and Ada (plus a few others I've probably overlooked) allow arrays to be indexed from whatever point you like including, obviously, 1.
Erlang indexes tuples and arrays from 1.
I think—but am no longer positive—that Algol and PL/1 both index from 1. I'm also pretty sure that Cobol indexes from 1.
Basically most high level programming languages before C indexed from 1 (with assembly languages being a notable exception for obvious reasons – and the reason C indexes from 0) and many languages from outside of the C-dominated hegemony still do so to this day.
Solution 18 - Arrays
There is also Smalltalk
Solution 19 - Arrays
Visual FoxPro, FoxPro and Clipper all use arrays where element 1 is the first element of an array... I assume that is what you mean by 1-indexed.
Solution 20 - Arrays
I see that the knowledge of fortran here is still on the '66 version.
Fortran has variable both the lower and the upper bounds of an array.
Meaning, if you declare an array like:
real, dimension (90) :: x
then 1 will be the lower bound (by default).
If you declare it like
real, dimension(0,89) :: x
then however, it will have a lower bound of 0.
If on the other hand you declare it like
real, allocatable :: x(:,:)
then you can allocate it to whatever you like. For example
allocate(x(0:np,0:np))
means the array will have the elements
x(0, 0), x(0, 1), x(0, 2 .... np)
x(1, 0), x(1, 1), ...
.
.
.
x(np, 0) ...
There are also some more interesting combinations possible:
real, dimension(:, :, 0:) :: d
real, dimension(9, 0:99, -99:99) :: iii
which are left as homework for the interested reader :)
These are just the ones I remembered off the top of my head. Since one of fortran's main strengths are array handling capabilities, it is clear that there are lot of other in&outs not mentioned here.
Solution 21 - Arrays
Nobody mentioned XPath.
Solution 22 - Arrays
Mathematica and Maxima, besides other languages already mentioned.
Solution 23 - Arrays
informix, besides other languages already mentioned.
Solution 24 - Arrays
Basic - not just VB, but all the old 1980s era line numbered versions.
Richard
Solution 25 - Arrays
FoxPro used arrays starting at index 1.
Solution 26 - Arrays
dBASE used arrays starting at index 1.
Solution 27 - Arrays
RPG, including modern RPGLE
Solution 28 - Arrays
Although C is by design 0 indexed, it is possible to arrange for an array in C to be accessed as if it were 1 (or any other value) indexed. Not something you would expect a normal C coder to do often, but it sometimes helps.
Example:
#include <stdio.h>
int main(){
int zero_based[10];
int* one_based;
int i;
one_based=zero_based-1;
for (i=1;i<=10;i++) one_based[i]=i;
for(i=10;i>=1;i--) printf("one_based[%d] = %d\n", i, one_based[i]);
return 0;
}