In one sentence
An array is a row (or a table) of boxes of the same type, with a fixed size and numbered from zero.
3.4.1 How to write itDimension «name» [ «size» ] As «type»
Dimension «name» [ «rows» , «columns» ] As «type»
Dimension «name» [ «size» ] , «name» [ «size» ] As «type»
«name» [ «index» ] <- «value» Dimension word that creates the arrays notas [ 4 ] name and size in brackets , separates another array on the same line tabla [ 2 , 3 ] two sizes: rows and columns As Integer type of the boxes, for every array on the line 3.4.2 How it worksDimension creates the array and its boxes at once. Each box is used like a variable: it is assigned a value and read with the name and the index in brackets. Indices start at zero : an array of size 3 has boxes 0, 1 and 2.
The boxes of an array Four boxes in a row with the values 7, 9, 6 and 8 and the indices 0, 1, 2 and 3 below. On the right, a fifth dashed, crossed-out box with index 4, which does not exist. notas 7 9 6 8 ✕ [0] [1] [2] [3] [4] Size 4: indices 0 to 3 notas[4] → arrayIndexOutOfRange
Figure 3.4-1. An array of size 4 has boxes 0 to 3. Box 4 does not exist.Four boxes in a row with the values 7, 9, 6 and 8 and the indices 0, 1, 2 and 3 below. On the right, a fifth dashed, crossed-out box with index 4, which does not exist.
Program The week's grades Algorithm Notas
Dimension notas [ 4 ] As Integer ;
notas [ 0 ] <- 7 ;
notas [ 1 ] <- 9 ;
notas [ 2 ] <- 6 ;
notas [ 3 ] <- 8 ;
Define suma As Integer <- 0 ;
For i <- 0 To 3 Do
suma <- suma + notas [ i ] ;
EndFor
Write "Media: " , suma / 4 ;
EndAlgorithm The program finishes normally.
An array can have several dimensions . A table with 2 rows and 3 columns is declared with two sizes and each box is named with two indices inside the same brackets:
Program A small multiplication table Algorithm Tabla
Dimension tabla [ 2 , 3 ] As Integer ;
For f <- 0 To 1 Do
For c <- 0 To 2 Do
tabla [ f , c ] <- ( f + 1 ) * ( c + 1 ) ;
EndFor
EndFor
Write tabla [ 0 , 2 ] , " " , tabla [ 1 , 2 ] ;
EndAlgorithm The program finishes normally.
A single statement can create several arrays separated by commas. The As at the end applies to all of them:
Program Two arrays on one line Algorithm Varios
Dimension precios [ 3 ] , cantidades [ 3 ] As Integer ;
precios [ 0 ] <- 12 ;
cantidades [ 0 ] <- 2 ;
Write precios [ 0 ] * cantidades [ 0 ] ;
EndAlgorithm The program finishes normally.
3.4.3 Dimensioning in two stepsClassic pseudocode first declares the variable with Define and then dimensions it with Dimension without repeating the type. PseudoLearn accepts this if the variable is not used between the two statements.
3.4.4 An array is not a valueA whole array cannot be assigned, compared, written or returned. Only its boxes are values. To pass it whole to a subroutine an array parameter is used, which shares the boxes.
Limit
Indices: integers from 0 to size − 1 in each dimension. Size 0: valid (an array with no boxes). Negative size: negativeArraySize . Sizes are computed when running, so they can be expressions. There is no maximum fixed by the language; the limit is the memory available to the engine. Common mistakes
Going outside the array stops the program at that access:
Program The box that does not exist Algorithm Fuera
Dimension v [ 3 ] As Integer ;
v [ 3 ] <- 1 ;
EndAlgorithm Diagnostics
arrayIndexOutOfRange · error · 3:3 · Index 3 is out of range for an array of size 3The program finishes with an error.
Using a number of indices different from the number of dimensions is detected before running:
Program One index short Algorithm Indices
Dimension m [ 2 , 2 ] As Integer ;
m [ 0 ] <- 1 ;
EndAlgorithm Diagnostics
arrayDimensionCountMismatch · error · 3:3 · Incorrect number of indices for array mThe program finishes with an error.
And a whole array cannot be written:
Program Writing the whole array Algorithm EscribirArreglo
Dimension v [ 2 ] As Integer ;
v [ 0 ] <- 1 ;
v [ 1 ] <- 2 ;
Write v ;
EndAlgorithm Diagnostics
arrayCannotBeUsedAsValue · error · 5:9 · An array cannot be used as a scalar value in an expression or assignmentThe program finishes with an error.
Under the hood
In the intermediate representation, accessing a box is an instruction that checks the index before reading or writing. If the index is out of range, the instruction produces the trap arrayIndexOutOfRange with the exact position of the access.
Intermediate representation
module Tabla en
func suspendible @main() -> unit {
b0() mass 2:
stmt.enter @2:3-2:35
%0 = const.i64 2 @2:19-2:20
%1 = const.i64 3 @2:22-2:23
%2 = array.new array<i64, 2>, %0, %1 @2:3-2:35
stmt.exit @2:3-2:35
stmt.enter @3:3-7:9
%3 = const.i64 0 @3:12-3:13
%4 = const.i64 1 @3:17-3:18
%5 = const.i64 0
%6 = int.add %4, %5 @3:3-7:9
%7 = const.i64 1 @3:3-7:9
%8 = const.i64 0
%9 = int.add %7, %8 @3:3-7:9
loop.check_step %9 @3:3-7:9
jump b1(%3)
b1(%10: i64) mass 0:
%11 = const.i64 0
%12 = int.cmp ge, %9, %11
branch %12, b2(), b3()
b2() mass 0:
%13 = int.cmp le, %10, %6 @3:3-7:9
jump b4(%13)
b3() mass 0:
%14 = int.cmp ge, %10, %6 @3:3-7:9
jump b4(%14)
b4(%15: bool) mass 0:
decide.bool %15 @3:3-7:9
branch %15, b5(), b6()
b5() mass 1:
stmt.enter @4:5-6:11
%16 = const.i64 0 @4:14-4:15
%17 = const.i64 2 @4:19-4:20
%18 = const.i64 0
%19 = int.add %17, %18 @4:5-6:11
%20 = const.i64 1 @4:5-6:11
%21 = const.i64 0
%22 = int.add %20, %21 @4:5-6:11
loop.check_step %22 @4:5-6:11
jump b7(%16)
b6() mass 1:
stmt.exit @3:3-7:9
stmt.enter @8:3-8:38
%23 = const.i64 0 @8:15-8:16
%24 = const.i64 2 @8:18-8:19
%25 = array.load %2, %23, %24 @8:9-8:20
%26 = const.char ' ' @8:22-8:25
%27 = const.i64 1 @8:33-8:34
%28 = const.i64 2 @8:36-8:37
%29 = array.load %2, %27, %28 @8:27-8:38
out.write %25, %26, %29, newline @8:3-8:38
stmt.exit @8:3-8:38
return @9:1-9:13
b7(%30: i64) mass 0:
%31 = const.i64 0
%32 = int.cmp ge, %22, %31
branch %32, b8(), b9()
b8() mass 0:
%33 = int.cmp le, %30, %19 @4:5-6:11
jump b10(%33)
b9() mass 0:
%34 = int.cmp ge, %30, %19 @4:5-6:11
jump b10(%34)
b10(%35: bool) mass 0:
decide.bool %35 @4:5-6:11
branch %35, b11(), b12()
b11() mass 1:
stmt.enter @5:7-5:39
%36 = const.i64 1 @5:27-5:28
%37 = int.add %10, %36 @5:25-5:26
%38 = const.i64 1 @5:37-5:38
%39 = int.add %30, %38 @5:35-5:36
%40 = int.mul %37, %39 @5:30-5:31
array.store %2, %10, %30, %40 @5:7-5:39
stmt.exit @5:7-5:39
%41 = int.add %30, %22 @4:5-6:11
jump b13()
b12() mass 0:
stmt.exit @4:5-6:11
%42 = int.add %10, %9 @3:3-7:9
jump b14()
b13() mass 0:
task.yield_check @1:1-1:16
jump b7(%41) @1:1-1:16
b14() mass 0:
task.yield_check @1:1-1:16
jump b1(%42) @1:1-1:16
} Note
The classic mistake is going from 1 to N instead of 0 to N − 1. Write the notas loop with For i <- 1 To 4 and look at the failure it produces: the message says which index and which size.
In short
Dimension creates a fixed-size array of boxes of the same type.Indices go from 0 to size − 1; several indices go inside the same brackets. Going out of range is a runtime failure. A whole array is not a value: it is not assigned, compared or written. See also