Create a two-dimensional array at runtime: Difference between revisions
Content added Content deleted
PatGarrett (talk | contribs) (→{{header|ALGOL 60}}: Section added) |
(Added Wren) |
||
Line 2,568: | Line 2,568: | ||
echo array[rows - 1][cols - 1] |
echo array[rows - 1][cols - 1] |
||
unlet array</lang> |
unlet array</lang> |
||
=={{header|Wren}}== |
|||
<lang ecmascript>import "io" for Stdin, Stdout |
|||
var x |
|||
var y |
|||
System.print("Enter the dimensions of the array:") |
|||
while (true) { |
|||
System.write(" First dimension : ") |
|||
Stdout.flush() |
|||
x = Num.fromString(Stdin.readLine()) |
|||
if (x && (x is Num) && (x.isInteger) && (x > 0) ) { |
|||
System.write(" Second dimension : ") |
|||
Stdout.flush() |
|||
y = Num.fromString(Stdin.readLine()) |
|||
if (y && (y is Num) && (y.isInteger) && (y > 0) ) break |
|||
System.print("Dimension must be a positive integer.") |
|||
} else { |
|||
System.print("Dimension must be a positive integer.") |
|||
} |
|||
} |
|||
// create the 2d array |
|||
var a = List.filled(x, [0] * y) |
|||
// write an element |
|||
a[x - 1][y - 1] = 42 |
|||
// print it |
|||
System.print("\na[%(x-1)][%(y-1)] = %(a[x-1][y-1])") |
|||
// make the array eligible for garbage collection |
|||
a = null</lang> |
|||
{{out}} |
|||
Example session: |
|||
<pre> |
|||
Enter the dimensions of the array: |
|||
First dimension : 3 |
|||
Second dimension : 4 |
|||
a[2][3] = 42 |
|||
</pre> |
|||
=={{header|XPL0}}== |
=={{header|XPL0}}== |