Skip to content

SpecificationIII · Types and values

3.4

Arrays

An array is a row (or a table) of boxes of the same type, with a fixed size and numbered from zero.

Markdown version

3.4.1 How to write it

Dimension «name»[«size»] As «type»
Dimension «name»[«rows», «columns»] As «type»
Dimension «name»[«size»], «name»[«size»] As «type»
«name»[«index»] <- «value»
  1. Dimensionword that creates the arrays
  2. notas[4]name and size in brackets
  3. ,separates another array on the same line
  4. tabla[2, 3]two sizes: rows and columns
  5. As Integertype of the boxes, for every array on the line

3.4.2 How it works

Dimension 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.

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.

ProgramThe 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

Output

Media: 7.5

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:

ProgramA 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

Output

3 6

The program finishes normally.

A single statement can create several arrays separated by commas. The As at the end applies to all of them:

ProgramTwo 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

Output

24

The program finishes normally.

3.4.3 Dimensioning in two steps

Classic 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 value

A 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.

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.