Create a two-dimensional array at runtime: Difference between revisions
Content added Content deleted
No edit summary |
m (mass lang tag) |
||
Line 65:
With language built-in facilities:
#include <istream>
#include <ostream>
Line 90:
delete[] array;
delete[] array_data;
}</lang>
Using std::vector from the standard library:
#include <istream>
#include <ostream>
Line 115:
// the array is automatically freed at the end of main()
}</lang>
=={{header|D}}==
<
import std.stdio: writef, writefln, readln;
import std.conv: toInt;
Line 134:
writefln("The number at place [0 0] is ", array[0][0]);
}
</
=={{header|Clean}}==
Start :: *World -> { {Real} }
Line 144:
(_, dim1, console) = freadi console
(_, dim2, console) = freadi console
= createArray dim1 (createArray dim2 1.0)</lang>
=={{header|Common Lisp}}==
Line 161:
=={{header|Forth}}==
create ( width height "name" ) over , * cells allot
does> ( x y -- addr ) dup cell+ >r @ * + cells r> + ;
Line 168:
36 0 0 test !
0 0 test @ . \ 36</lang>
{{libheader|Forth Scientific Library}}
<lang forth>
INTEGER DMATRIX my-matrix{{
& my-matrix{{ 8 9 }}malloc
Line 179:
& my-matrix{{ }}free
</lang>
=={{header|Fortran}}==
In Fortran 90 and later
IMPLICIT NONE
Line 202:
DEALLOCATE (array, STAT=errcheck)
END PROGRAM Example</lang>
=={{header|Haskell}}==
=={{header|IDL}}==
The following is only for demonstration. No real program should just assume that the user input is valid, integer, large enough etc.
read, y, prompt='Enter y size:'
d = fltarr(x,y)
Line 219:
;==> outputs 5.6
delvar, d</lang>
=={{header|J}}==
In J, all aspects of arrays are resolved through evaluation. Everything counts as being given at run time.
'init new' =. 0;1 NB. values for initialization and alteration
array =. y $ init NB. create array of shape y
Line 230:
array =. new element } array NB. amend that element to new value
element { array NB. return value of changed element
)</lang>
Passing two integers to <tt>task</tt> (as a list) satisfies the specifications for a two-dimensional array, but providing a longer list of integers accomplishes the same task on an array of as many dimensions as the count of integers given.
The type of the array is determined by the type of the values used in filling the array. E.g., alternate data types are obtained by substituting any of the following lines:
'init new' =. 1r2;2r3 NB. fractions
'init new' =. a: ; <<'Rosetta' NB. boxes</lang>
=={{header|Java}}==
public class twoDimArray {
Line 253:
System.out.println("The number at place [0 0] is " + array[0][0]);
}
}</lang>
=={{header|Logo}}==
{{works with|UCB Logo}}
mdsetitem [1 1] :a2 0 ; by default, arrays are indexed starting at 1
print mditem [1 1] :a2 ; 0</lang>
=={{header|MAXScript}}==
Line 276:
=={{header|OCaml}}==
let nbr2 = read_int ();;
let array = Array.make_matrix nbr1 nbr2 0.0;;
array.(0).(0) <- 3.5;;
print_float array.(0).(0); print_newline ();;</lang>
=={{header|Pascal}}==
Line 287:
The following code is standard Extended Pascal (tested with <tt>gpc --extended-pascal</tt>):
type
Line 312:
{ get rid of array }
dispose(data);
end.</lang>
Line 320:
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.
# get array sizes from provided params, but force numeric value
my $x = ($_[0] =~ /^\d+$/) ? shift : 0;
Line 338:
print "\n";
}
}</lang>
=={{header|Pop11}}==
incharitem(charin) -> itemrep;
;;; Read sizes
Line 352:
ar(0,0) =>
;;; Make sure array is unreferenced
0 -> ar;</lang>
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.
Line 377:
=={{header|Ruby}}==
<lang ruby> puts 'Enter width and height: '
w=gets.to_i
arr = Array.new(gets.to_i){Array.new(w)}
arr[1][3] = 5
p arr[1][3]</lang>
=={{header|Tcl}}==
Line 399:
Toka has no direct support for 2D arrays, but they can be created and operated on in a manner similar to normal arrays using the following functions.
cells malloc >r
dup cells >r
Line 412:
[ ( value a b address -- )
array.get array.put
] is 2D-put-element</lang>
And a short test:
10 2 3 r@ 2D-put-element #! Set element 2,3 to 10
2 3 r@ 2D-get-element #! Get the element at 2,3
r> drop #! Discard the pointer to the array</lang>
|