Create a two-dimensional array at runtime: Difference between revisions

Add CLU
(Add CLU)
Line 945:
(aset a 0 0 12)
(println "Element at 0,0:" (aget a 0 0)))</lang>
 
=={{header|CLU}}==
<lang clu>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</lang>
{{out}}
<pre>Width? 8
Height? 6
arr[5][4] = 123</pre>
 
=={{header|Common Lisp}}==
2,095

edits