
Data Structure

This illustrates a data structure, a means of storing data within a program.

You may see other such structures in the Data Structures category.
Get two integers from the user, then create a two-dimensional array where the two dimensions have the sizes given by those numbers, and which can be accessed in the most natural way possible. Write some element of that array, and then output that element. Finally destroy the array if not done by the language itself.

prompt = proc (s: string) returns (int)
    stream$puts(stream$primary_output(), s)
    return(int$parse(stream$getl(stream$primary_input())))
end prompt
 
start_up = proc ()
    po: stream := stream$primary_output()
 
    % Ask for width and height
    width: int := prompt("Width? ")
    height: int := prompt("Height? ")   
 
    % Create an array of arrays.
    % In order to actually create separate arrays, rather than repeating
    % a reference to the same array over and over, fill_copy must be used.
    arr: array[array[int]] :=
        array[array[int]]$fill_copy(1, width, array[int]$fill(1, height, 0))
 
    % Set a value
    x: int := 1+width/2
    y: int := 1+height/2
    arr[x][y] := 123
 
    % Retrieve the value
    stream$putl(po, "arr[" || int$unparse(x) || "][" || int$unparse(y)
                 || "] = " || int$unparse(arr[x][y]))
 
    % The array will be automatically garbage-collected once there
    % are no more references to it.
end start_up