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

Content added Content deleted
No edit summary
m (mass lang tag)
Line 65: Line 65:
With language built-in facilities:
With language built-in facilities:


#include <iostream>
<lang cpp> #include <iostream>
#include <istream>
#include <istream>
#include <ostream>
#include <ostream>
Line 90: Line 90:
delete[] array;
delete[] array;
delete[] array_data;
delete[] array_data;
}
}</lang>


Using std::vector from the standard library:
Using std::vector from the standard library:


#include <iostream>
<lang cpp> #include <iostream>
#include <istream>
#include <istream>
#include <ostream>
#include <ostream>
Line 115: Line 115:
// the array is automatically freed at the end of main()
// the array is automatically freed at the end of main()
}
}</lang>


=={{header|D}}==
=={{header|D}}==
<pre>
<lang d>
import std.stdio: writef, writefln, readln;
import std.stdio: writef, writefln, readln;
import std.conv: toInt;
import std.conv: toInt;
Line 134: Line 134:
writefln("The number at place [0 0] is ", array[0][0]);
writefln("The number at place [0 0] is ", array[0][0]);
}
}
</pre>
</lang>


=={{header|Clean}}==
=={{header|Clean}}==
import StdEnv
<lang clean> import StdEnv
Start :: *World -> { {Real} }
Start :: *World -> { {Real} }
Line 144: Line 144:
(_, dim1, console) = freadi console
(_, dim1, console) = freadi console
(_, dim2, console) = freadi console
(_, dim2, console) = freadi console
= createArray dim1 (createArray dim2 1.0)
= createArray dim1 (createArray dim2 1.0)</lang>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 161: Line 161:


=={{header|Forth}}==
=={{header|Forth}}==
: cell-matrix
<lang forth> : cell-matrix
create ( width height "name" ) over , * cells allot
create ( width height "name" ) over , * cells allot
does> ( x y -- addr ) dup cell+ >r @ * + cells r> + ;
does> ( x y -- addr ) dup cell+ >r @ * + cells r> + ;
Line 168: Line 168:
36 0 0 test !
36 0 0 test !
0 0 test @ . \ 36
0 0 test @ . \ 36</lang>


