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

Content added Content deleted
(→‎{{header|J}}: Amplified dynamic qualities.)
m (Spelling/grammar/aesthetics)
Line 1: Line 1:
{{task}}
{{task}}
{{data structure}}
{{data structure}}
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 oputput that element. Finally destroy the array if not done by the language itself.
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.


=={{header|Ada}}==
=={{header|Ada}}==
Line 121: Line 121:


=={{header|IDL}}==
=={{header|IDL}}==
The following is only for demonstration. No real program should
The following is only for demonstration. No real program should just assume that the user input is valid, integer, large enough etc.
just assume that the user input is valid, integer, large enough etc.
read, x, prompt='Enter x size:'
read, x, prompt='Enter x size:'
Line 185: Line 184:
'''Interpreter:''' [[Perl]] 5.x
'''Interpreter:''' [[Perl]] 5.x


Predefining an array (or multi-dimension array) size is unnecessary, Perl dynamically resizes the array to meet the requirements. Of course I'm assuming that the user is entering array size 0 based.
Predefining an array (or multi-dimension array) size is unnecessary, Perl dynamically resizes the array to meet the requirements. Of course I'm assuming that the user is entering array size 0 based.


sub make_array($ $){
sub make_array($ $){
Line 196: Line 195:
$array[0][0] = 'X '; # first by first element
$array[0][0] = 'X '; # first by first element
$array[5][7] = 'X ' if (5 <= $y and 7 <= $x); # sixth by eighth element, if the max size is big enough
$array[5][7] = 'X ' if (5 <= $y and 7 <= $x); # sixth by eighth element, if the max size is big enough
$array[12][15] = 'X ' if (12 <= $y and 15 <= $x); # thirteeth by sixteenth element, if the max size is big enough
$array[12][15] = 'X ' if (12 <= $y and 15 <= $x); # thirteenth by sixteenth element, if the max size is big enough
# loop through the elements expected to exist base on input, and display the elements contents in a grid
# loop through the elements expected to exist base on input, and display the elements contents in a grid
Line 221: Line 220:
0 -> ar;
0 -> ar;


Pop11 is garbage collected so there is no need to destroy array. However,
Pop11 is garbage collected so there is no need to destroy array. However, the array is live as long as variable ar references it. The last assignment makes sure that we loose all our references to the array turning it into garbage.
the array is live as long as variable ar references it. The last
assignment makes sure that we loose all our references to the array
turning it into garbage.


Pop11 arrays may have arbitrary lower bounds, since we are given only
Pop11 arrays may have arbitrary lower bounds, since we are given only size we create 0 based array.
size we create 0 based array.


=={{header|Python}}==
=={{header|Python}}==