Array Declarations in Various Programming Langauges

In many programming languages, the elemens of an array of size N are numbered 0through N-1 rather than 1 through N, and this is the convention that we follow in CMIS 102. For example, in Java or C one would write

        int cat[20];
        char dog[12];

    the 20 elements of cat are cat[0] through cat[19]
    the 12 elements of dog are dog[0] through dog[11]

Every programming language supports arrays. But the ways in which they do so vary widely. The table below compares how integer arrays are declared in some well-known programming languages:

Language Typical Array Declaration Valid Subscripts
FORTRAN INTEGER MYARRAY(10) 1 through 10
BASIC (most dialects) Dim myArray(10) As Integer
(Notice that the array contains 11 elements, not 10!)
0 through 10
Visual BASIC 4.0 Dim myArray(3 to 11) As Integer 3 through 11
Pascal var myArray: array[0..10] of integer 0 through 10
Pascal var myOtherArray: array[3..8] of integer 3 through 8
C, C++ int myArray[10]; 0 through 9
Java int myArray[] = new int[10]; 0 through 9

Note the variety. FORTRAN arrays start with index 1; FORTRAN arrays are said to be 1-based. C, C++, and Java arrays start with index 0; these arrays are said to be 0-based. In most dialects of BASIC, arrays are 0-based and the number you write in parentheses is the highest index allowed; so if you write N in the parentheses, the array contains N + 1 elements. In Pascal and in Visual BASIC 4.0, you can base your arrays any way you like! In C, C++, and Java, the number you write is the number of elements; so if you write N in parentheses, the highest index allowed in N - 1. Finally, some languages use parentheses whereas others use brackets.

You DO NOT need to know any of these details. But you should be aware that there is great variation from one language to another (and in the case of BASIC, from one dialect to another).