{{libheader|Forth Scientific Library}}
{{libheader|Forth Scientific Library}}
<lang forth>
<pre><nowiki>
INTEGER DMATRIX my-matrix{{
INTEGER DMATRIX my-matrix{{
& my-matrix{{ 8 9 }}malloc
& my-matrix{{ 8 9 }}malloc
Line 179: Line 179:
& my-matrix{{ }}free
& my-matrix{{ }}free
</lang>
</nowiki></pre>


=={{header|Fortran}}==
=={{header|Fortran}}==
In Fortran 90 and later
In Fortran 90 and later
PROGRAM Example
<lang fortran> PROGRAM Example
IMPLICIT NONE
IMPLICIT NONE
Line 202: Line 202:
DEALLOCATE (array, STAT=errcheck)
DEALLOCATE (array, STAT=errcheck)
END PROGRAM Example
END PROGRAM Example</lang>


=={{header|Haskell}}==
=={{header|Haskell}}==


doit n m = a!(0,0) where a = array ((0,0),(n,m)) [((0,0),42)]
<lang haskell> doit n m = a!(0,0) where a = array ((0,0),(n,m)) [((0,0),42)]</lang>


=={{header|IDL}}==
=={{header|IDL}}==
The following is only for demonstration. No real program should just assume that the user input is valid, integer, large enough etc.
The following is only for demonstration. No real program should just assume that the user input is valid, integer, large enough etc.
read, x, prompt='Enter x size:'
<lang idl> read, x, prompt='Enter x size:'
read, y, prompt='Enter y size:'
read, y, prompt='Enter y size:'
d = fltarr(x,y)
d = fltarr(x,y)
Line 219: Line 219:
;==> outputs 5.6
;==> outputs 5.6
delvar, d
delvar, d</lang>


=={{header|J}}==
=={{header|J}}==


In J, all aspects of arrays are resolved through evaluation. Everything counts as being given at run time.
In J, all aspects of arrays are resolved through evaluation. Everything counts as being given at run time.
task=: 3 : 0
<lang j> task=: 3 : 0
'init new' =. 0;1 NB. values for initialization and alteration
'init new' =. 0;1 NB. values for initialization and alteration
array =. y $ init NB. create array of shape y
array =. y $ init NB. create array of shape y
Line 230: Line 230:
array =. new element } array NB. amend that element to new value
array =. new element } array NB. amend that element to new value
element { array NB. return value of changed element
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.
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:
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' =. ' ';'x' NB. literals
<lang j> 'init new' =. ' ';'x' NB. literals
'init new' =. 1r2;2r3 NB. fractions
'init new' =. 1r2;2r3 NB. fractions
'init new' =. a: ; <<'Rosetta' NB. boxes
'init new' =. a: ; <<'Rosetta' NB. boxes</lang>


=={{header|Java}}==
=={{header|Java}}==


import java.util.Scanner;
<lang java> import java.util.Scanner;
public class twoDimArray {
public class twoDimArray {
Line 253: Line 253:
System.out.println("The number at place [0 0] is " + array[0][0]);
System.out.println("The number at place [0 0] is " + array[0][0]);
}
}
}
}</lang>


=={{header|Logo}}==
=={{header|Logo}}==
{{works with|UCB Logo}}
{{works with|UCB Logo}}
make "a2 mdarray [5 5]
<lang logo> make "a2 mdarray [5 5]
mdsetitem [1 1] :a2 0 ; by default, arrays are indexed starting at 1
mdsetitem [1 1] :a2 0 ; by default, arrays are indexed starting at 1
print mditem [1 1] :a2 ; 0
print mditem [1 1] :a2 ; 0</lang>


=={{header|MAXScript}}==
=={{header|MAXScript}}==
Line 276: Line 276:
=={{header|OCaml}}==
=={{header|OCaml}}==


let nbr1 = read_int ();;
<lang ocaml> let nbr1 = read_int ();;
let nbr2 = read_int ();;
let nbr2 = read_int ();;
let array = Array.make_matrix nbr1 nbr2 0.0;;
let array = Array.make_matrix nbr1 nbr2 0.0;;
array.(0).(0) <- 3.5;;
array.(0).(0) <- 3.5;;
print_float array.(0).(0); print_newline ();;
print_float array.(0).(0); print_newline ();;</lang>


=={{header|Pascal}}==
=={{header|Pascal}}==
Line 287: Line 287:
The following code is standard Extended Pascal (tested with <tt>gpc --extended-pascal</tt>):
The following code is standard Extended Pascal (tested with <tt>gpc --extended-pascal</tt>):


program array2d(input, output);
<lang pascal> program array2d(input, output);
type
type
Line 312: Line 312:
{ get rid of array }
{ get rid of array }
dispose(data);
dispose(data);
end.
end.</lang>




Line 320: 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.
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($ $){
<lang perl> sub make_array($ $){
# get array sizes from provided params, but force numeric value
# get array sizes from provided params, but force numeric value
my $x = ($_[0] =~ /^\d+$/) ? shift : 0;
my $x = ($_[0] =~ /^\d+$/) ? shift : 0;
Line 338: Line 338:
print "\n";
print "\n";
}
}
}
}</lang>


=={{header|Pop11}}==
=={{header|Pop11}}==
vars itemrep;
<lang pop11> vars itemrep;
incharitem(charin) -> itemrep;
incharitem(charin) -> itemrep;
;;; Read sizes
;;; Read sizes
Line 352: Line 352:
ar(0,0) =>
ar(0,0) =>
;;; Make sure array is unreferenced
;;; Make sure array is unreferenced
0 -> ar;
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.
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: Line 377:


=={{header|Ruby}}==
=={{header|Ruby}}==
puts 'Enter width and height: '
<lang ruby> puts 'Enter width and height: '
w=gets.to_i
w=gets.to_i
arr = Array.new(gets.to_i){Array.new(w)}
arr = Array.new(gets.to_i){Array.new(w)}
arr[1][3] = 5
arr[1][3] = 5
p arr[1][3]
p arr[1][3]</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==
Line 399: 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.
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.


[ ( x y -- address )
<lang toka> [ ( x y -- address )
cells malloc >r
cells malloc >r
dup cells >r
dup cells >r
Line 412: Line 412:
[ ( value a b address -- )
[ ( value a b address -- )
array.get array.put
array.get array.put
] is 2D-put-element
] is 2D-put-element</lang>


And a short test:
And a short test:
5 5 2D-array >r #! Create an array and save the pointer to it
<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
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
2 3 r@ 2D-get-element #! Get the element at 2,3
r> drop #! Discard the pointer to the array
r> drop #! Discard the pointer to the array</lang>