Declaration | Referring an array element | Initialising the elements of an array in the array declaration |
The following are some examples of array declarations:
C++ version | Equivalent Pascal version |
int a[5], b[10]; | var a: array[0..4] of integer; b: array[0..9] of integer; |
The integer constant in the square brackets ([]) specifies the number of elements in the array. In C++, the subscript of an array must start with 0.
Referring an array element in C++ is the same as in Pascal: the array name, followed by a subscript (which is an integer expression) enclosed by square brackets. For example, "a[4]", "b[x + 1]" (assume that x is an integer variable).
In addition to initialising the elements of an array one by one, they can also be initialised in the array declaration by following the declaration with "=", followed by a comma-separated list of initialisers (the initialisation values) which is enclosed in braces. For example:
C++ version | Equivalent Pascal version |
int n[5] = {14, 23, 7, 10, 5}; | var n: array[0..4] of integer; ... n[0] := 14; n[1] := 23; n[2] := 7; n[3] := 10; n[4] := 5; |
The elements of an array must be initialised separately if the initialisation is not done in the array declaration.
If the number of elements in the array is more than the number of the initialisers, the remaining elements are automatically initialised to zero. For example:
C++ version | Equivalent Pascal version |
int n[5] = {14, 23}; | var n: array[0..4] of integer; ... n[0] := 14; n[1] := 23; n[2] := 0; n[3] := 0; n[4] := 0; |
If an array is not initialised in array declaration, the values stored in the array are unknown until initialisation of the elements are done separately.
There will be a compile-time error if the number of initialisers is more than the number of the elements in the array.
If the array size is not specified in the array declaration, there should be an
initialiser list. The number of elements in the array will be the numbers of elements in
the initialiser list. For example, the statement
int x[] = {4, 7, 10, 8, 5};
specifies that there are 5 elements in the array (range of subscripts is 0 to 4).
The following sample program uses a one-dimensional array to store odd numbers:
C++ version | Pascal version |
#include <iostream.h> #define MAX 10 main() { int a[MAX], i; for (i = 0; i < MAX; i++) a[i] = 2 * i + 1; cout << "Subscript? (-ve value to quit) "; cin >> i; while (i >= 0) { if (i >= MAX) cout << "Subscript out of range!" << endl; else cout << "a[" << i << "]: " << a[i] << endl; cout << "Subscript? (-ve value to quit) "; cin >> i; } return 0; } |
program prog_24(input, output); const MAX = 10; var a: array[0..MAX - 1] of integer; i: integer; begin for i := 0 to MAX - 1 do a[i] := 2 * i + 1; write('Subscript? (-ve value to quit) '); readln(i); while i >= 0 do begin if i >= MAX then writeln('Subscript out of range!') else writeln('a[', i, ']: ', a[i]); write('Subscript? (-ve value to quit) '); readln(i) end end. |
Sample output (text in red is entered by user): | |
Subscript? (-ve value to quit) 5 a[5]: 11 Subscript? (-ve value to quit) 9 a[9]: 19 Subscript? (-ve value to quit) 10 Subscript out of range! Subscript? (-ve value to quit) 0 a[0]: 1 Subscript? (-ve value to quit) -2 |