Anonymous user
Create a two-dimensional array at runtime: Difference between revisions
Create a two-dimensional array at runtime (view source)
Revision as of 07:34, 1 February 2017
, 7 years ago→{{header|zkl}}: re-write
(→{{header|zkl}}: re-write) |
|||
Line 2,318:
zkl doesn't have a native array type but lists of lists can do the same thing. It is garbage collected so things are cleaned up implicitly.
This code creates a 2d row major zero filled array.
Since the array is row major, sub-scripting works as expected: [row][col]
▲Since the array is row major, sub-scripting works as expected: [row][col]. [row,length] is the other option.
<lang zkl>rows:=ask("Rows: ").toInt();
cols:=ask("columns: ").toInt();
array[1][2]=123;
array[1][2].println();</lang>
The createLong method pre-allocates a list, optionally filled with a constant or computation.
{{out}}
<pre>
Rows: 3
columns: 4
123
</pre>
If you want Matrix/linear algebra, you can use the GNU Scientific Library:
<lang zkl>var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library)
rows:=ask("Rows: ").toInt();
cols:=ask("columns: ").toInt();
m:=GSL.Matrix(rows,cols);
m[1,2]=3;
m.format().println();
println(m[1,2]);</lang>
Again, garbage collected.
{{out}}
<pre>
Rows: 3
columns: 4
0.00, 0.00, 0.00, 0.00
0.00, 0.00, 3.00, 0.00
▲L(L(0,0,0,0),L(0,0,0,0),L(0,0,0,0))
0.00, 0.00, 0.00, 0.00
3
</pre>
|