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:
 
<lang cpp> #include <iostream>
#include <istream>
#include <ostream>
Line 90:
delete[] array;
delete[] array_data;
}</lang>
 
Using std::vector from the standard library:
 
<lang cpp> #include <iostream>
#include <istream>
#include <ostream>
Line 115:
// the array is automatically freed at the end of main()
}</lang>
 
=={{header|D}}==
<prelang d>
import std.stdio: writef, writefln, readln;
import std.conv: toInt;
Line 134:
writefln("The number at place [0 0] is ", array[0][0]);
}
</prelang>
 
=={{header|Clean}}==
<lang clean> import StdEnv
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}}==
<lang forth> : cell-matrix
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>
<pre><nowiki>
INTEGER DMATRIX my-matrix{{
& my-matrix{{ 8 9 }}malloc
Line 179:
& my-matrix{{ }}free
</lang>
</nowiki></pre>
 
=={{header|Fortran}}==
In Fortran 90 and later
<lang fortran> PROGRAM Example
IMPLICIT NONE
Line 202:
DEALLOCATE (array, STAT=errcheck)
END PROGRAM Example</lang>
 
=={{header|Haskell}}==
 
<lang haskell> doit n m = a!(0,0) where a = array ((0,0),(n,m)) [((0,0),42)]</lang>
 
=={{header|IDL}}==
The following is only for demonstration. No real program should just assume that the user input is valid, integer, large enough etc.
<lang idl> read, x, prompt='Enter x size:'
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.
<lang j> task=: 3 : 0
'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:
<lang j> 'init new' =. ' ';'x' NB. literals
'init new' =. 1r2;2r3 NB. fractions
'init new' =. a: ; <<'Rosetta' NB. boxes</lang>
 
=={{header|Java}}==
 
<lang java> import java.util.Scanner;
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}}
<lang logo> make "a2 mdarray [5 5]
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}}==
 
<lang ocaml> let nbr1 = read_int ();;
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>):
 
<lang pascal> program array2d(input, output);
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.
 
<lang perl> sub make_array($ $){
# get array sizes from provided params, but force numeric value
my $x = ($_[0] =~ /^\d+$/) ? shift : 0;
Line 338:
print "\n";
}
}</lang>
 
=={{header|Pop11}}==
<lang pop11> vars itemrep;
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.
 
<lang toka> [ ( x y -- address )
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:
<lang toka> 5 5 2D-array >r #! Create an array and save the pointer to it
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>