Create a two-dimensional array at runtime: Difference between revisions
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 4:
=={{header|11l}}==
<
V height = 5
V myarray = [[0] * width] * height
print(myarray[height-1][width-1])</
=={{header|68000 Assembly}}==
The routine used to retrieve the input is left unimplemented.
<
; Array setup
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 96:
DBRA D7,loop_destroyArray ;loop using bottom 2 bytes of the array size as a loop counter
DBRA D6,loop_destroyArray ;decrement this, D7 is $FFFF each time execution gets here so this acts as a "carry" of sorts.
;if this value was 0 prior to the loop, the loop ends immediately.</
=={{header|Action!}}==
The user must type in the monitor the following command after compilation and before running the program!<pre>SET EndProg=*</pre>
{{libheader|Action! Tool Kit}}
<
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program!
Line 208:
TestGetValue(a,a.rows/2,a.cols/2)
TestDestroy(a)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Create_a_two-dimensional_array_at_runtime.png Screenshot from Atari 8-bit computer]
Line 221:
=={{header|Ada}}==
<
with Ada.Text_Io;
with Ada.Float_Text_Io;
Line 242:
end;
-- The variable Matrix is popped off the stack automatically
end Two_Dimensional_Arrays;</
{{omit from|Modula-2}}
=={{header|ALGOL 60}}==
<
comment Create a two-dimensional array at runtime - Algol 60;
integer n,m;
Line 258:
end;
comment array a : out of scope;
end </
{{in}}
<pre>
Line 271:
=={{header|ALGOL 68}}==
<
print("Input two positive whole numbers separated by space and press newline:");
[read int,read int] INT array;
array[1,1]:=42;
print (array[1,1])
)</
=={{header|ALGOL W}}==
<
integer dimension1UpperBound, dimension2UpperBound;
write( "upper bound for dimension 1: " );
Line 300:
end
end.</
=={{header|Amazing Hopper}}==
Amazing Hopper crea arrays de forma dinámica.
<syntaxhighlight lang="amazing hopper">
#include <flow.h>
#import lib/input.bas.lib
Line 325:
END
SUBRUTINES
</syntaxhighlight>
{{out}}
<pre>
Line 335:
Arrays are an integral part of APL. Array size, shape, and data type can be easily manipulated at runtime.
<
array[1;1]←73 ⍝ assign a value to location 1;1.
Line 342:
⎕ex 'array' ⍝ erase the array
</syntaxhighlight>
=={{header|AppleScript}}==
AppleScript has no array, but an AppleScript list can be used in a multidimensional fashion. There's no issue with their dimensions, they grow while adding elements. Memory allocation is dynamic.
<
set c to text returned of (display dialog "Enter number of columns:" default answer 2) as integer
set array to {}
Line 371:
-- Destroy array (typically unnecessary since it'll automatically be destroyed once script ends).
set array to {}
</syntaxhighlight>
To correct the last comment in the script above: when a script's run, the values of its properties, globals, and run-handler (''ie.'' top level) variables are saved back to the script file when the execution finishes. So variables containing bulky values like lists ideally ''should'' be set to something smaller before the end in order to prevent file bloat. Or local variables could be used, which would be the better option above as the <code>return</code> statement prevents the last line from being executed anyway.
Line 377:
=={{header|Arturo}}==
<
height: to :integer input "give me the array's height: "
Line 387:
arr\[x]\[y]: 123
print ["item at [" x "," y "] =" arr\[x]\[y]]</
{{out}}
Line 397:
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
<
InputBox, data,, Enter two integers separated by a Space:`n(ex. 5 7)
StringSplit, i, data, %A_Space%
Array[i1,i2] := "that element"
MsgBox, % "Array[" i1 "," i2 "] = " Array[i1,i2]</
=={{header|AutoIt}}==
<
$sInput = InputBox('2D Array Creation', 'Input comma separated count of rows and columns, i.e. "5,3"')
$aDimension = StringSplit($sInput, ',', 2)
Line 417:
MsgBox(0, 'Output', 'row[' & UBound($a2D) -1 & '], col[' & UBound($a2D, 2) -1 & ']' & @CRLF & '= ' & $a2D[ UBound($a2D) -1 ][ UBound($a2D, 2) -1 ] )
</syntaxhighlight>
=={{header|AWK}}==
AWK has no multidimensional array; but AWK arrays (which are [[Associative array]] indeed) can be used also in a multidimensional fashion. Since AWK arrays are associative arrays, there's no issue in their dimensions: they grow while adding new key-value pair.
<
for(i=0; i < $1; i++) {
for(j=0; j < $2; j++) {
Line 434:
print idx[1] "," idx[2] "->" arr[idx[1], idx[2]]
}
}</
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<
20 DIM A%(X% - 1, Y% - 1)
30 X% = RND(1) * X%
Line 445:
50 A%(X%, Y%) = -32767
60 PRINT A%(X%, Y%)
70 CLEAR</
==={{header|BASIC256}}===
<
input integer "Enter one positive integer: ", i
input integer "Enter other positive integer: ", j
Line 454:
a[i, j] = i * j
print "a("; string(i); ","; string(j); ") = "; a[i, j]
end</
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<
DIM array(1 TO i, 1 TO j)
array(i, j) = i * j
PRINT "a("; STR$(i); ","; STR$(j); " ) = "; array(i, j)
ERASE array</
==={{header|True BASIC}}===
<
DIM array(0,0)
MAT REDIM array(1 TO i, 1 TO j)
Line 472:
PRINT "a("; STR$(i); ","; STR$(j); ") ="; array(i, j)
MAT REDIM array(0,0)
END</
==={{header|Yabasic}}===
<
input "Enter other positive integer: " j
dim a(i, j)
a(i, j) = i * j
print "a(", str$(i), ",", str$(j), ") = ", a(i, j)
exit</
==={{header|BBC BASIC}}===
<
DIM array(a%, b%)
array(1, 1) = PI
PRINT array(1, 1)</
==={{header|Commodore BASIC}}===
Note: Size of array may be limited by RAM availability in some Commodore machines.
<
15 print "Size of array:"
20 print "Columns (1-20)";:input x%
Line 502:
70 print "Element";nx;",";ny;"= '";a$(nx,ny);"'"
80 clr:rem clear variables from ram
</syntaxhighlight>
{{out}}
Line 518:
</pre>
==={{header|FreeBASIC}}===
<
Dim As Integer i, j
Line 528:
Print
Print "Press any key to quit"
Sleep</
{{out}}
Line 537:
==={{header|IS-BASIC}}===
<
110 NUMERIC ARRAY(1 TO A,1 TO B)
120 LET ARRAY(1,1)=PI
130 PRINT ARRAY(1,1)</
==={{header|Liberty BASIC}}===
Line 552:
If you dimension arrays that are extra large to make sure you can hold data, but only have a small set of data, then all the space you reserved is wasted.
This hurts performance, because memory is set aside for the number of elements in the DIM statement.
<syntaxhighlight lang="lb">
input "Enter first array dimension "; a
input "Enter second array dimension "; b
Line 562:
end
</syntaxhighlight>
==={{header|PureBasic}}===
<
Define x, y
Line 582:
PrintN("Press ENTER to exit"):Input()
End ; Close down and let PureBasic delete the Console and all variables.
EndIf</
==={{header|QuickBASIC}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<
INPUT a, b 'inputs need to be separated by commas
DIM array (1 TO a, 1 TO b)
array(1,1) = 42
PRINT array(1,1)
ERASE array</
==={{header|Run BASIC}}===
<
print "Enter array 2 greater than 0"; : input a2
Line 603:
chrArray$(1,1) = "Hello"
numArray(1,1) = 987.2
print chrArray$(1,1);" ";numArray(1,1)</
==={{header|Sinclair ZX81 BASIC}}===
Arrays are indexed from 1; the only limit on their size (which may be an exigent limit) is the available memory. We create an array, write to a randomly selected element and then print it out, and finally use <code>CLEAR</code> to destroy the array (and all the other variables in the program).
<
20 INPUT D1
30 PRINT D1
Line 620:
120 PRINT "ITEM ";X;", ";Y;" = ";A(X,Y)
130 CLEAR
140 PRINT "ARRAY DESTROYED"</
{{out}}
<pre>1ST DIMENSION: 11
Line 629:
==={{header|Sinclair ZX Spectrum BASIC}}===
<
20 DIM a(rows,columns): REM defines a numeric array
30 LET a=INT (RND*rows)+1: LET c=INT (RND*columns+1): REM the array is labelled a, but the letter a is still available for variable assignment
40 LET a(a,c)=1
50 PRINT a(a,c)
60 DIM a(1): REM arrays cannot be removed without CLEARing the entire variable space, but redimensioning them to 1 will save most of the space they used</
==={{header|TI-83 BASIC}}===
<
Input "COLS? ",C
{R,C}→dim([A])
42→[A](1,1)
Disp [A](1,1)
DelVar [A]</
==={{header|Visual Basic .NET}}===
<
Sub Main()
Console.WriteLine("Enter two space-delimited integers:")
Line 659:
Console.WriteLine(arr(0, 0))
End Sub
End Module</
{{out}}
Line 670:
{{works with|https://github.com/dzaima/CBQN CBQN}}
<
# Cut 𝕩 at occurrences of 𝕨, removing separators and empty segments
Line 683:
# •GetLine is a nonstandard CBQN extension.
•Show ⥊⟜(↕×´) ParseNums •GetLine@</
{{out}}
Line 697:
=== C99 ===
{{works with|C99}}
<
int main(int argc, char **argv) {
Line 709:
return 0;
}</
=== Traditional Style ===
Allocate multi-dimensional arrays with a single call to malloc. The demonstration code builds a rank 3 array.
<syntaxhighlight lang="c">
/*
assume this file is c.c ,
Line 829:
return EXIT_SUCCESS;
}
#endif</
This style is supported by all 'C' compilers.
<
#include <stdlib.h>
int main(int argc, char **argv)
Line 852:
free(a1);
return 0;
}</
This style also supports more efficient memory utilization if you're only using a portion of the
array. If you only need the upper right half of a square array, you can do something like the following.
<
#include <stdlib.h>
int main(int argc, char **argv)
Line 882:
free(a1);
return 0;
}</
This approach most closely matches the C99 example, as '''alloca''' allocates on the [[stack]], rather than the [[heap]], as '''malloc''' does.
<
#include <alloca.h>
int main(int argc, char **argv)
Line 903:
printf("array[%d][%d] is %d\n",user1/2,user2/2,array[user1/2][user2/2]);
return 0;
}</
=={{header|C sharp|C#}}==
<
class Program
{
Line 920:
Console.ReadLine();
}
}</
=={{header|C++}}==
=== With language built-in facilities ===
<
int main()
Line 950:
return 0;
}</
=== Using std::vector from the standard library ===
<
#include <vector>
Line 974:
// the array is automatically freed at the end of main()
return 0;
}</
=== Using boost::multi_array ===
{{libheader|Boost}}
<
#include <boost/multi_array.hpp>
Line 999:
return 0;
}</
=== Using boost::uBLAS ===
{{libheader|Boost|from 1.29}}
{{works with|Boost|1.54}}
<
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
Line 1,021:
return EXIT_FAILURE;
}</
=={{header|Clean}}==
<
Start :: *World -> { {Real} }
Line 1,031:
(_, dim1, console) = freadi console
(_, dim2, console) = freadi console
= createArray dim1 (createArray dim2 1.0)</
=={{header|Clojure}}==
<
cols (Integer/parseInt (read-line))
a (to-array-2d (repeat rows (repeat cols nil)))]
(aset a 0 0 12)
(println "Element at 0,0:" (aget a 0 0)))</
=={{header|CLU}}==
<
stream$puts(stream$primary_output(), s)
return(int$parse(stream$getl(stream$primary_input())))
Line 1,070:
% The array will be automatically garbage-collected once there
% are no more references to it.
end start_up</
{{out}}
<pre>Width? 8
Line 1,077:
=={{header|Common Lisp}}==
<
(d2 (read)))
(assert (and (typep d1 '(integer 1))
Line 1,086:
(p2 (floor d2 2)))
(setf (aref array p1 p2) t)
(print (aref array p1 p2))))</
The <tt>[http://www.lispworks.com/documentation/HyperSpec/Body/m_assert.htm assert]</tt> will allow the user to reenter the dimensions if they are not positive integers.
Line 1,094:
Arrays in Component Pascal are started from zero index. No DISPOSE-like procedures because of garbage collection.
<
MODULE TestArray;
(* Implemented in BlackBox Component Builder *)
Line 1,112:
END DoTwoDim;
END TestArray.</
=={{header|Crystal}}==
<
first = gets.not_nil!.to_i32
Line 1,128:
arr[first][second] = random.next_int
puts arr[first][second]</
=={{header|D}}==
<
import std.stdio, std.conv, std.string;
int nRow, nCol;
Line 1,154:
array[0][0] = 3.5;
writeln("The number at place [0, 0] is ", array[0][0]);
}</
{{out}}
<pre>Give me the numer of rows:
Line 1,162:
=={{header|Delphi}}==
Dimensions are generated randomly, not input by user.
<
{$APPTYPE CONSOLE}
Line 1,189:
Readln;
end.</
Test run:
Line 1,209:
Typified array
<
public program()
Line 1,224:
console.printLine(myArray.at(0, 0))
}</
Jagged array
<
import extensions;
Line 1,243:
console.printLine(myArray2[0][0]);
console.printLine(myArray2[1][0]);
}</
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">
defmodule TwoDimArray do
Line 1,276:
IO.puts(TwoDimArray.get(arr,2,0))
</syntaxhighlight>
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
-module( two_dimensional_array ).
Line 1,298:
New_array = set( X - 1, Y - 1, X * Y, Array ),
io:fwrite( "In position ~p ~p we have ~p~n", [X - 1, Y - 1, get( X - 1, Y - 1, New_array)] ).
</syntaxhighlight>
{{out}}
<pre>
Line 1,308:
=={{header|ERRE}}==
In ERRE language arrays created at run-time is "dynamic arrays". For this task will be enough this code:
<syntaxhighlight lang="text">
PROGRAM DYNAMIC
Line 1,321:
PRINT("Value in row";2;"and col";3;"is";A%[2,3])
END PROGRAM
</syntaxhighlight>
You can redimension A% using pragmas:
!$ERASE A% with a subsequent
Line 1,327:
=={{header|Euphoria}}==
<
sequence array
Line 1,341:
array[i][j] = height + width
printf(1,"array[%d][%d] is %d\n", {i,j,array[i][j]})</
=={{header|F Sharp|F#}}==
<
let width = int( Console.ReadLine() )
Line 1,350:
let arr = Array2D.create width height 0
arr.[0,0] <- 42
printfn "%d" arr.[0,0]</
=={{header|Factor}}==
Factor doesn't provide any support for easy access of 2d arrays. But since factor's written in factor, we can just add it and it's just as good :)
<
sequences ;
IN: rosettacode.runtime2darray
Line 1,367:
[ [ 42 { 0 0 } ] dip set-Mi,j ] ! set the { 0 0 } element to 42
[ [ { 0 0 } ] dip Mi,j . ] ! read the { 0 0 } element
bi ;</
=={{header|Fermat}}==
<
?n;
Array a[m,n]; {generate an array of m rows and n columns}
a[m\2, n\2] := m+n-1; {put some value in one of the cells}
!!a[m\2, n\2]; {display that entry}
@[a]; {delete the array}</
=={{header|Forth}}==
<
create ( width height "name" ) over , * cells allot
does> ( x y -- addr ) dup cell+ >r @ * + cells r> + ;
Line 1,385:
36 0 0 test !
0 0 test @ . \ 36</
{{libheader|Forth Scientific Library}}
<
& my-matrix{{ 8 9 }}malloc
Line 1,394:
my-matrix{{ 3 4 }} @ .
& my-matrix{{ }}free</
=={{header|Fortran}}==
In Fortran 90 and later
<
IMPLICIT NONE
Line 1,417:
DEALLOCATE (array, STAT=errcheck)
END PROGRAM Example</
=={{header|Fōrmulæ}}==
Line 1,428:
=={{header|Frink}}==
<
[rows, cols] = dims = eval[input["Enter dimensions: ", ["Rows", "Columns"]]]
a = new array[dims, 0] // Create and initialize to 0
a@(rows-1)@(cols-1) = 10
println[a@(rows-1)@(cols-1)]
</syntaxhighlight>
=={{header|GAP}}==
<
a := NullMat(2, 2);
# [ [ 0, 0 ], [ 0, 0 ] ]
Line 1,450:
Determinant(a);
# 1</
=={{header|Go}}==
Arrays in Go are only one dimensional. Code below show the obvious way of composing a 2d array as an array of arrays that can be indexed as a[r][c].
<
import "fmt"
Line 1,481:
a = nil
// memory allocated earlier with make can now be garbage collected.
}</
The technique above alocates each row separately. This might be good if you need extremely large arrays that cannot be allocated in a single piece. It might be bad though, for locality, as there would be no guarantee that the separate allocations would be localized in memory. A technique that maintains locality is this,
<
a := make([][]int, row)
e := make([]int, row * col)
for i := range a {
a[i] = e[i*col:(i+1)*col]
}</
Now all rows are allocated with a single allocation. Alternatively, slice e can be used directly without going through slice a. Element r c can be accessed simply as e[r*cols+c] for example, or accessor functions can be defined such as,
<syntaxhighlight lang="go">
func get(r, c int) int {
return e[r*cols+c]
}</
=={{header|Groovy}}==
Solution:
<
(0..<nrows).collect { [0]*ncols }
}</
Test:
<
System.in.splitEachLine(/,\s*/) { dim ->
Line 1,520:
a2d.each { println it }
println()
}</
Input:
Line 1,540:
=={{header|Haskell}}==
<
doit n m = a!(0,0) where a = array ((0,0),(n,m)) [((0,0),42)]</
=={{header|HicEst}}==
<
DLG(NameEdit=rows, NameEdit=cols, Button='OK', TItle='Enter array dimensions')
Line 1,551:
ALLOCATE(array, cols, rows)
array(1,1) = 1.234
WRITE(Messagebox, Name) array(1,1) </
=={{header|Icon}} and {{header|Unicon}}==
Line 1,557:
Multiply dimensioned arrays are arrays of arrays in both languages.
<
nr := integer(args[1]) | 3 # Default to 3x3
nc := integer(args[2]) | 3
Line 1,569:
A[x][y] := &pi
write("A[",x,"][",y,"] -> ",A[x][y])
end</
Sample output:
Line 1,579:
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 1,587:
;==> outputs 5.6
delvar, d</
=={{header|J}}==
Line 1,593:
The natural ways of creating a two dimensional array, from array dimensions, in J are <code>i.</code> and <code>$</code>
<
array2=: 5 6 $ 2 NB. a 5 by 6 array where every value is the number 2</
To update the upper left corner of the array with the value 99, you might use <code>}</code>
<
And, to retrieve that value you might use <code>{</code>
<
Finally, J manages storage for you, so to delete the array, you could either have the name refer to a new value
<
or you could remove the name itself:
<syntaxhighlight lang
Putting these ideas together and adding a few frills:
<
assert. y -: 0 0 + , y NB. error except when 2 dimensions are specified
INIT=. 0 NB. array will be populated with this value
Line 1,622:
ARRAY=. NEW INDEX} ARRAY NB. use our new value at that location
INDEX { ARRAY NB. and return the value from that location
)</
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.
Example use (result should always be 1 which is the value of <code>NEW</code>):
<
1</
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</
=={{header|Java}}==
<
public class twoDimArray {
Line 1,650:
System.out.println("The number at place [0 0] is " + array[0][0]);
}
}</
=={{header|JavaScript}}==
<
var height = Number(prompt("Enter height: "));
Line 1,669:
//cleanup array
arr = void(0);</
=={{header|jq}}==
Line 1,686:
Here's a simple example.
<
# filled with the input element
def matrix(m;n):
Line 1,696:
# Task: create a matrix with dimensions specified by the user
# and set the [1,2] element:
(0 | matrix($m|tonumber; $n|tonumber)) | setpath([1,2]; 99)</
If the above is in a file, say 2d.jq, the invocation:
Line 1,702:
<tt>jq -n -c --arg m 2 --arg n 3 -f 2d.jq </tt>
would produce: <
=={{header|Julia}}==
Line 1,708:
Julia supports n-dimensional arrays as native data types: `Array{T, N}`, where `T` is the type of it´s elements and `N` is the number of dimensions.
<
print(prompt)
return readline()
Line 1,723:
x[3, 3] = 5.0 # overloads `setindex!` generic function
x::Matrix # `Matrix{T}` is an alias for `Array{T, 2}`
x = 0; gc() # Julia has no `del` command, rebind `x` and call the garbage collector</
Manually calling the garbage collector may or may not actually collect the array, but it will be eventually.
Line 1,729:
=={{header|Kotlin}}==
Program arguments provide dimensions of the array (4 5 in the example).
<
// build
val dim = arrayOf(10, 15)
Line 1,743:
// print
array.forEach { println(it.asList()) }
}</
{{out}}
<pre>[1, 2, 3, 4, 5]
Line 1,752:
=={{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</
=={{header|Lua}}==
<
a, b = io.read() + 0, io.read() + 0
Line 1,763:
matrix[a][b] = 5
print(matrix[a][b])
print(matrix[1][1])</
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module CheckArray {
Do {
Line 1,782:
}
CheckArray
</syntaxhighlight>
=={{header|Maple}}==
This hardly covers the richness and complexity of arrays in Maple, but here goes:
<
> a[1,1] := 1: # assign an element
> a[2,3] := 4: # assign an element
Line 1,797:
> a := 'a': # unassign the name
> gc(); # force a garbage collection; may or may not actually collect the array, but it will be eventually</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
array[[1,1]]=RandomReal[];
array[[1,1]]
]</
=={{header|MATLAB}} / {{header|Octave}}==
<
height = input('Array Height: ');
Line 1,816:
clear array; % de-allocate (remove) array from workspace
</syntaxhighlight>
Sample Output:
<
Array Height: 12
Array element (1,1) = 12</
=={{header|Maxima}}==
<
n: readonly("Input x-size: ")$
m: readonly("Input y-size: ")$
Line 1,833:
/* indexing starts from 0 */
print(a[0,0]);
print(a[n-1,m-1]);</
=={{header|MAXScript}}==
<
b = getKBValue prompt:"Enter second dimension:"
arr1 = #()
Line 1,846:
)
arr1[a][b] = 1
print arr1[a][b]</
=={{header|MUMPS}}==
This example uses a two level tree to mimic an 2D array.
<syntaxhighlight lang="mumps">
ARA2D
NEW X,Y,A,I,J
Line 1,862:
KILL X,Y,A,I,J
QUIT
</syntaxhighlight>
=={{header|NetRexx}}==
'''Note:''' No attempt is made to validate the input.
Any errors will be handled as exceptions by the underlying JVM.
<
options replace format comments java crossref symbols nobinary
Line 1,879:
say "arry["xPos","yPos"]:" arry[xPos, yPos]
return
</syntaxhighlight>
'''Output:'''
<pre>
Line 1,888:
=={{header|Nim}}==
<
let
Line 1,907:
echo s[0][0]
# The allocated memory is freed by the garbage collector.</
=={{header|Objeck}}==
<
use IO;
Line 1,934:
}
}
</syntaxhighlight>
=={{header|Objective-C}}==
Line 1,944:
{{works with|GNUstep}}
{{works with|Cocoa}}
<
int main()
Line 1,969:
}
return 0;
}</
=={{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 ();;</
or using the module [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Bigarray.html Bigarray]:
<
let nbr2 = read_int ();;
let arr = Bigarray.Array2.create Bigarray.float32 Bigarray.c_layout nbr1 nbr2 ;;
arr.{0,0} <- 3.5;;
print_float arr.{0,0}; print_newline ();;</
=={{header|ooRexx}}==
ooRexx arrays can be created with up to 999,999,999 dimensions...assuming you have enough memory to do so.
Actually it's the 'size' of the array that's limited (the product of the dimensions).
<
pull d1
say "enter the second dimension"
Line 2,000:
say a[10,10]
max=1000000000
b = .array~new(max,max) </
{{out}}
<pre>D:\>rexx 2d
Line 2,017:
=={{header|Oz}}==
Oz does not have multi-dimensional arrays. But we can create an array of arrays (similarly to most examples on this page):
<
%% Read width and height from stdin
class TextFile from Open.file Open.text end
Line 2,031:
%% set and read element
Arr.1.1 := 42
{Show Arr.1.1}</
=={{header|PARI/GP}}==
<
my(M=matrix(m,n,i,j,0));
M[1,1]=1;
M[1,1]
};</
=={{header|Pascal}}==
Line 2,045:
The following code is standard Extended Pascal (tested with <tt>gpc --extended-pascal</tt>):
<
type
Line 2,070:
{ get rid of array }
dispose(data);
end.</
=={{header|Perl}}==
Line 2,077:
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 2,095:
print "\n";
}
}</
The above is a bit verbose, here is a simpler implementation:
<
my ($x, $y) = @_;
map {[ (0) x $x ]} 1 .. $y
Line 2,117:
> 0 0 0
> 0 1 0
> 0 0 0</
=={{header|Phix}}==
Obviously the sigificant part here is the three lines beginning "sequence array", the rest is all just boilerplate gui code.
<!--<
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Create2Darray.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">-- (layout/spacing leaves a little to be desired...)</span>
Line 2,164:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
The distributed version contains the older and simpler but console-only code.
=={{header|Phixmonti}}==
<
"Enter height: " input tonum nl
Line 2,177:
-1 get -1 get "Value of the last element of the last dimension: " print print drop
drop /# remove array/list from the stack #/
</syntaxhighlight>
With syntactic sugar
<
"Enter height: " input tonum nl
Line 2,188:
( -1 -1 ) mget "Value of the last element of the last dimension: " print print drop
drop /# remove array/list from the stack #/
</syntaxhighlight>
With more syntactic sugar
<
"Enter height: " input tonum nl
Line 2,199:
( -1 -1 ) sget "Value of the last element of the last dimension: " print print
drop /# remove array/list from the stack #/
</syntaxhighlight>
=={{header|Picat}}==
<
go =>
Line 2,209:
X=new_array(Rows,Cols),
X[1,1] = Rows*Cols+1,
println(X[1,1]).</
{{out}}
Line 2,216:
=={{header|PicoLisp}}==
<
(let A (make (do DX (link (need DY))))
(set (nth A 3 3) 999) # Set A[3][3] to 999
Line 2,222:
(get A 3 3) ) ) # Return A[3][3]
(2dimTest 5 5)</
Output:
<pre>(NIL NIL NIL NIL NIL)
Line 2,232:
=={{header|PL/I}}==
<syntaxhighlight lang="text">
/* First way using a controlled variable: */
Line 2,245:
free A;
</syntaxhighlight>
<
1.00000E+0000
</syntaxhighlight>
<syntaxhighlight lang="pl/i">
/* Second way using a BEGIN block: */
Line 2,262:
/* The array is automatically destroyed when the block terminates. */
</syntaxhighlight>
<
6.00000E+0000 7.00000E+0000 8.00000E+0000 9.00000E+0000 1.00000E+0001
1.10000E+0001 1.20000E+0002
</syntaxhighlight>
<syntaxhighlight lang="pl/i">
/* Third way using a PROCEDURE block: */
Line 2,281:
/* The array is automatically destroyed when the procedure terminates. */
</syntaxhighlight>
<syntaxhighlight lang="pl/i">
1.00000E+0000 2.00000E+0000 3.00000E+0000 4.00000E+0000 5.00000E+0000
6.00000E+0000 7.00000E+0000 8.00000E+0000 9.00000E+0000 1.00000E+0001
1.10000E+0001 1.20000E+0001 1.30000E+0001 1.40000E+0001 1.50000E+0001
1.60000E+0001 1.70000E+0001 1.80000E+0001 1.90000E+0001 2.00000E+0001
</syntaxhighlight>
=={{header|Pop11}}==
<
incharitem(charin) -> itemrep;
;;; Read sizes
Line 2,302:
ar(0,0) =>
;;; Make sure array is unreferenced
0 -> ar;</
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 2,309:
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function Read-ArrayIndex ([string]$Prompt = "Enter an integer greater than zero")
{
Line 2,332:
$array2d = New-Object -TypeName 'System.Object[,]' -ArgumentList $x, $y
</syntaxhighlight>
{{Out}}
<pre>
Line 2,339:
</pre>
Populate the array:
<syntaxhighlight lang="powershell">
[int]$k = 1
Line 2,346:
0..5 | ForEach-Object -Begin {$k += 10} -Process {$array2d[$i,$_] = $k + $_}
}
</syntaxhighlight>
This is the entire array:
<syntaxhighlight lang="powershell">
for ($i = 0; $i -lt 6; $i++)
{
"{0}`t{1}`t{2}`t{3}`t{4}`t{5}" -f (0..5 | ForEach-Object {$array2d[$i,$_]})
}
</syntaxhighlight>
{{Out}}
<pre>
Line 2,364:
</pre>
Get an element of the array:
<syntaxhighlight lang="powershell">
$array2d[2,2]
</syntaxhighlight>
{{Out}}
<pre>
Line 2,375:
{{works with|Python| 2.5 and 3.6}}
<
height = int(raw_input("Height of Array: "))
myarray = [[0] * width for i in range(height)]
myarray[0][0] = 3.5
print (myarray[0][0])</
'''Note:''' Some people may instinctively try to write myarray as [[0] * width] * height, but the * operator creates ''n'' references to [[0] * width]
Line 2,385:
You can also use a two element tuple to index a dictionary like so:
<
# or, in pre 2.7 versions of Python: myarray = dict(((w,h), 0) for w in range(width) for h in range(height))
myarray[(0,0)] = 3.5
print (myarray[(0,0)])</
=={{header|Quackery}}==
<
[ dip dup
Line 2,425:
say "Reading element {1,4} of array: "
' [ 1 4 ] {peek} echo</
{{out}}
Line 2,438:
=={{header|R}}==
{{trans|C}}
<
dims <- as.numeric(strsplit(input, " ")[[1]])
arr <- array(dim=dims)
Line 2,444:
jj <- ceiling(dims[2]/2)
arr[ii, jj] <- sum(dims)
cat("array[", ii, ",", jj, "] is ", arr[ii, jj], "\n", sep="")</
=={{header|Racket}}==
Line 2,450:
Using a vector of vectors to represent arrays:
<syntaxhighlight lang="racket">
#lang racket
Line 2,463:
array
</syntaxhighlight>
Output:
Line 2,483:
Line 4: Print each line of the array.
<syntaxhighlight lang="raku"
my @array = [ '@' xx $minor ] xx $major;
@array[ *.rand ][ *.rand ] = ' ';
.say for @array;</
Typical run:
<syntaxhighlight lang="text">Dimensions? 5x35
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
Line 2,495:
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
</syntaxhighlight>
The most recent versions of Rakudo have preliminary support for 'shaped arrays'. Natively shaped arrays are a flexible feature for declaring typed, potentially multi-dimensional arrays, potentially with pre-defined
dimensions. They will make memory-efficient matrix storage and matrix operations possible.
<syntaxhighlight lang="raku"
my Int @array[$major;$minor] = (7 xx $minor ) xx $major;
@array[$major div 2;$minor div 2] = 42;
say @array;</
Typical run:
<syntaxhighlight lang="text">Dimensions? 3 x 10
[[7 7 7 7 7 7 7 7 7 7] [7 7 7 7 7 42 7 7 7 7] [7 7 7 7 7 7 7 7 7 7]]</
=={{header|Red}}==
<
width: to-integer ask "What is the width of the array? "
Line 2,528:
matrix/(a)/(a): 10 ; accessing elements with words requires special care
matrix/:b/:b: 33 ; alternative
print mold matrix</
{{out}}
<pre>
Line 2,537:
=={{header|REXX}}==
<
call bloat /*the BLOAT procedure does all allocations.*/
/*no more array named @ at this point. */
Line 2,569:
╚════════════════════════════════════════════════════════════════════╝*/
drop @. /*because of the PROCEDURE statement, the*/
return /* [↑] DROP statement is superfluous. */</
'''output''' when the following input is entered after the prompt message: <tt> 30 15 </tt>
<pre>
Line 2,605:
=={{header|Ring}}==
<
See 'Enter width : ' give width
See 'Enter height : ' give height
Line 2,611:
aList = list(height) for x in aList x = list(width) next
aList[1][2] = 10 See aList[1][2] + nl
</syntaxhighlight>
=={{header|Ruby}}==
<
w=gets.to_i
arr = Array.new(gets.to_i){Array.new(w)}
arr[1][3] = 5
p arr[1][3]</
=={{header|Rust}}==
<
fn main() {
Line 2,635:
v[0][0] = 1;
println!("{}", v[0][0]);
}</
=={{header|Scala}}==
<
def main(args: Array[String]): Unit = {
val x = Console.readInt
Line 2,647:
println("The number at (0, 0) is "+a(0)(0))
}
}</
=={{header|Scheme}}==
Line 2,655:
There is no two-dimensional array-type built in to Scheme, but there is a vector.
<
(import (scheme base)
(scheme read)
Line 2,680:
(floor (/ y 2))))
(newline)
</syntaxhighlight>
{{out}}
Line 2,696:
There are SRFI libraries providing arrays. This example uses SRFI 63.
<
(import (except (scheme base) equal?)
(scheme read)
Line 2,715:
;; Retrieve and display result
(display (array-ref array (floor (/ x 2)) (floor (/ y 2)))) (newline)
</syntaxhighlight>
{{out}}
Line 2,727:
=={{header|Seed7}}==
<
const proc: main is func
Line 2,742:
anArray[1][1] := 3;
writeln("The number at place [1, 1] is " <& anArray[1][1]);
end func;</
Output:
Line 2,752:
=={{header|Sidef}}==
<
y.of { x.of(0) };
}
Line 2,761:
var matrix = make_matrix(x, y); # create the matrix
matrix[y/2][x/2] = 1; # write something inside it
say matrix; # display the matrix</
{{out}}
<pre>
Line 2,769:
</pre>
=={{header|Simula}}==
<
INTEGER N,M;
M := ININT;
Line 2,780:
END;
! ARRAY A OUT OF SCOPE ;
END.</
{{in}}
<pre>10 20</pre>
Line 2,790:
=={{header|Smalltalk}}==
{{works with|Pharo}}
<
m := (FillInTheBlankMorph request: 'Number of rows?') asNumber.
n := (FillInTheBlankMorph request: 'Number of columns?') asNumber.
Line 2,802:
at: (aMatrix columnCount // 2).
Transcript show: 'Entry is', e printString.
</syntaxhighlight>
{{works with|GNU Smalltalk}}
Smalltalk has no problems in creating objects at runtime. I haven't found a class for multidimensional array in the standard library, so let us suppose to have a class named MultidimensionalArray.
<
num1 := stdin nextLine asInteger.
num2 := stdin nextLine asInteger.
Line 2,823:
(arr at: {i. j}) displayNl
]
].</
A possible implementation for a '''Bi'''dimensionalArray class is the following (changing ''Multi'' into ''Bi'' and using this class, the previous code runs fine):
<
|biArr|
<comment: 'bidim array'>
Line 2,852:
^ (biArr at: (biDim at: 1)) at: (biDim at: 2) put: val
]
].</
Instead of implementing such a class (or the MultidimensionalArray one), we can use a LookupTable class, using Array objects as keys (each element of the array will be an index for a specific ''dimension'' of the "array"). The final effect is the same as using an array (almost in the ''AWK sense'') and the approach has some advantages.
<
num1 := stdin nextLine asInteger.
num2 := stdin nextLine asInteger.
Line 2,875:
(pseudoArr at: {i. j}) displayNl.
]
].</
=={{header|SNOBOL4}}==
Line 2,885:
Note: trim(input) is needed for Snobol4+.
<
output = 'Enter X,Y:'; xy = trim(input)
xy break(',') . x ',' rem . y
Line 2,905:
* # Release array for garbage collection
arr =
end</
Output:
Line 2,915:
=={{header|Standard ML}}==
<
val nbr2 = valOf (TextIO.scanStream (Int.scan StringCvt.DEC) TextIO.stdIn);
val array = Array2.array (nbr1, nbr2, 0.0);
Array2.update (array, 0, 0, 3.5);
print (Real.toString (Array2.sub (array, 0, 0)) ^ "\n");</
=={{header|Stata}}==
<
display "Number of columns?" _request(nc)
matrix define a=J($nr,$nc,0)
matrix a[1,2]=1.5
matrix list a
matrix drop a</
This is also possible in Mata, except that console input is still done from Stata:
<
mata stata display "Number of rows?" _request(nr)
mata stata display "Number of columns?" _request(nc)
Line 2,940:
a
mata drop a
end</
=={{header|Swift}}==
<syntaxhighlight lang="text">import Foundation
print("Enter the dimensions of the array seperated by a space (width height): ")
Line 2,961:
println(array)
}
}</
{{out}}
<pre>
Line 2,971:
=={{header|Tcl}}==
{{works with|Tcl|8.5}}
<
puts "Enter width:"
set width [gets stdin]
Line 2,988:
# Cleanup array
unset arr
</syntaxhighlight>
=={{header|Toka}}==
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 3,006:
[ ( value a b address -- )
array.get array.put
] is 2D-put-element</
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</
=={{header|Ursa}}==
<
out "width: " console
set width (in int console)
Line 3,034:
set twodstream<0><0> 5
out twodstream<0><0> endl console</
=={{header|VBA}}==
<syntaxhighlight lang="vb">
Option Explicit
Line 3,058:
Erase myArray
End Sub
</syntaxhighlight>
=={{header|Vim Script}}==
<
" The optional third argument specifies the initial value
" (default is 0).
Line 3,089:
let array[rows - 1][cols - 1] = rows * cols
echo array[rows - 1][cols - 1]
unlet array</
=={{header|Wren}}==
<
var x
Line 3,119:
System.print("\na[%(x-1)][%(y-1)] = %(a[x-1][y-1])")
// make the array eligible for garbage collection
a = null</
{{out}}
Line 3,132:
=={{header|XPL0}}==
<
def IntSize=4; \number of bytes in an integer (2 or 4 depending on version)
int X, Y, A, I;
Line 3,140:
A(X/2, Y/2):= X+Y;
IntOut(0, A(X/2, Y/2)); CrLf(0);
]</
Space allocated by the Reserve intrinsic is automatically released when
Line 3,158:
Since the array is row major, sub-scripting works as expected: [row][col].
<
cols:=ask("columns: ").toInt();
array:=rows.pump(List.createLong(rows),List.createLong(cols,0).copy);
array[1][2]=123;
array.println();
array[1][2].println();</
The createLong method pre-allocates a list, optionally filled with a constant or computation.
{{out}}
Line 3,173:
</pre>
If you want Matrix/linear algebra, you can use the GNU Scientific Library:
<
rows:=ask("Rows: ").toInt();
cols:=ask("columns: ").toInt();
Line 3,179:
m[1,2]=123;
m.format().println();
println(m[1,2]);</
Again, garbage collected.
{{out}}
Line 3,192:
=={{header|zonnon}}==
<
module Main;
type
Line 3,208:
writeln("m[0,1].> ",m[0,1])
end Main.
</syntaxhighlight>
{{Out}}
<pre>
|
Revision as of 22:41, 26 August 2022
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Data Structure
This illustrates a data structure, a means of storing data within a program.
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.
11l
V width = 3
V height = 5
V myarray = [[0] * width] * height
print(myarray[height-1][width-1])
68000 Assembly
The routine used to retrieve the input is left unimplemented.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Array setup
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Create_2D_Array:
ARRAY_2D equ $100000
ARRAY_POINTER_VARIABLE equ $200000
; input: D0 = width, D1 = height
; assume the input is byte length and unsigned, ranging from 1 to FF.
AND.L #$000000FF,D0
AND.L #$000000FF,D1 ;sanitize the input to byte length.
LEA ARRAY_2D,A0 ;get base array address.
;The array's size will be measured in bytes, as this is how memory offsetting is measured.
;For this example the elements will all be 32-bit.
;Therefore, the dimensions need to be multiplied by the byte count of each element.
LSL.W #2,D0 ;four bytes per element = multiply by 4
LSL.W #2,D1
;Next, these values are multiplied to get the array's size.
MOVE.L D0,D2
MULU D1,D2
;D2 is the array's size (measured in bytes) and will be placed at the beginning.
;This does not count as an element of the array for the purposes of row/column indexing.
;The array's base address will be offset by 4 bytes prior to any indexing.
MOVE.L D2,(A0)+ ;store D2 in A0, add 4 to A0
MOVEA.L A0,[ARRAY_POINTER_VARIABLE]
;the brackets are optional, they show that this is a memory address label.
;this is still a move to a memory address with or without the brackets.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Storing a value in the array
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LEA ARRAY_POINTER_VARIABLE,A1 ;load the address where the array's base address is stored.
MOVE.L (A1),A1 ;dereference the pointer and get ARRAY_2D+4 into A1.
; for this example the arbitrary row/column indices (2,5) will be used.
MOVE.L #2,D4
MULU D0,D4 ;there are D0 entries per row, multiply row index by elements per row.
MOVE.L #5,D5
MOVE.L #$00112233,D7 ;determine the value we want to store in the array.
; The bytes per element was factored into D0 when the array was created. So D4 is already where it should be.
LSL.L #2,D5 ;column index still needs to be scaled by the bytes per element.
LEA (A1,D4),A1 ;select the desired row.
;68000 doesn't allow you to use more than 1 data register at a time to offset. So we have to offset separately.
;Despite the use of parentheses this is NOT a dereference like it would be with "MOVE.L (A1),D7". D4 is merely added to the address in A1.
MOVE.L D7,(A1,D5) ;store #$00112233 in row 2, column 5 of the array.
;Loading a value is the same as storing it, except the operands in the last instruction are reversed, and MOVE.L #$00112233,D7
;is omitted.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Destroying the array
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; The array is destroyed by storing something else in its location. If you really want to reset it to zero, you can
; do so with the following:
LEA ARRAY_POINTER_VARIABLE,A1
MOVE.L (A1),A1
MOVE.L -(A1),D7
;get the array size into D7. Remember that the array's size was stored just before its data.
This value is potentially too large for a single DBRA, but it can be split up.
SWAP D7
MOVE.W D7,D6 ;get the top half of D7 into D6. D6 will be the outer loop's DBRA value.
SWAP D7
SUBQ.L #1,D7 ;D7 needs to be decremented by 1. D6 is fine the way it is.
MOVE.L (A0)+,D0 ;dummy move to increment the pointer back to the array base.
MOVEQ #0,D0 ;faster than MOVE.L #0,D0
loop_destroyArray:
MOVE.L D0,(A0)+
DBRA D7,loop_destroyArray ;loop using bottom 2 bytes of the array size as a loop counter
DBRA D6,loop_destroyArray ;decrement this, D7 is $FFFF each time execution gets here so this acts as a "carry" of sorts.
;if this value was 0 prior to the loop, the loop ends immediately.
Action!
The user must type in the monitor the following command after compilation and before running the program!
SET EndProg=*
CARD EndProg ;required for ALLOCATE.ACT
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program!
DEFINE PTR="CARD"
DEFINE INT_SIZE="2"
DEFINE CARD_SIZE="2"
TYPE IntArray2D=[BYTE rows,cols PTR p]
BYTE FUNC GetNumber(CHAR ARRAY s)
BYTE n,min=[1],max=[100]
DO
PrintF("Get number of %S (%B..%B): ",s,min,max)
n=InputB()
IF n>=min AND n<=max THEN
EXIT
FI
OD
RETURN (n)
PROC Create(IntArray2D POINTER a)
PTR ARRAY rowArray
BYTE row
IF a.p#0 THEN Break() FI
rowArray=Alloc(a.rows*CARD_SIZE)
a.p=rowArray
FOR row=0 TO a.rows-1
DO
rowArray(row)=Alloc(a.cols*INT_SIZE)
OD
RETURN
PROC Destroy(IntArray2D POINTER a)
PTR ARRAY rowArray
BYTE row
IF a.p=0 THEN Break() FI
rowArray=a.p
FOR row=0 TO a.rows-1
DO
Free(rowArray(row),a.cols*INT_SIZE)
OD
Free(a.p,a.rows*CARD_SIZE)
a.p=0
RETURN
PROC SetValue(IntArray2D POINTER a BYTE row,col INT v)
PTR ARRAY rowArray
INT ARRAY colArray
IF a.p=0 OR row>=a.rows OR col>=a.cols THEN
Break()
FI
rowArray=a.p
colArray=rowArray(row)
colArray(col)=v
RETURN
INT FUNC GetValue(IntArray2D POINTER a BYTE row,col)
PTR ARRAY rowArray
INT ARRAY colArray
IF a.p=0 OR row>=a.rows OR col>=a.cols THEN
Break()
FI
rowArray=a.p
colArray=rowArray(row)
RETURN (colArray(col))
PROC TestCreate(IntArray2D POINTER a)
PrintF("Create array of %B rows and %B cols%E",a.rows,a.cols)
Create(a)
RETURN
PROC TestDestroy(IntArray2D POINTER a)
PrintF("Destroy array of %B rows and %B cols%E",a.rows,a.cols)
Destroy(a)
RETURN
PROC TestSetValue(IntArray2D POINTER a BYTE row,col INT v)
PrintF("Write %I to row %B and col %B%E",v,row,col)
SetValue(a,row,col,v)
RETURN
PROC TestGetValue(IntArray2D POINTER a BYTE row,col)
INT v
v=GetValue(a,row,col)
PrintF("Read at row %B and col %B: %I%E",row,col,v)
RETURN
PROC Main()
IntArray2D a
Put(125) PutE() ;clear screen
AllocInit(0)
a.rows=GetNumber("rows")
a.cols=GetNumber("cols")
a.p=0
TestCreate(a)
TestSetValue(a,a.rows/2,a.cols/2,6502)
TestGetValue(a,a.rows/2,a.cols/2)
TestDestroy(a)
RETURN
- Output:
Screenshot from Atari 8-bit computer
Get number of rows (1..100): 80 Get number of cols (1..100): 90 Create array of 80 rows and 90 cols Write 6502 to row 40 and col 45 Read at row 40 and col 45: 6502 Destroy array of 80 rows and 90 cols
Ada
with Ada.Text_Io;
with Ada.Float_Text_Io;
with Ada.Integer_Text_Io;
procedure Two_Dimensional_Arrays is
type Matrix_Type is array(Positive range <>, Positive range <>) of Float;
Dim_1 : Positive;
Dim_2 : Positive;
begin
Ada.Integer_Text_Io.Get(Item => Dim_1);
Ada.Integer_Text_Io.Get(Item => Dim_2);
-- Create an inner block with the correctly sized array
declare
Matrix : Matrix_Type(1..Dim_1, 1..Dim_2);
begin
Matrix(1, Dim_2) := 3.14159;
Ada.Float_Text_Io.Put(Item => Matrix(1, Dim_2), Fore => 1, Aft => 5, Exp => 0);
Ada.Text_Io.New_Line;
end;
-- The variable Matrix is popped off the stack automatically
end Two_Dimensional_Arrays;
ALGOL 60
begin
comment Create a two-dimensional array at runtime - Algol 60;
integer n,m;
ininteger(0,m);
ininteger(0,n);
begin
integer array a[1:m,1:n];
a[m,n] := 99;
outinteger(1,a[m,n]);
outstring(1,"\n")
end;
comment array a : out of scope;
end
- Input:
5 5
- Output:
99
ALGOL 68
main:(
print("Input two positive whole numbers separated by space and press newline:");
[read int,read int] INT array;
array[1,1]:=42;
print (array[1,1])
)
ALGOL W
begin
integer dimension1UpperBound, dimension2UpperBound;
write( "upper bound for dimension 1: " );
read( dimension1UpperBound );
write( "upper bound for dimension 2: " );
read( dimension2UpperBound );
begin
% we start a new block because declarations must precede statements %
% and variables in array bounds must be from outside the block %
integer array matrix ( 1 :: dimension1UpperBound
, 1 :: dimension2UpperBound
);
% set the first element - the program will crash if the user input %
% upper bounds less than 1 %
matrix( 1, 1 ) := 3;
% write it %
write( matrix( 1, 1 ) );
% the array is automatically deleted when the block ends %
end
end.
Amazing Hopper
Amazing Hopper crea arrays de forma dinámica.
#include <flow.h>
#import lib/input.bas.lib
#include include/flow-input.h
DEF-MAIN
CLR-SCR
MSET(nRow, nCol)
LOCATE( 2,5 ), PRN("Input size rows :")
LOC-COL( 23 ), LET( nRow := ABS(VAL(READ-NUMBER( nRow ) )))
LOCATE( 3,5 ), PRN("Input size cols :")
LOC-COL( 23 ), LET( nCol := ABS(VAL(READ-NUMBER( nCol ) )))
COND( IS-NOT-ZERO?( MUL(nRow,nCol) ) )
DIM(nRow, nCol) AS-VOID( array )
BLK-[1,1], {100} PUT(array)
PRNL("\tElement at position 1,1 : ", GET(array) )
CLEAR(array) /* destroy array */
CEND
END
SUBRUTINES
- Output:
Input size rows : 30 Input size cols : 30 Element at position 1,1 : 100
APL
Arrays are an integral part of APL. Array size, shape, and data type can be easily manipulated at runtime.
array←m n ⍴ 0 ⍝ array of zeros with shape of m by n.
array[1;1]←73 ⍝ assign a value to location 1;1.
array[1;1] ⍝ read the value back out
⎕ex 'array' ⍝ erase the array
AppleScript
AppleScript has no array, but an AppleScript list can be used in a multidimensional fashion. There's no issue with their dimensions, they grow while adding elements. Memory allocation is dynamic.
set R to text returned of (display dialog "Enter number of rows:" default answer 2) as integer
set c to text returned of (display dialog "Enter number of columns:" default answer 2) as integer
set array to {}
repeat with i from 1 to R
set temp to {}
repeat with j from 1 to c
set temp's end to 0
end repeat
set array's end to temp
end repeat
-- Address the first column of the first row:
set array's item 1's item 1 to -10
-- Negative index values can be used to address from the end:
set array's item -1's item -1 to 10
-- Access an item (row 2 column 1):
set x to array's item 2's item 1
return array
-- Destroy array (typically unnecessary since it'll automatically be destroyed once script ends).
set array to {}
To correct the last comment in the script above: when a script's run, the values of its properties, globals, and run-handler (ie. top level) variables are saved back to the script file when the execution finishes. So variables containing bulky values like lists ideally should be set to something smaller before the end in order to prevent file bloat. Or local variables could be used, which would be the better option above as the return
statement prevents the last line from being executed anyway.
Arturo
width: to :integer input "give me the array's width: "
height: to :integer input "give me the array's height: "
arr: array.of: @[width height] 0
x: random 0 dec width
y: random 0 dec height
arr\[x]\[y]: 123
print ["item at [" x "," y "] =" arr\[x]\[y]]
- Output:
give me the array's width: 10 give me the array's height: 2 item at [ 9 , 0 ] = 123
AutoHotkey
Array := []
InputBox, data,, Enter two integers separated by a Space:`n(ex. 5 7)
StringSplit, i, data, %A_Space%
Array[i1,i2] := "that element"
MsgBox, % "Array[" i1 "," i2 "] = " Array[i1,i2]
AutoIt
; == get dimensions from user input
$sInput = InputBox('2D Array Creation', 'Input comma separated count of rows and columns, i.e. "5,3"')
$aDimension = StringSplit($sInput, ',', 2)
; == create array
Dim $a2D[ $aDimension[0] ][ $aDimension[1] ]
; == write value to last row/last column
$a2D[ UBound($a2D) -1 ][ UBound($a2D, 2) -1 ] = 'test string'
; == output this value to MsgBox
MsgBox(0, 'Output', 'row[' & UBound($a2D) -1 & '], col[' & UBound($a2D, 2) -1 & ']' & @CRLF & '= ' & $a2D[ UBound($a2D) -1 ][ UBound($a2D, 2) -1 ] )
AWK
AWK has no multidimensional array; but AWK arrays (which are Associative array indeed) can be used also in a multidimensional fashion. Since AWK arrays are associative arrays, there's no issue in their dimensions: they grow while adding new key-value pair.
/[0-9]+ [0-9]+/ {
for(i=0; i < $1; i++) {
for(j=0; j < $2; j++) {
arr[i, j] = i*j
}
}
# how to scan "multidim" array as explained in the GNU AWK manual
for (comb in arr) {
split(comb, idx, SUBSEP)
print idx[1] "," idx[2] "->" arr[idx[1], idx[2]]
}
}
BASIC
Applesoft BASIC
10 INPUT "ENTER TWO INTEGERS:"; X%, Y%
20 DIM A%(X% - 1, Y% - 1)
30 X% = RND(1) * X%
40 Y% = RND(1) * Y%
50 A%(X%, Y%) = -32767
60 PRINT A%(X%, Y%)
70 CLEAR
BASIC256
arraybase 1
input integer "Enter one positive integer: ", i
input integer "Enter other positive integer: ", j
dim a(i, j)
a[i, j] = i * j
print "a("; string(i); ","; string(j); ") = "; a[i, j]
end
QBasic
INPUT "Enter two positive integers, separated by a comma"; i, j
DIM array(1 TO i, 1 TO j)
array(i, j) = i * j
PRINT "a("; STR$(i); ","; STR$(j); " ) = "; array(i, j)
ERASE array
True BASIC
INPUT prompt "Enter two positive integers, separated by a comma ": i, j
DIM array(0,0)
MAT REDIM array(1 TO i, 1 TO j)
LET array(i, j) = i*j
PRINT "a("; STR$(i); ","; STR$(j); ") ="; array(i, j)
MAT REDIM array(0,0)
END
Yabasic
input "Enter one positive integer: " i
input "Enter other positive integer: " j
dim a(i, j)
a(i, j) = i * j
print "a(", str$(i), ",", str$(j), ") = ", a(i, j)
exit
BBC BASIC
INPUT "Enter array dimensions separated by a comma: " a%, b%
DIM array(a%, b%)
array(1, 1) = PI
PRINT array(1, 1)
Commodore BASIC
Note: Size of array may be limited by RAM availability in some Commodore machines.
10 print chr$(147);chr$(14);
15 print "Size of array:"
20 print "Columns (1-20)";:input x%
25 if x%<1 or x%>20 then print "Try again.":goto 20
30 print "Rows (1-20)";:input y%
35 if y%<1 or y%>20 then print "Try again.":goto 30
40 x%=x%-1:y%=y%-1:dim a$(x%,y%)
50 nx=int(rnd(1)*x%):ny=int(rnd(1)*y%)
60 a$(nx,ny)="X"
70 print "Element";nx;",";ny;"= '";a$(nx,ny);"'"
80 clr:rem clear variables from ram
- Output:
Size of array: Columns (1-20)? 10 Rows (1-20)? 10 Element 6 , 3 = 'X' ready. print a$(6,3) ready.
FreeBASIC
' FB 1.05.0 Win64
Dim As Integer i, j
Input "Enter two positive integers, separated by a comma"; i, j
Dim a(1 To i, 1 To j) As Integer
a(i, j) = i * j
Print "a("; Str(i); ","; Str(j); ") ="; a(i, j)
Erase a
Print
Print "Press any key to quit"
Sleep
- Output:
Enter two positive integers, separated by a comma? 4, 7 a(4,7) = 28
IS-BASIC
100 INPUT PROMPT "Enter array dimensions separated by a coma: ":A,B
110 NUMERIC ARRAY(1 TO A,1 TO B)
120 LET ARRAY(1,1)=PI
130 PRINT ARRAY(1,1)
Liberty BASIC
Arrays can hold numbers ( eg age( 100)( or strings ( eg name$( 100)) LB arrays can only be one or two dimensioned. If an array is not DIMensioned explicitly, then the array will be limited to 11 elements, 0 to 10. Non DIMensioned double subscript arrays will be limited to 100 elements 0 to 9 by 0 to 9. The DIM statement can be followed by a list of arrays to be dimensioned, separated by commas. REDIM redimensions an already dimensioned array and clears all elements to zero (or to an empty string in the case of string arrays). This can be very useful for writing applications that have data sets of unknown size. If you dimension arrays that are extra large to make sure you can hold data, but only have a small set of data, then all the space you reserved is wasted. This hurts performance, because memory is set aside for the number of elements in the DIM statement.
input "Enter first array dimension "; a
input "Enter second array dimension "; b
dim array( a, b)
array( 1, 1) = 123.456
print array( 1, 1)
end
PureBasic
If OpenConsole()
Define x, y
Print("Input X-Size: ")
x = Val(Input())
Print("Input Y-Size: ")
y = Val(Input())
Dim a(x,y) ; Should really check if x & y are larger then 1, but that would be less fun....
a(1,1)=Random(1000)
PrintN("a(1,1)= " + Str(a(1,1)) )
PrintN("Press ENTER to exit"):Input()
End ; Close down and let PureBasic delete the Console and all variables.
EndIf
QuickBASIC
CLS
INPUT a, b 'inputs need to be separated by commas
DIM array (1 TO a, 1 TO b)
array(1,1) = 42
PRINT array(1,1)
ERASE array
Run BASIC
print "Enter array 1 greater than 0"; : input a1
print "Enter array 2 greater than 0"; : input a2
dim chrArray$(max(a1,1),max(a2,1))
dim numArray(max(a1,1),max(a2,1))
chrArray$(1,1) = "Hello"
numArray(1,1) = 987.2
print chrArray$(1,1);" ";numArray(1,1)
Sinclair ZX81 BASIC
Arrays are indexed from 1; the only limit on their size (which may be an exigent limit) is the available memory. We create an array, write to a randomly selected element and then print it out, and finally use CLEAR
to destroy the array (and all the other variables in the program).
10 PRINT "1ST DIMENSION: ";
20 INPUT D1
30 PRINT D1
40 PRINT "2ND DIMENSION: ";
50 INPUT D2
60 PRINT D2
70 DIM A(D1,D1)
80 PRINT "ARRAY CREATED"
90 LET X=1+INT (D1*RND)
100 LET Y=1+INT (D2*RND)
110 LET A(X,Y)=37
120 PRINT "ITEM ";X;", ";Y;" = ";A(X,Y)
130 CLEAR
140 PRINT "ARRAY DESTROYED"
- Output:
1ST DIMENSION: 11 2ND DIMENSION: 6 ARRAY CREATED ITEM 7, 4 = 37 ARRAY DESTROYED
Sinclair ZX Spectrum BASIC
10 INPUT "Size? ";rows;"*";columns
20 DIM a(rows,columns): REM defines a numeric array
30 LET a=INT (RND*rows)+1: LET c=INT (RND*columns+1): REM the array is labelled a, but the letter a is still available for variable assignment
40 LET a(a,c)=1
50 PRINT a(a,c)
60 DIM a(1): REM arrays cannot be removed without CLEARing the entire variable space, but redimensioning them to 1 will save most of the space they used
TI-83 BASIC
Input "ROWS? ",R
Input "COLS? ",C
{R,C}→dim([A])
42→[A](1,1)
Disp [A](1,1)
DelVar [A]
Visual Basic .NET
Module Program
Sub Main()
Console.WriteLine("Enter two space-delimited integers:")
Dim input = Console.ReadLine().Split()
Dim rows = Integer.Parse(input(0))
Dim cols = Integer.Parse(input(1))
' VB uses max-index for array creation.
Dim arr(rows - 1, cols - 1) As Integer
arr(0, 0) = 2
Console.WriteLine(arr(0, 0))
End Sub
End Module
- Output:
Enter two space-delimited integers: 5 42 2
BQN
#!/usr/bin/env bqn
# Cut 𝕩 at occurrences of 𝕨, removing separators and empty segments
# (BQNcrate phrase).
Split ← (¬-˜⊢×·+`»⊸>)∘≠⊔⊢
# Natural number from base-10 digits (BQNcrate phrase).
Base10 ← 10⊸×⊸+˜´∘⌽
# Parse any number of space-separated numbers from string 𝕩.
ParseNums ← {Base10¨ -⟜'0' ' ' Split 𝕩}
# •GetLine is a nonstandard CBQN extension.
•Show ⥊⟜(↕×´) ParseNums •GetLine@
- Output:
4 2 ┌─ ╵ 0 1 2 3 4 5 6 7 ┘
C
C99
#include <stdio.h>
int main(int argc, char **argv) {
int user1 = 0, user2 = 0;
printf("Enter two integers. Space delimited, please: ");
scanf("%d %d",&user1, &user2);
int array[user1][user2];
array[user1/2][user2/2] = user1 + user2;
printf("array[%d][%d] is %d\n",user1/2,user2/2,array[user1/2][user2/2]);
return 0;
}
Traditional Style
Allocate multi-dimensional arrays with a single call to malloc. The demonstration code builds a rank 3 array.
/*
assume this file is c.c ,
compile and run on linux: cc -Wall -g -DCOMPILE_EXAMPLE c.c -lm -o c && ./c
*/
#include<stdlib.h>
#include<stdio.h>
static void error(int status, char*message) {
fprintf(stderr,"\n%s\n",message);
exit(status);
}
static void*dwlcalloc(int n,size_t bytes) {
void*rv = (void*)calloc(n,bytes);
if (NULL == rv)
error(1, "memory allocation failure");
return rv;
}
void*allocarray(size_t rank,size_t*shape,size_t itemSize) {
/*
Allocates arbitrary dimensional arrays (and inits all pointers)
with only 1 call to malloc. Lambert Electronics, USA, NY.
This is wonderful because one only need call free once to deallocate
the space. Special routines for each size array are not need for
allocation of for deallocation. Also calls to malloc might be expensive
because they might have to place operating system requests. One call
seems optimal.
*/
size_t size,i,j,dataSpace,pointerSpace,pointers,nextLevelIncrement;
char*memory,*pc,*nextpc;
if (rank < 2) {
if (rank < 0)
error(1,"invalid negative rank argument passed to allocarray");
size = rank < 1 ? 1 : *shape;
return dwlcalloc(size,itemSize);
}
pointerSpace = 0, dataSpace = 1;
for (i = 0; i < rank-1; ++i)
pointerSpace += (dataSpace *= shape[i]);
pointerSpace *= sizeof(char*);
dataSpace *= shape[i]*itemSize;
memory = pc = dwlcalloc(1,pointerSpace+dataSpace);
pointers = 1;
for (i = 0; i < rank-2; ) {
nextpc = pc + (pointers *= shape[i])*sizeof(char*);
nextLevelIncrement = shape[++i]*sizeof(char*);
for (j = 0; j < pointers; ++j)
*((char**)pc) = nextpc, pc+=sizeof(char*), nextpc += nextLevelIncrement;
}
nextpc = pc + (pointers *= shape[i])*sizeof(char*);
nextLevelIncrement = shape[++i]*itemSize;
for (j = 0; j < pointers; ++j)
*((char**)pc) = nextpc, pc+=sizeof(char*), nextpc += nextLevelIncrement;
return memory;
}
#ifdef COMPILE_EXAMPLE
#include<string.h>
#include<math.h>
#define Z 5
#define Y 10
#define X 39
#define BIND(A,L,H) ((L)<(A)?(A)<(H)?(A):(H):(L))
void p_char(void*pv) {
char s[3];
int i = 0;
s[i++] = ' ', s[i++] = *(char*)pv, s[i++] = 0;
fputs(s, stdout);
}
void display(void*a,size_t rank,size_t*shape,void(*f)(void*)) {
int i;
if (0 == rank)
(*f)(a);
else if (1 == rank) {
for (i = 0; i < *shape; ++i)
(*f)(a+i);
putchar('\n');
} else {
for (i = 0; i < *shape; ++i)
display(((void**)a)[i], rank-1, shape+1, f);
putchar('\n');
}
}
int main() { /* character cell 3D graphics. Whoot */
char***array;
float x,y,z;
size_t rank, shape[3], i, j, k;
rank = 0;
shape[rank++] = Z, shape[rank++] = Y, shape[rank++] = X;
array = allocarray(rank, shape, sizeof(char));
memset(**array, ' ', X*Y*Z*(sizeof(***array))); /* load the array with spaces */
for (i = 0; i < X; ++i) {
x = i/(float)X;
for (j = 0; j < Y; ++j) {
y = j/(float)X;
z = x*y*(4*M_PI);
z = 5.2*(0.5+(0.276765 - sin(z)*cos(z)*exp(1-z))/0.844087); /* a somewhat carefully designed silly function */
/* printf("%g %g %g\n", x, y, z); */
k = (int)z;
array[BIND(k, 0, Z-1)][j][i] = '@'; /* BIND ensures a valid index */
}
}
display(array, rank, shape, p_char);
puts("\nIt is what it is.");
free(array);
return EXIT_SUCCESS;
}
#endif
This style is supported by all 'C' compilers.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int user1 = 0, user2 = 0;
int *a1, **array, row;
printf("Enter two integers. Space delimited, please: ");
scanf("%d %d",&user1, &user2);
a1 = malloc(user1*user2*sizeof(int));
array = malloc(user1*sizeof(int*));
for (row=0; row<user1; row++) array[row]=a1+row*user2;
array[user1/2][user2/2] = user1 + user2;
printf("array[%d][%d] is %d\n",user1/2,user2/2,array[user1/2][user2/2]);
free(array);
free(a1);
return 0;
}
This style also supports more efficient memory utilization if you're only using a portion of the array. If you only need the upper right half of a square array, you can do something like the following.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int user1 = 0;
int space_needed;
int *a1, **array;
int row, col, offset;
printf("Enter size of array: ");
scanf("%d",&user1);
space_needed = (user1+1)*user1/2;
a1 = malloc(space_needed * sizeof(*a1));
array = malloc(user1 * sizeof(*array));
for (row=0,offset=0; row<user1; offset+=(user1-row), row++) {
array[row]=a1+offset-row;
for (col=row; col<user1; col++)
array[row][col] = 1+col-row;
}
for (row=0; row<user1; row++)
printf("%d ", array[row][user1-1]);
printf("\n");
free(array);
free(a1);
return 0;
}
This approach most closely matches the C99 example, as alloca allocates on the stack, rather than the heap, as malloc does.
#include <stdio.h>
#include <alloca.h>
int main(int argc, char **argv)
{
int user1 = 0, user2 = 0;
int *a1, **array, row;
printf("Enter two integers. Space delimited, please: ");
scanf("%d %d",&user1, &user2);
a1 = alloca(user1*user2*sizeof(int));
array = alloca(user1*sizeof(int*));
for (row=0; row<user1; row++) array[row]=a1+row*user2;
array[user1/2][user2/2] = user1 + user2;
printf("array[%d][%d] is %d\n",user1/2,user2/2,array[user1/2][user2/2]);
return 0;
}
C#
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter two integers. Space delimited please: ");
string s = Console.ReadLine();
int[,] myArray=new int[(int)s[0],(int)s[2]];
myArray[0, 0] = 2;
Console.WriteLine(myArray[0, 0]);
Console.ReadLine();
}
}
C++
With language built-in facilities
#include <iostream>
int main()
{
// read values
int dim1, dim2;
std::cin >> dim1 >> dim2;
// create array
double* array_data = new double[dim1*dim2];
double** array = new double*[dim1];
for (int i = 0; i < dim1; ++i)
array[i] = array_data + dim2*i;
// write element
array[0][0] = 3.5;
// output element
std::cout << array[0][0] << std::endl;
// get rid of array
delete[] array;
delete[] array_data;
return 0;
}
Using std::vector from the standard library
#include <iostream>
#include <vector>
int main()
{
// read values
int dim1, dim2;
std::cin >> dim1 >> dim2;
// create array
std::vector<std::vector<double> > array(dim1, std::vector<double>(dim2));
// write element
array[0][0] = 3.5;
// output element
std::cout << array[0][0] << std::endl;
// the array is automatically freed at the end of main()
return 0;
}
Using boost::multi_array
#include <iostream>
#include <boost/multi_array.hpp>
typedef boost::multi_array<double, 2> two_d_array_type;
int main()
{
// read values
int dim1, dim2;
std::cin >> dim1 >> dim2;
// create array
two_d_array_type A(boost::extents[dim1][dim2]);
// write element
A[0][0] = 3.1415;
// read element
std::cout << A[0][0] << std::endl;
return 0;
}
Using boost::uBLAS
#include <cstdlib>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
int main (const int argc, const char** argv) {
if (argc > 2) {
using namespace boost::numeric::ublas;
matrix<double> m(atoi(argv[1]), atoi(argv[2])); // build
for (unsigned i = 0; i < m.size1(); i++)
for (unsigned j = 0; j < m.size2(); j++)
m(i, j) = 1.0 + i + j; // fill
std::cout << m << std::endl; // print
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
Clean
import StdEnv
Start :: *World -> { {Real} }
Start world
# (console, world) = stdio world
(_, dim1, console) = freadi console
(_, dim2, console) = freadi console
= createArray dim1 (createArray dim2 1.0)
Clojure
(let [rows (Integer/parseInt (read-line))
cols (Integer/parseInt (read-line))
a (to-array-2d (repeat rows (repeat cols nil)))]
(aset a 0 0 12)
(println "Element at 0,0:" (aget a 0 0)))
CLU
prompt = proc (s: string) returns (int)
stream$puts(stream$primary_output(), s)
return(int$parse(stream$getl(stream$primary_input())))
end prompt
start_up = proc ()
po: stream := stream$primary_output()
% Ask for width and height
width: int := prompt("Width? ")
height: int := prompt("Height? ")
% Create an array of arrays.
% In order to actually create separate arrays, rather than repeating
% a reference to the same array over and over, fill_copy must be used.
arr: array[array[int]] :=
array[array[int]]$fill_copy(1, width, array[int]$fill(1, height, 0))
% Set a value
x: int := 1+width/2
y: int := 1+height/2
arr[x][y] := 123
% Retrieve the value
stream$putl(po, "arr[" || int$unparse(x) || "][" || int$unparse(y)
|| "] = " || int$unparse(arr[x][y]))
% The array will be automatically garbage-collected once there
% are no more references to it.
end start_up
- Output:
Width? 8 Height? 6 arr[5][4] = 123
Common Lisp
(let ((d1 (read))
(d2 (read)))
(assert (and (typep d1 '(integer 1))
(typep d2 '(integer 1)))
(d1 d2))
(let ((array (make-array (list d1 d2) :initial-element nil))
(p1 0)
(p2 (floor d2 2)))
(setf (aref array p1 p2) t)
(print (aref array p1 p2))))
The assert will allow the user to reenter the dimensions if they are not positive integers.
Component Pascal
Arrays in Component Pascal are started from zero index. No DISPOSE-like procedures because of garbage collection.
MODULE TestArray;
(* Implemented in BlackBox Component Builder *)
IMPORT Out;
(* Open array *)
PROCEDURE DoTwoDim*;
VAR d: POINTER TO ARRAY OF ARRAY OF INTEGER;
BEGIN
NEW(d, 5, 4); (* allocating array in memory *)
d[1, 2] := 100; (* second row, third column element *)
d[4, 3] := -100; (* fifth row, fourth column element *)
Out.Int(d[1, 2], 0); Out.Ln;
Out.Int(d[4, 3], 0); Out.Ln;
END DoTwoDim;
END TestArray.
Crystal
require "random"
first = gets.not_nil!.to_i32
second = gets.not_nil!.to_i32
arr = Array(Array(Int32)).new(first, Array(Int32).new second, 0)
random = Random.new
first = random.rand 0..(first - 1)
second = random.rand 0..(second - 1)
arr[first][second] = random.next_int
puts arr[first][second]
D
void main() {
import std.stdio, std.conv, std.string;
int nRow, nCol;
write("Give me the numer of rows: ");
try {
nRow = readln.strip.to!int;
} catch (StdioException) {
nRow = 3;
writeln;
}
write("Give me the numer of columns: ");
try {
nCol = readln.strip.to!int;
} catch (StdioException) {
nCol = 5;
writeln;
}
auto array = new float[][](nRow, nCol);
array[0][0] = 3.5;
writeln("The number at place [0, 0] is ", array[0][0]);
}
- Output:
Give me the numer of rows: Give me the numer of columns: The number at place [0, 0] is 3.5
Delphi
Dimensions are generated randomly, not input by user.
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
matrix:array of array of Byte;
i,j:Integer;
begin
Randomize;
//Finalization is not required in this case, but you have to do
//so when reusing the variable in scope
Finalize(matrix);
//Init first dimension with random size from 1..10
//Remember dynamic arrays are indexed from 0
SetLength(matrix,Random(10) + 1);
//Init 2nd dimension with random sizes too
for i := Low(matrix) to High(matrix) do
SetLength(matrix[i],Random(10) + 1);
//End of code, the following part is just output
Writeln(Format('Total amount of columns = %.2d',[Length(matrix)]));
for i := Low(matrix) to High(matrix) do
Writeln(Format('Column %.2d = %.2d rows',[i,Length(matrix[i])]));
Readln;
end.
Test run:
Total amount of columns = 10 Column 00 = 04 rows Column 01 = 08 rows Column 02 = 09 rows Column 03 = 05 rows Column 04 = 01 rows Column 05 = 04 rows Column 06 = 07 rows Column 07 = 04 rows Column 08 = 10 rows Column 09 = 02 rows
Elena
ELENA 5.0 :
Typified array
import extensions;
public program()
{
var n := new Integer();
var m := new Integer();
console.write:"Enter two space delimited integers:";
console.loadLine(n,m);
var myArray := class Matrix<int>.allocate(n,m);
myArray.setAt(0,0,2);
console.printLine(myArray.at(0, 0))
}
Jagged array
import system'routines;
import extensions;
public program()
{
auto n := new Integer();
auto m := new Integer();
console.write:"Enter two space delimited integers:";
console.loadLine(n,m);
auto myArray2 := new object[][](n.Value).populate:(int i => (new object[](m.Value)) );
myArray2[0][0] := 2;
myArray2[1][0] := "Hello";
console.printLine(myArray2[0][0]);
console.printLine(myArray2[1][0]);
}
Elixir
defmodule TwoDimArray do
def create(w, h) do
List.duplicate(0, w)
|> List.duplicate(h)
end
def set(arr, x, y, value) do
List.replace_at(arr, x,
List.replace_at(Enum.at(arr, x), y, value)
)
end
def get(arr, x, y) do
arr |> Enum.at(x) |> Enum.at(y)
end
end
width = IO.gets "Enter Array Width: "
w = width |> String.trim() |> String.to_integer()
height = IO.gets "Enter Array Height: "
h = height |> String.trim() |> String.to_integer()
arr = TwoDimArray.create(w, h)
arr = TwoDimArray.set(arr,2,0,42)
IO.puts(TwoDimArray.get(arr,2,0))
Erlang
-module( two_dimensional_array ).
-export( [create/2, get/3, set/4, task/0] ).
create( X, Y ) -> array:new( [{size, X}, {default, array:new( [{size, Y}] )}] ).
get( X, Y, Array ) -> array:get( Y, array:get(X, Array) ).
set( X, Y, Value, Array ) ->
Y_array = array:get( X, Array ),
New_y_array = array:set( Y, Value, Y_array ),
array:set( X, New_y_array, Array ).
task() ->
{ok, [X, Y]} = io:fread( "Input two integers. Space delimited, please: ", "~d ~d" ),
Array = create( X, Y ),
New_array = set( X - 1, Y - 1, X * Y, Array ),
io:fwrite( "In position ~p ~p we have ~p~n", [X - 1, Y - 1, get( X - 1, Y - 1, New_array)] ).
- Output:
7> two_dimensional_array:task(). Input two integers. Space delimited, please: 4 5 In position 3 4 we have 20
ERRE
In ERRE language arrays created at run-time is "dynamic arrays". For this task will be enough this code:
PROGRAM DYNAMIC
!$DYNAMIC
DIM A%[0,0]
BEGIN
PRINT(CHR$(12);) !CLS
INPUT("Subscripts",R%,C%)
!$DIM A%[R%,C%]
A%[2,3]=6
PRINT("Value in row";2;"and col";3;"is";A%[2,3])
END PROGRAM
You can redimension A% using pragmas: !$ERASE A% with a subsequent !$DIM A%[.,.]
Euphoria
include get.e
sequence array
integer height,width,i,j
height = floor(prompt_number("Enter height: ",{}))
width = floor(prompt_number("Enter width: ",{}))
array = repeat(repeat(0,width),height)
i = floor(height/2+0.5)
j = floor(width/2+0.5)
array[i][j] = height + width
printf(1,"array[%d][%d] is %d\n", {i,j,array[i][j]})
F#
open System
let width = int( Console.ReadLine() )
let height = int( Console.ReadLine() )
let arr = Array2D.create width height 0
arr.[0,0] <- 42
printfn "%d" arr.[0,0]
Factor
Factor doesn't provide any support for easy access of 2d arrays. But since factor's written in factor, we can just add it and it's just as good :)
USING: io kernel math.matrices math.parser prettyprint
sequences ;
IN: rosettacode.runtime2darray
: set-Mi,j ( elt {i,j} matrix -- )
[ first2 swap ] dip nth set-nth ;
: Mi,j ( {i,j} matrix -- elt )
[ first2 swap ] dip nth nth ;
: example ( -- )
readln readln [ string>number ] bi@ zero-matrix ! create the array
[ [ 42 { 0 0 } ] dip set-Mi,j ] ! set the { 0 0 } element to 42
[ [ { 0 0 } ] dip Mi,j . ] ! read the { 0 0 } element
bi ;
Fermat
?m; {imput the dimensions of the array}
?n;
Array a[m,n]; {generate an array of m rows and n columns}
a[m\2, n\2] := m+n-1; {put some value in one of the cells}
!!a[m\2, n\2]; {display that entry}
@[a]; {delete the array}
Forth
: cell-matrix
create ( width height "name" ) over , * cells allot
does> ( x y -- addr ) dup cell+ >r @ * + cells r> + ;
5 5 cell-matrix test
36 0 0 test !
0 0 test @ . \ 36
INTEGER DMATRIX my-matrix{{
& my-matrix{{ 8 9 }}malloc
8 my-matrix{{ 3 4 }} !
my-matrix{{ 3 4 }} @ .
& my-matrix{{ }}free
Fortran
In Fortran 90 and later
PROGRAM Example
IMPLICIT NONE
INTEGER :: rows, columns, errcheck
INTEGER, ALLOCATABLE :: array(:,:)
WRITE(*,*) "Enter number of rows"
READ(*,*) rows
WRITE(*,*) "Enter number of columns"
READ(*,*) columns
ALLOCATE (array(rows,columns), STAT=errcheck) ! STAT is optional and is used for error checking
array(1,1) = 42
WRITE(*,*) array(1,1)
DEALLOCATE (array, STAT=errcheck)
END PROGRAM Example
Fōrmulæ
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition.
Programs in Fōrmulæ are created/edited online in its website, However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
In this page you can see the program(s) related to this task and their results.
Frink
[rows, cols] = dims = eval[input["Enter dimensions: ", ["Rows", "Columns"]]]
a = new array[dims, 0] // Create and initialize to 0
a@(rows-1)@(cols-1) = 10
println[a@(rows-1)@(cols-1)]
GAP
# Creating an array of 0
a := NullMat(2, 2);
# [ [ 0, 0 ], [ 0, 0 ] ]
# Some assignments
a[1][1] := 4;
a[1][2] := 5;
a[2][1] := 3;
a[2][2] := 4;
a
# [ [ 4, 5 ], [ 3, 4 ] ]
Determinant(a);
# 1
Go
Arrays in Go are only one dimensional. Code below show the obvious way of composing a 2d array as an array of arrays that can be indexed as a[r][c].
package main
import "fmt"
func main() {
var row, col int
fmt.Print("enter rows cols: ")
fmt.Scan(&row, &col)
// allocate composed 2d array
a := make([][]int, row)
for i := range a {
a[i] = make([]int, col)
}
// array elements initialized to 0
fmt.Println("a[0][0] =", a[0][0])
// assign
a[row-1][col-1] = 7
// retrieve
fmt.Printf("a[%d][%d] = %d\n", row-1, col-1, a[row-1][col-1])
// remove only reference
a = nil
// memory allocated earlier with make can now be garbage collected.
}
The technique above alocates each row separately. This might be good if you need extremely large arrays that cannot be allocated in a single piece. It might be bad though, for locality, as there would be no guarantee that the separate allocations would be localized in memory. A technique that maintains locality is this,
// allocate composed 2d array
a := make([][]int, row)
e := make([]int, row * col)
for i := range a {
a[i] = e[i*col:(i+1)*col]
}
Now all rows are allocated with a single allocation. Alternatively, slice e can be used directly without going through slice a. Element r c can be accessed simply as e[r*cols+c] for example, or accessor functions can be defined such as,
func get(r, c int) int {
return e[r*cols+c]
}
Groovy
Solution:
def make2d = { nrows, ncols ->
(0..<nrows).collect { [0]*ncols }
}
Test:
def r = new Random()
System.in.splitEachLine(/,\s*/) { dim ->
def nrows = dim[0] as int
def ncols = dim[1] as int
def a2d = make2d(nrows, ncols)
def row = r.nextInt(nrows)
def col = r.nextInt(ncols)
def val = r.nextInt(nrows*ncols)
a2d[row][col] = val
println "a2d[${row}][${col}] == ${a2d[row][col]}"
a2d.each { println it }
println()
}
Input:
3, 5 4, 4
Output:
a2d[0][3] == 8 [0, 0, 0, 8, 0] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] a2d[2][2] == 5 [0, 0, 0, 0] [0, 0, 0, 0] [0, 0, 5, 0] [0, 0, 0, 0]
Haskell
import Data.Array
doit n m = a!(0,0) where a = array ((0,0),(n,m)) [((0,0),42)]
HicEst
REAL :: array(1)
DLG(NameEdit=rows, NameEdit=cols, Button='OK', TItle='Enter array dimensions')
ALLOCATE(array, cols, rows)
array(1,1) = 1.234
WRITE(Messagebox, Name) array(1,1)
Icon and Unicon
All Icon and Unicon data objects are automatically reclaimed. Multiply dimensioned arrays are arrays of arrays in both languages.
Sample output:
->ar 65 2 A[37][1] -> 3.141592654
IDL
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:'
read, y, prompt='Enter y size:'
d = fltarr(x,y)
d[3,4] = 5.6
print,d[3,4]
;==> outputs 5.6
delvar, d
J
The natural ways of creating a two dimensional array, from array dimensions, in J are i.
and $
array1=:i. 3 4 NB. a 3 by 4 array with arbitrary values
array2=: 5 6 $ 2 NB. a 5 by 6 array where every value is the number 2
To update the upper left corner of the array with the value 99, you might use }
array1=: 99 (<0 0)} array1
And, to retrieve that value you might use {
(<0 0) { array1
Finally, J manages storage for you, so to delete the array, you could either have the name refer to a new value
array1=: 0
or you could remove the name itself:
erase'array1'
Putting these ideas together and adding a few frills:
task=: verb define
assert. y -: 0 0 + , y NB. error except when 2 dimensions are specified
INIT=. 0 NB. array will be populated with this value
NEW=. 1 NB. we will later update one location with this value
ARRAY=. y $ INIT NB. here, we create our 2-dimensional array
INDEX=. < ? $ ARRAY NB. pick an arbitrary location within our array
ARRAY=. NEW INDEX} ARRAY NB. use our new value at that location
INDEX { ARRAY NB. and return the value from that location
)
Passing two integers to task (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.
Example use (result should always be 1 which is the value of NEW
):
task 99 99
1
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
'init new' =. 1r2;2r3 NB. fractions
'init new' =. a: ; <<'Rosetta' NB. boxes
Java
import java.util.Scanner;
public class twoDimArray {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int nbr1 = in.nextInt();
int nbr2 = in.nextInt();
double[][] array = new double[nbr1][nbr2];
array[0][0] = 42.0;
System.out.println("The number at place [0 0] is " + array[0][0]);
}
}
JavaScript
var width = Number(prompt("Enter width: "));
var height = Number(prompt("Enter height: "));
//make 2D array
var arr = new Array(height);
for (var i = 0; i < h; i++) {
arr[i] = new Array(width);
}
//set value of element
a[0][0] = 'foo';
//print value of element
console.log('arr[0][0] = ' + arr[0][0]);
//cleanup array
arr = void(0);
jq
jq data types are exactly those of JSON, so there are various alternatives for representing matrices.
One way to represent an m by n matrix is as an array of m arrays, one of which is of length n, and all of which have length less than or equal to n.
If M is such as array, then the syntax M[i][j] can be used to access the (i,j) element in the conventional sense, except that the index origin in jq is 0.
Note that the expression M | getpath([i,j]) would also access M[i][j].
To set the M[i][j] element to, say, e, one can use the idiom:
M | setpath([i,j]; e)
Here's a simple example.
# A function to create an m x n matrix
# filled with the input element
def matrix(m;n):
. as $init
| ( [ range(0; n + 1) ] | map($init)) as $row
| ( [ range(0; m + 1) ] | map($row))
;
# Task: create a matrix with dimensions specified by the user
# and set the [1,2] element:
(0 | matrix($m|tonumber; $n|tonumber)) | setpath([1,2]; 99)
If the above is in a file, say 2d.jq, the invocation:
jq -n -c --arg m 2 --arg n 3 -f 2d.jq
would produce:
[[0,0,0,0],[0,0,99,0],[0,0,0,0]]
Julia
Julia supports n-dimensional arrays as native data types: `Array{T, N}`, where `T` is the type of it´s elements and `N` is the number of dimensions.
function input(prompt::AbstractString)
print(prompt)
return readline()
end
n = input("Upper bound for dimension 1: ") |>
x -> parse(Int, x)
m = input("Upper bound for dimension 2: ") |>
x -> parse(Int, x)
x = rand(n, m)
display(x)
x[3, 3] # overloads `getindex` generic function
x[3, 3] = 5.0 # overloads `setindex!` generic function
x::Matrix # `Matrix{T}` is an alias for `Array{T, 2}`
x = 0; gc() # Julia has no `del` command, rebind `x` and call the garbage collector
Manually calling the garbage collector may or may not actually collect the array, but it will be eventually.
Kotlin
Program arguments provide dimensions of the array (4 5 in the example).
fun main(args: Array<String>) {
// build
val dim = arrayOf(10, 15)
val array = Array(dim[0], { IntArray(dim[1]) } )
// fill
array.forEachIndexed { i, it ->
it.indices.forEach { j ->
it[j] = 1 + i + j
}
}
// print
array.forEach { println(it.asList()) }
}
- Output:
[1, 2, 3, 4, 5] [2, 3, 4, 5, 6] [3, 4, 5, 6, 7] [4, 5, 6, 7, 8]
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
Lua
function multiply(n, a, b) if a <= b then return n, multiply(n, a + 1, b) end end
a, b = io.read() + 0, io.read() + 0
matrix = {multiply({multiply(1, 1, b)}, 1, a)}
matrix[a][b] = 5
print(matrix[a][b])
print(matrix[1][1])
M2000 Interpreter
Module CheckArray {
Do {
Input "A, B=", A% ,B%
} Until A%>0 and B%>0
\\ 1@ is 1 Decimal
addone=lambda N=1@ ->{=N : N++}
Dim Base 1, Arr(A%,B%)<<addone()
\\ pi also is decimal
Arr(1,1)=pi
Print Arr(1,1)
Print Arr()
\\ all variables/arrays/inner functions/modules erased now
}
CheckArray
Maple
This hardly covers the richness and complexity of arrays in Maple, but here goes:
> a := Array( 1 .. 3, 1 .. 4 ): # initialised to 0s
> a[1,1] := 1: # assign an element
> a[2,3] := 4: # assign an element
> a; # display the array
[1 0 0 0]
[ ]
[0 0 4 0]
[ ]
[0 0 0 0]
> a := 'a': # unassign the name
> gc(); # force a garbage collection; may or may not actually collect the array, but it will be eventually
Mathematica/Wolfram Language
arrayFun[m_Integer,n_Integer]:=Module[{array=ConstantArray[0,{m,n}]},
array[[1,1]]=RandomReal[];
array[[1,1]]
]
MATLAB / Octave
width = input('Array Width: ');
height = input('Array Height: ');
array = zeros(width,height);
array(1,1) = 12;
disp(['Array element (1,1) = ' num2str(array(1,1))]);
clear array; % de-allocate (remove) array from workspace
Sample Output:
Array Width: 18
Array Height: 12
Array element (1,1) = 12
Maxima
printf(true, "in the following terminate every number with semicolon `;'")$
n: readonly("Input x-size: ")$
m: readonly("Input y-size: ")$
a: make_array(fixnum, n, m)$
fillarray(a, makelist(i, i, 1, m*n))$
/* indexing starts from 0 */
print(a[0,0]);
print(a[n-1,m-1]);
MAXScript
a = getKBValue prompt:"Enter first dimension:"
b = getKBValue prompt:"Enter second dimension:"
arr1 = #()
arr2 = #()
arr2[b] = undefined
for i in 1 to a do
(
append arr1 (deepCopy arr2)
)
arr1[a][b] = 1
print arr1[a][b]
MUMPS
This example uses a two level tree to mimic an 2D array.
ARA2D
NEW X,Y,A,I,J
REARA
WRITE !,"Please enter two positive integers"
READ:10 !,"First: ",X
READ:10 !,"Second: ",Y
GOTO:(X\1'=X)!(X<0)!(Y\1'=Y)!(Y<0) REARA
FOR I=1:1:X FOR J=1:1:Y SET A(I,J)=I+J
WRITE !,"The corner of X and Y is ",A(X,Y)
KILL X,Y,A,I,J
QUIT
NetRexx
Note: No attempt is made to validate the input. Any errors will be handled as exceptions by the underlying JVM.
/* NetRexx */
options replace format comments java crossref symbols nobinary
say "give me the X and Y dimensions as two positive integers:"
parse ask xDim yDim
xPos = xDim % 2 -- integer divide to get close to the middle of the array
yPos = yDim % 2
arry = Rexx[xDim, yDim]
arry[xPos, yPos] = xDim / yDim -- make up a value...
say "arry["xPos","yPos"]:" arry[xPos, yPos]
return
Output:
give me the X and Y dimensions as two positive integers: 1250 1777 arry[625,888]: 0.703432752
Nim
import strutils, rdstdin
let
w = readLineFromStdin("Width: ").parseInt()
h = readLineFromStdin("Height: ").parseInt()
# Create the rows.
var s = newSeq[seq[int]](h)
# Create the columns.
for i in 0 ..< h:
s[i].newSeq(w)
# Store a value in an element.
s[0][0] = 5
# Retrieve and print it.
echo s[0][0]
# The allocated memory is freed by the garbage collector.
Objeck
use IO;
bundle Default {
class TwoDee {
function : Main(args : System.String[]) ~ Nil {
DoIt();
}
function : native : DoIt() ~ Nil {
Console->GetInstance()->Print("Enter x: ");
x := Console->GetInstance()->ReadString()->ToInt();
Console->GetInstance()->Print("Enter y: ");
y := Console->GetInstance()->ReadString()->ToInt();
if(x > 0 & y > 0) {
array : Int[,] := Int->New[x, y];
array[0, 0] := 2;
array[0, 0]->PrintLine();
};
}
}
}
Objective-C
Being Objective-C derivated from C, the C solution works fine in Objective-C too.
The "OpenStep" frameworks (GNUstep, Cocoa) does not provide a class for multidimensional array; of course it can be implemented in several way (also as a wrapper for the plain C way of handling arrays). Here I show a straightforward use of the NSMutableArray class.
#import <Foundation/Foundation.h>
int main()
{
@autoreleasepool {
int num1, num2;
scanf("%d %d", &num1, &num2);
NSLog(@"%d %d", num1, num2);
NSMutableArray *arr = [NSMutableArray arrayWithCapacity: (num1*num2)];
// initialize it with 0s
for(int i=0; i < (num1*num2); i++) [arr addObject: @0];
// replace 0s with something more interesting
for(int i=0; i < num1; i++) {
for(int j=0; j < num2; j++) {
arr[i*num2+j] = @(i*j);
}
}
// access a value: i*num2+j, where i,j are the indexes for the bidimensional array
NSLog(@"%@", arr[1*num2+3]);
}
return 0;
}
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 ();;
or using the module Bigarray:
let nbr1 = read_int ();;
let nbr2 = read_int ();;
let arr = Bigarray.Array2.create Bigarray.float32 Bigarray.c_layout nbr1 nbr2 ;;
arr.{0,0} <- 3.5;;
print_float arr.{0,0}; print_newline ();;
ooRexx
ooRexx arrays can be created with up to 999,999,999 dimensions...assuming you have enough memory to do so. Actually it's the 'size' of the array that's limited (the product of the dimensions).
Say "enter first dimension"
pull d1
say "enter the second dimension"
pull d2
a = .array~new(d1, d2)
a[1, 1] = "Abc"
say a[1, 1]
say d1 d2 a[d1,d2]
say a[10,10]
max=1000000000
b = .array~new(max,max)
- Output:
D:\>rexx 2d enter first dimension 3 enter the second dimension 5 Abc 3 5 The NIL object The NIL object *-* Compiled method NEW with scope "Array" 11 *-* b = .array~new(max,max) Error 93 running D:\2d.rex line 11: Incorrect call to method Error 93.959: An array cannot contain more than 99,999,999 elements
Oz
Oz does not have multi-dimensional arrays. But we can create an array of arrays (similarly to most examples on this page):
declare
%% Read width and height from stdin
class TextFile from Open.file Open.text end
StdIn = {New TextFile init(name:stdin)}
Width = {String.toInt {StdIn getS($)}}
Height = {String.toInt {StdIn getS($)}}
%% create array
Arr = {Array.new 1 Width unit}
in
for X in 1..Width do
Arr.X := {Array.new 1 Height 0}
end
%% set and read element
Arr.1.1 := 42
{Show Arr.1.1}
PARI/GP
tmp(m,n)={
my(M=matrix(m,n,i,j,0));
M[1,1]=1;
M[1,1]
};
Pascal
The following code is standard Extended Pascal (tested with gpc --extended-pascal):
program array2d(input, output);
type
tArray2d(dim1, dim2: integer) = array[1 .. dim1, 1 .. dim2] of real;
pArray2D = ^tArray2D;
var
d1, d2: integer;
data: pArray2D;
begin
{ read values }
readln(d1, d2);
{ create array }
new(data, d1, d2);
{ write element }
data^[1,1] := 3.5;
{ output element }
writeln(data^[1,1]);
{ get rid of array }
dispose(data);
end.
Perl
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($ $){
# get array sizes from provided params, but force numeric value
my $x = ($_[0] =~ /^\d+$/) ? shift : 0;
my $y = ($_[0] =~ /^\d+$/) ? shift : 0;
# define array, then add multi-dimensional elements
my @array;
$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[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
foreach my $dy (0 .. $y){
foreach my $dx (0 .. $x){
(defined $array[$dy][$dx]) ? (print $array[$dy][$dx]) : (print '. ');
}
print "\n";
}
}
The above is a bit verbose, here is a simpler implementation:
sub array {
my ($x, $y) = @_;
map {[ (0) x $x ]} 1 .. $y
}
my @square = array 3, 3;
# everything above this line is mostly redundant in perl,
# since perl would have created the array automatically when used.
# however, the above function initializes the array elements to 0,
# while perl would have used undef
#
# $cube[3][4][5] = 60 # this is valid even if @cube was previously undefined
$square[1][1] = 1;
print "@$_\n" for @square;
> 0 0 0
> 0 1 0
> 0 0 0
Phix
Obviously the sigificant part here is the three lines beginning "sequence array", the rest is all just boilerplate gui code.
-- demo\rosetta\Create2Darray.exw with javascript_semantics -- (layout/spacing leaves a little to be desired...) include pGUI.e Ihandle lab, tab, res, dlg function valuechanged_cb(Ihandle tab) string s = IupGetAttribute(tab,"VALUE") sequence r = scanf(s,"%d %d") if length(r)=1 then integer {height,width} = r[1], i = floor(height/2+0.5), j = floor(width/2+0.5) if i>0 and j>0 then sequence array = repeat(repeat(0,width),height) array[i][j] = height + width s = sprintf("array[%d][%d] is %d\n", {i,j,array[i][j]}) IupSetStrAttribute(res,"TITLE",s) IupRefresh(res) end if end if return IUP_DEFAULT end function procedure main() IupOpen() lab = IupLabel("Enter two numbers (>0) separated by a space") tab = IupText("VALUECHANGED_CB", Icallback("valuechanged_cb"),"EXPAND=HORIZONTAL") res = IupLabel("") dlg = IupDialog(IupVbox({IupHbox({lab,tab},"GAP=10,NORMALIZESIZE=VERTICAL"), IupHbox({res})},"MARGIN=5x5"),`TITLE="Create 2D array"`) IupShow(dlg) if platform()!=JS then IupMainLoop() IupClose() end if end procedure main()
The distributed version contains the older and simpler but console-only code.
Phixmonti
include ..\Utilitys.pmt
"Enter height: " input tonum nl
"Enter width: " input tonum nl
0 swap repeat swap repeat /# create two dimensional array/list. All zeroes #/
-1 get 99 -1 set -1 set /# set the last element o last dimension #/
pstack /# show the content of the stack #/
-1 get -1 get "Value of the last element of the last dimension: " print print drop
drop /# remove array/list from the stack #/
With syntactic sugar
include ..\Utilitys.pmt
"Enter height: " input tonum nl
"Enter width: " input tonum nl
0 swap repeat swap repeat /# create two dimensional array/list. All zeroes #/
-1 get 99 ( -1 -1 ) mset /# set the last element o last dimension #/
pstack /# show the content of the stack #/
( -1 -1 ) mget "Value of the last element of the last dimension: " print print drop
drop /# remove array/list from the stack #/
With more syntactic sugar
include ..\Utilitys.pmt
"Enter height: " input tonum nl
"Enter width: " input tonum nl
0 swap repeat swap repeat /# create two dimensional array/list. All zeroes #/
99 ( -1 -1 ) sset /# set the last element o last dimension #/
pstack /# show the content of the stack #/
( -1 -1 ) sget "Value of the last element of the last dimension: " print print
drop /# remove array/list from the stack #/
Picat
import util.
go =>
print("Input the number of rows and columns: "),
[Rows,Cols]=split(read_line()).map(to_int),
X=new_array(Rows,Cols),
X[1,1] = Rows*Cols+1,
println(X[1,1]).
- Output:
Input the number of rows and columns: 10 20 201
PicoLisp
(de 2dimTest (DX DY)
(let A (make (do DX (link (need DY))))
(set (nth A 3 3) 999) # Set A[3][3] to 999
(mapc println A) # Print all
(get A 3 3) ) ) # Return A[3][3]
(2dimTest 5 5)
Output:
(NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL) (NIL NIL 999 NIL NIL) (NIL NIL NIL NIL NIL) (NIL NIL NIL NIL NIL) -> 999
PL/I
/* First way using a controlled variable: */
declare A(*,*) float controlled;
get list (m, n);
allocate A(m,n);
get list (A);
put skip list (A);
/* The array remains allocated until the program terminates, */
/* or until explicitly destroyed using a FREE statement. */
free A;
6.00000E+0000 5.00000E+0000 4.00000E+0000 3.00000E+0000 2.00000E+0000
1.00000E+0000
/* Second way using a BEGIN block: */
get list (m, n);
begin;
declare A(m, n) float;
get list (A);
put skip list (A);
end;
/* The array is automatically destroyed when the block terminates. */
1.00000E+0000 2.00000E+0000 3.00000E+0000 4.00000E+0000 5.00000E+0000
6.00000E+0000 7.00000E+0000 8.00000E+0000 9.00000E+0000 1.00000E+0001
1.10000E+0001 1.20000E+0002
/* Third way using a PROCEDURE block: */
get list (m, n);
call S (m, n);
S: procedure (m, n);
declare A(m, n) float;
get list (A);
put skip list (A);
end S;
/* The array is automatically destroyed when the procedure terminates. */
1.00000E+0000 2.00000E+0000 3.00000E+0000 4.00000E+0000 5.00000E+0000
6.00000E+0000 7.00000E+0000 8.00000E+0000 9.00000E+0000 1.00000E+0001
1.10000E+0001 1.20000E+0001 1.30000E+0001 1.40000E+0001 1.50000E+0001
1.60000E+0001 1.70000E+0001 1.80000E+0001 1.90000E+0001 2.00000E+0001
Pop11
vars itemrep;
incharitem(charin) -> itemrep;
;;; Read sizes
vars n1 = itemrep(), n2= itemrep();
;;; Create 0 based array
vars ar = newarray([0 ^(n1 - 1) 0 ^(n2 - 1)], 0);
;;; Set element value
15 -> ar(0, 0);
;;; Print element value
ar(0,0) =>
;;; Make sure array is unreferenced
0 -> ar;
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 arrays may have arbitrary lower bounds, since we are given only size we create 0 based array.
PowerShell
function Read-ArrayIndex ([string]$Prompt = "Enter an integer greater than zero")
{
[int]$inputAsInteger = 0
while (-not [Int]::TryParse(([string]$inputString = Read-Host $Prompt), [ref]$inputAsInteger))
{
$inputString = Read-Host "Enter an integer greater than zero"
}
if ($inputAsInteger -gt 0) {return $inputAsInteger} else {return 1}
}
$x = $y = $null
do
{
if ($x -eq $null) {$x = Read-ArrayIndex -Prompt "Enter two dimensional array index X"}
if ($y -eq $null) {$y = Read-ArrayIndex -Prompt "Enter two dimensional array index Y"}
}
until (($x -ne $null) -and ($y -ne $null))
$array2d = New-Object -TypeName 'System.Object[,]' -ArgumentList $x, $y
- Output:
Enter two dimensional array index X: 6 Enter two dimensional array index Y: 6
Populate the array:
[int]$k = 1
for ($i = 0; $i -lt 6; $i++)
{
0..5 | ForEach-Object -Begin {$k += 10} -Process {$array2d[$i,$_] = $k + $_}
}
This is the entire array:
for ($i = 0; $i -lt 6; $i++)
{
"{0}`t{1}`t{2}`t{3}`t{4}`t{5}" -f (0..5 | ForEach-Object {$array2d[$i,$_]})
}
- Output:
11 12 13 14 15 16 21 22 23 24 25 26 31 32 33 34 35 36 41 42 43 44 45 46 51 52 53 54 55 56 61 62 63 64 65 66
Get an element of the array:
$array2d[2,2]
- Output:
33
Python
width = int(raw_input("Width of myarray: "))
height = int(raw_input("Height of Array: "))
myarray = [[0] * width for i in range(height)]
myarray[0][0] = 3.5
print (myarray[0][0])
Note: Some people may instinctively try to write myarray as [[0] * width] * height, but the * operator creates n references to [[0] * width]
You can also use a two element tuple to index a dictionary like so:
myarray = {(w,h): 0 for w in range(width) for h in range(height)}
# or, in pre 2.7 versions of Python: myarray = dict(((w,h), 0) for w in range(width) for h in range(height))
myarray[(0,0)] = 3.5
print (myarray[(0,0)])
Quackery
[ witheach peek ] is {peek} ( { p --> x )
[ dip dup
witheach [ peek dup ]
drop ] is depack ( { p --> * )
[ reverse
witheach
[ dip swap poke ] ] is repack ( * p --> { )
[ dup dip
[ rot dip
[ depack drop ] ]
repack ] is {poke} ( x { p --> { )
[ 0 swap of
nested swap of ] is 2array ( n n --> [ )
$ "Array width (at least 2): " input $->n drop
$ "Array length (at least 5): " input $->n drop
say "Creating " over echo say " by "
dup echo say " array." cr
2array
say "Writing 12345 to element {1,4} of array." cr
12345 swap ' [ 1 4 ] {poke}
say "Reading element {1,4} of array: "
' [ 1 4 ] {peek} echo
- Output:
Array width (at least 2): 2 Array length (at least 5): 5 Creating 2 by 5 array. Writing 12345 to element {1,4} of array. Reading element {1,4} of array: 12345
R
input <- readline("Enter two integers. Space delimited, please: ")
dims <- as.numeric(strsplit(input, " ")[[1]])
arr <- array(dim=dims)
ii <- ceiling(dims[1]/2)
jj <- ceiling(dims[2]/2)
arr[ii, jj] <- sum(dims)
cat("array[", ii, ",", jj, "] is ", arr[ii, jj], "\n", sep="")
Racket
Using a vector of vectors to represent arrays:
#lang racket
(printf "Enter XY dimensions: ")
(define xy (cons (read) (read)))
(define array (for/vector ([x (car xy)]) (for/vector ([y (cdr xy)]) 0)))
(printf "Enter a number for the top-left: ")
(vector-set! (vector-ref array 0) 0 (read))
(printf "Enter a number for the bottom-right: ")
(vector-set! (vector-ref array (sub1 (car xy))) (sub1 (cdr xy)) (read))
array
Output:
Enter XY dimensions: 3 3 Enter a number for the top-left: 1 Enter a number for the bottom-right: 9 '#(#(1 0 0) #(0 0 0) #(0 0 9))
Raku
(formerly Perl 6)
Line 1: The input parse doesn't care how you separate the dimensions as long as there are two distinct numbers.
Line 2: The list replication operator xx will automatically thunkify its left side so this produces new subarrays for each replication.
Line 3: Subscripting with a closure automatically passes the size of the dimension to the closure, so we pick an appropriate random index on each level.
Line 4: Print each line of the array.
my ($major,$minor) = prompt("Dimensions? ").comb(/\d+/);
my @array = [ '@' xx $minor ] xx $major;
@array[ *.rand ][ *.rand ] = ' ';
.say for @array;
Typical run:
Dimensions? 5x35
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
The most recent versions of Rakudo have preliminary support for 'shaped arrays'. Natively shaped arrays are a flexible feature for declaring typed, potentially multi-dimensional arrays, potentially with pre-defined dimensions. They will make memory-efficient matrix storage and matrix operations possible.
my ($major,$minor) = +«prompt("Dimensions? ").comb(/\d+/);
my Int @array[$major;$minor] = (7 xx $minor ) xx $major;
@array[$major div 2;$minor div 2] = 42;
say @array;
Typical run:
Dimensions? 3 x 10
[[7 7 7 7 7 7 7 7 7 7] [7 7 7 7 7 42 7 7 7 7] [7 7 7 7 7 7 7 7 7 7]]
Red
Red ["Create two-dimensional array at runtime"]
width: to-integer ask "What is the width of the array? "
height: to-integer ask "What is the height of the array? "
; 2D arrays are just nested blocks in Red.
matrix: copy [] ; Make an empty block to hold our rows.
loop height [ ; A loop for each row...
row: append/dup copy [] 0 width ; Create a block like [0 0 0 0] if width is 4.
append/only matrix row ; Append the row to our matrix as its own block.
]
a: 3
b: 2
matrix/2/4: 27 ; use path syntax to access or assign
matrix/1/1: 99 ; series are 1-indexed in Red; there is no matrix/0/0
matrix/(a)/(a): 10 ; accessing elements with words requires special care
matrix/:b/:b: 33 ; alternative
print mold matrix
- Output:
What is the width of the array? 5 What is the height of the array? 3 [[99 0 0 0 0] [0 33 0 27 0] [0 0 10 0 0]]
REXX
/*REXX program allocates/populates/displays a two-dimensional array. */
call bloat /*the BLOAT procedure does all allocations.*/
/*no more array named @ at this point. */
exit /*stick a fork in it, we're all done honey.*/
/*─────────────────────────BLOAT subroutine─────────────────────────────*/
bloat: procedure; say /*"PROCEDURE" makes this a ··· procedure. */
say 'Enter two positive integers (a 2-dimensional array will be created).'
pull n m . /*elements are allocated as they're defined*/
/*N and M should be verified at this point.*/
@.=' · ' /*Initial value for all @ array elements,*/
/*this ensures every element has a value.*/
do j =1 for n /*traipse through the first dimension [N]*/
do k=1 for m /* " " " second " [M]*/
if random()//7==0 then @.j.k=j'~'k /*populate every 7th random*/
end /*k*/
end /*j*/
/* [↓] display array to console: row,col */
do r=1 for n; _= /*construct one row (or line) at a time. */
do c=1 for m /*construct row one column at a time. */
_=_ right(@.r.c,4) /*append a nice-aligned column to the line.*/
end /*kk*/ /* [↑] an nicely aligned line is built. */
say _ /*display one row at a time to the terminal*/
end /*jj*/
/*╔════════════════════════════════════════════════════════════════════╗
║ When the RETURN is executed (from a PROCEDURE in this case), and ║
║ array @ is "de─allocated", that is, it's no longer defined, and ║
║ the array's storage is now free for other REXX variables. If the ║
║ BLOAT subroutine didn't have a "PROCEDURE" on that statement,║
║ the array @ would've been left intact. The same effect is ║
║ performed by a DROP statement (an example is shown below). ║
╚════════════════════════════════════════════════════════════════════╝*/
drop @. /*because of the PROCEDURE statement, the*/
return /* [↑] DROP statement is superfluous. */
output when the following input is entered after the prompt message: 30 15
· · · · · · · · · · · 1~12 · · · · · · · · · · · · · · · 2~13 · · · 3~2 · · · · · · · · 3~11 · · · 3~15 · · · · · 4~6 · · · · · · · · · · 5~2 · · · · · · 5~9 · · · · · · · · · 6~4 · · · · · · · · · · · · 7~2 7~3 · · · · · · · 7~11 · · 7~14 · · · 8~3 · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · 10~8 · · · 0~12 · · · · · · · · · 11~7 · · · · · · · · 12~1 · · · · · · · · · · · · · 2~15 · · · · · · · · · · · · 3~13 · · 14~1 · · · · 14~6 · · · · · · · 4~14 · 15~1 15~2 · · · · · · · · · 5~12 · · · 16~1 · · · · · · · · · · · · · · · · · · · · · · · 7~10 · 7~12 · 7~14 · · · · · · · · · · · 8~11 · · · · · · · · · 19~6 · · · · 9~11 · · · · 20~1 · · · · · · 20~8 · · 0~11 · · · · · · · · · · · · · · · · · · · · · · · · 22~6 · · · · · · · 2~14 · · · · · · · · 23~8 · · · · · · · · · · · 24~5 24~6 · · · · · · 4~13 · · · · · · · · · · · 5~10 · · · · · · · · · · · · · · · · · · · · 27~1 · 27~3 · · · · · · · · · · · 7~15 · · · · · · · · · · · · · · 8~15 · · · · · · · · · · · · · · · · · · · · 30~6 · 30~8 30~9 · · · · · ·
Ring
See 'Enter width : ' give width
See 'Enter height : ' give height
width=0+width height=0+height
aList = list(height) for x in aList x = list(width) next
aList[1][2] = 10 See aList[1][2] + nl
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]
Rust
use std::env;
fn main() {
let mut args = env::args().skip(1).flat_map(|num| num.parse());
let rows = args.next().expect("Expected number of rows as first argument");
let cols = args.next().expect("Expected number of columns as second argument");
assert_ne!(rows, 0, "rows were zero");
assert_ne!(cols, 0, "cols were zero");
// Creates a vector of vectors with all elements initialized to 0.
let mut v = vec![vec![0; cols]; rows];
v[0][0] = 1;
println!("{}", v[0][0]);
}
Scala
object Array2D{
def main(args: Array[String]): Unit = {
val x = Console.readInt
val y = Console.readInt
val a=Array.fill(x, y)(0)
a(0)(0)=42
println("The number at (0, 0) is "+a(0)(0))
}
}
Scheme
Pure R7RS
There is no two-dimensional array-type built in to Scheme, but there is a vector.
(import (scheme base)
(scheme read)
(scheme write))
;; Read x/y from user
(define x (begin (display "X: ") (flush-output-port) (read)))
(define y (begin (display "Y: ") (flush-output-port) (read)))
;; Create a vector, and fill it with a vector for each row
(define arr (make-vector x))
(do ((i 0 (+ 1 i)))
((= i x) )
(vector-set! arr i (make-vector y 0)))
;; set element (x/2, y/2) to 3
(vector-set! (vector-ref arr (floor (/ x 2)))
(floor (/ y 2))
3)
(display arr) (newline)
(display "Retrieved: ")
(display (vector-ref (vector-ref arr (floor (/ x 2)))
(floor (/ y 2))))
(newline)
- Output:
X: 3 Y: 5 #(#(0 0 0 0 0) #(0 0 3 0 0) #(0 0 0 0 0)) Retrieved: 3
Using a standard library
There are SRFI libraries providing arrays. This example uses SRFI 63.
(import (except (scheme base) equal?)
(scheme read)
(scheme write)
(srfi 63) ; an array SRFI
)
;; Read x/y from user
(define x (begin (display "X: ") (flush-output-port) (read)))
(define y (begin (display "Y: ") (flush-output-port) (read)))
;; Create an array
(define array (make-array #(0) x y))
;; Write to middle element of the array
(array-set! array 3 (floor (/ x 2)) (floor (/ y 2)))
;; Retrieve and display result
(display (array-ref array (floor (/ x 2)) (floor (/ y 2)))) (newline)
- Output:
X: 3 Y: 5 3
The array will be destroyed by the garbage collector, when it is no longer needed.
Seed7
$ include "seed7_05.s7i";
const proc: main is func
local
var integer: numRows is 0;
var integer: numCols is 0;
var array array integer: anArray is 0 times 0 times 0;
begin
write("Give me the numer of rows: ");
readln(numRows);
write("Give me the numer of columns: ");
readln(numCols);
anArray := numRows times numCols times 0;
anArray[1][1] := 3;
writeln("The number at place [1, 1] is " <& anArray[1][1]);
end func;
Output:
Give me the numer of rows: 5 Give me the numer of columns: 7 The number at place [1, 1] is 3
Sidef
func make_matrix(x, y) {
y.of { x.of(0) };
}
var y = Sys.scanln("rows: ").to_i;
var x = Sys.scanln("cols: ").to_i;
var matrix = make_matrix(x, y); # create the matrix
matrix[y/2][x/2] = 1; # write something inside it
say matrix; # display the matrix
- Output:
rows: 3 cols: 4 [[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]]
Simula
BEGIN
INTEGER N,M;
M := ININT;
N := ININT;
BEGIN
INTEGER ARRAY A(1:M,1:N);
A(M,N) := 99;
OUTINT(A(M,N),0);
OUTIMAGE;
END;
! ARRAY A OUT OF SCOPE ;
END.
- Input:
10 20
- Output:
99
Smalltalk
m := (FillInTheBlankMorph request: 'Number of rows?') asNumber.
n := (FillInTheBlankMorph request: 'Number of columns?') asNumber.
aMatrix := Matrix rows: m columns: n.
aMatrix
at: (aMatrix rowCount // 2)
at: (aMatrix columnCount // 2)
put: 3.4.
e := aMatrix
at: (aMatrix rowCount // 2)
at: (aMatrix columnCount // 2).
Transcript show: 'Entry is', e printString.
Smalltalk has no problems in creating objects at runtime. I haven't found a class for multidimensional array in the standard library, so let us suppose to have a class named MultidimensionalArray.
|num1 num2 arr|
num1 := stdin nextLine asInteger.
num2 := stdin nextLine asInteger.
arr := MultidimensionalArray new: { num1. num2 }.
1 to: num1 do: [ :i |
1 to: num2 do: [ :j |
arr at: { i. j } put: (i*j)
]
].
1 to: num1 do: [ :i |
1 to: num2 do: [ :j |
(arr at: {i. j}) displayNl
]
].
A possible implementation for a BidimensionalArray class is the following (changing Multi into Bi and using this class, the previous code runs fine):
Object subclass: BidimensionalArray [
|biArr|
<comment: 'bidim array'>
].
BidimensionalArray class extend [
new: biDim [ |r|
r := super new.
r init: biDim.
^ r
]
].
BidimensionalArray extend [
init: biDim [
biArr := Array new: (biDim at: 1).
1 to: (biDim at: 1) do: [ :i |
biArr at: i put: (Array new: (biDim at: 2))
].
^ self
]
at: biDim [
^ (biArr at: (biDim at: 1)) at: (biDim at: 2)
]
at: biDim put: val [
^ (biArr at: (biDim at: 1)) at: (biDim at: 2) put: val
]
].
Instead of implementing such a class (or the MultidimensionalArray one), we can use a LookupTable class, using Array objects as keys (each element of the array will be an index for a specific dimension of the "array"). The final effect is the same as using an array (almost in the AWK sense) and the approach has some advantages.
|num1 num2 pseudoArr|
num1 := stdin nextLine asInteger.
num2 := stdin nextLine asInteger.
"we can 'suggest' an initial value for the number
of ''slot'' the table can hold; anyway, if we use
more than these, the table automatically grows"
pseudoArr := LookupTable new: (num1 * num2).
1 to: num1 do: [ :i |
1 to: num2 do: [ :j |
pseudoArr at: {i. j} put: (i * j).
]
].
1 to: num1 do: [ :i |
1 to: num2 do: [ :j |
(pseudoArr at: {i. j}) displayNl.
]
].
SNOBOL4
Note: trim(input) is needed for Snobol4+.
* # Get user X,Y dimensions
output = 'Enter X,Y:'; xy = trim(input)
xy break(',') . x ',' rem . y
* # Define and create array, 1-based
arr = array(x ',' y) ;* Or arr = array(xy)
* # Display array prototype
output = 'Prototype: ' prototype(arr)
* # Assign elements, angle or square brackets
* # Same array can hold ints, strings, etc.
arr<x,y> = 99; arr[1,1] = 'dog'
* # Display elements
output = 'arr[' xy '] = ' arr[x,y]
output = 'arr[1,1] = ' arr[1,1]
* # Release array for garbage collection
arr =
end
Output:
Enter X,Y: 5,5 Prototype: 5,5 arr[5,5] = 99 arr[1,1] = dog
Standard ML
val nbr1 = valOf (TextIO.scanStream (Int.scan StringCvt.DEC) TextIO.stdIn);
val nbr2 = valOf (TextIO.scanStream (Int.scan StringCvt.DEC) TextIO.stdIn);
val array = Array2.array (nbr1, nbr2, 0.0);
Array2.update (array, 0, 0, 3.5);
print (Real.toString (Array2.sub (array, 0, 0)) ^ "\n");
Stata
display "Number of rows?" _request(nr)
display "Number of columns?" _request(nc)
matrix define a=J($nr,$nc,0)
matrix a[1,2]=1.5
matrix list a
matrix drop a
This is also possible in Mata, except that console input is still done from Stata:
mata
mata stata display "Number of rows?" _request(nr)
mata stata display "Number of columns?" _request(nc)
nr = strtoreal(st_global("nr"))
nc = strtoreal(st_global("nc"))
a = J(nr,nc,0)
a[1,2] = 1.5
a
mata drop a
end
Swift
import Foundation
print("Enter the dimensions of the array seperated by a space (width height): ")
let fileHandle = NSFileHandle.fileHandleWithStandardInput()
let dims = NSString(data: fileHandle.availableData, encoding: NSUTF8StringEncoding)?.componentsSeparatedByString(" ")
if let dims = dims where dims.count == 2{
let w = dims[0].integerValue
let h = dims[1].integerValue
if let w = w, h = h where w > 0 && h > 0 {
var array = Array<[Int!]>(count: h, repeatedValue: Array<Int!>(count: w, repeatedValue: nil))
array[0][0] = 2
println(array[0][0])
println(array)
}
}
- Output:
Enter the dimensions of the array seperated by a space (width height): 3 4 2 [[2, nil, nil], [nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]
Tcl
puts "Enter width:"
set width [gets stdin]
puts "Enter height:"
set height [gets stdin]
# Initialize array
for {set i 0} {$i < $width} {incr i} {
for {set j 0} {$j < $height} {incr j} {
set arr($i,$j) ""
}
}
# Store value
set arr(0,0) "abc"
# Print value
puts "Element (0/0): $arr(0,0)"
# Cleanup array
unset arr
Toka
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 )
cells malloc >r
dup cells >r
[ r> r> r> 2dup >r >r swap malloc swap i swap array.put >r ] iterate
r> r> nip
] is 2D-array
[ ( a b address -- value )
array.get array.get
] is 2D-get-element
[ ( value a b address -- )
array.get array.put
] is 2D-put-element
And a short test:
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
Ursa
decl int width height
out "width: " console
set width (in int console)
out "height: " console
set height (in int console)
decl int<><> twodstream
for (decl int i) (< i height) (inc i)
append (new int<>) twodstream
end for
for (set i 0) (< i height) (inc i)
decl int j
for (set j 0) (< j width) (inc j)
append 0 twodstream<i>
end for
end for
set twodstream<0><0> 5
out twodstream<0><0> endl console
VBA
Option Explicit
Sub Main_Create_Array()
Dim NbColumns As Integer, NbRows As Integer
'Get two integers from the user,
Do
NbColumns = Application.InputBox("Enter number of columns : ", "Numeric only", 3, Type:=1)
NbRows = Application.InputBox("Enter number of rows : ", "Numeric only", 5, Type:=1)
Loop While NbColumns = 0 Or NbRows = 0
'Create a two-dimensional array at runtime
ReDim myArray(1 To NbRows, 1 To NbColumns)
'Write some element of that array,
myArray(LBound(myArray, 1), UBound(myArray, 2)) = "Toto"
'and then output that element.
MsgBox myArray(LBound(myArray, 1), UBound(myArray, 2))
'destroy the array
Erase myArray
End Sub
Vim Script
" Create a two-dimensional array with r rows and c columns.
" The optional third argument specifies the initial value
" (default is 0).
function MakeArray(r, c, ...)
if a:0
let init = a:1
else
let init = 0
endif
let temp = []
for c in range(a:c)
call add(temp, init)
endfor
let array = []
for r in range(a:r)
call add(array, temp[:])
endfor
return array
endfunction
let rows = input("Enter number of rows: ")
let cols = input("Enter number of columns: ")
echo "\n"
let array = MakeArray(rows, cols)
let array[rows - 1][cols - 1] = rows * cols
echo array[rows - 1][cols - 1]
unlet array
Wren
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, null)
for (i in 0...x) a[i] = List.filled(y, 0)
// 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
- Output:
Example session:
Enter the dimensions of the array: First dimension : 3 Second dimension : 4 a[2][3] = 42
XPL0
inc c:\cxpl\codes; \(command words can be abbreviated to first 3 letters)
def IntSize=4; \number of bytes in an integer (2 or 4 depending on version)
int X, Y, A, I;
[X:= IntIn(0); Y:= IntIn(0); \get 2 dimensions from user
A:= Reserve(X*IntSize);
for I:= 0 to X-1 do A(I):= Reserve(Y*IntSize);
A(X/2, Y/2):= X+Y;
IntOut(0, A(X/2, Y/2)); CrLf(0);
]
Space allocated by the Reserve intrinsic is automatically released when the procedure that calls Release returns.
Example of a 12000x100 integer array for a total of 1.2 million integers:
12000 100 12100
zkl
zkl doesn't have a native array type but lists of lists can do the same thing. It is garbage collected so things are cleaned up implicitly.
This code creates a 2d row major zero filled array.
Since the array is row major, sub-scripting works as expected: [row][col].
rows:=ask("Rows: ").toInt();
cols:=ask("columns: ").toInt();
array:=rows.pump(List.createLong(rows),List.createLong(cols,0).copy);
array[1][2]=123;
array.println();
array[1][2].println();
The createLong method pre-allocates a list, optionally filled with a constant or computation.
- Output:
Rows: 3 columns: 4 L(L(0,0,0,0),L(0,0,123,0),L(0,0,0,0)) 123
If you want Matrix/linear algebra, you can use the GNU Scientific Library:
var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library)
rows:=ask("Rows: ").toInt();
cols:=ask("columns: ").toInt();
m:=GSL.Matrix(rows,cols);
m[1,2]=123;
m.format().println();
println(m[1,2]);
Again, garbage collected.
- Output:
Rows: 3 columns: 4 0.00, 0.00, 0.00, 0.00 0.00, 0.00, 123.00, 0.00 0.00, 0.00, 0.00, 0.00 123
zonnon
module Main;
type
Matrix = array *,* of integer;
var
m: Matrix;
i,j: integer;
begin
write("first dim? ");readln(i);
write("second dim? ");readln(j);
m := new Matrix(i,j);
m[0,0] := 10;
writeln("m[0,0]:> ",m[0,0]);
writeln("m[0,1].> ",m[0,1])
end Main.
- Output:
first dim? 10 second dim? 10 m[0,0]:> 10 m[0,1].> 0
- Programming Tasks
- Basic language learning
- Data Structures
- 11l
- 68000 Assembly
- Action!
- Action! Tool Kit
- Ada
- Modula-2/Omit
- ALGOL 60
- ALGOL 68
- ALGOL W
- Amazing Hopper
- APL
- AppleScript
- Arturo
- AutoHotkey
- AutoIt
- AWK
- BASIC
- Applesoft BASIC
- BASIC256
- QBasic
- True BASIC
- Yabasic
- BBC BASIC
- Commodore BASIC
- FreeBASIC
- IS-BASIC
- Liberty BASIC
- PureBasic
- QuickBASIC
- Run BASIC
- Sinclair ZX81 BASIC
- Sinclair ZX Spectrum BASIC
- TI-83 BASIC
- Visual Basic .NET
- BQN
- C
- C sharp
- C++
- Boost
- Clean
- Clojure
- CLU
- Common Lisp
- Component Pascal
- Crystal
- D
- Delphi
- Elena
- Elixir
- Erlang
- ERRE
- Euphoria
- F Sharp
- Factor
- Fermat
- Forth
- Forth Scientific Library
- Fortran
- Fōrmulæ
- Frink
- GAP
- Go
- Groovy
- Haskell
- HicEst
- Icon
- Unicon
- IDL
- J
- Java
- JavaScript
- Jq
- Julia
- Kotlin
- Logo
- Lua
- M2000 Interpreter
- Maple
- Mathematica
- Wolfram Language
- MATLAB
- Octave
- Maxima
- MAXScript
- MUMPS
- NetRexx
- Nim
- Objeck
- Objective-C
- OCaml
- OoRexx
- Oz
- PARI/GP
- Pascal
- Perl
- Phix
- Phixmonti
- Picat
- PicoLisp
- PL/I
- Pop11
- PowerShell
- Python
- Quackery
- R
- Racket
- Raku
- Red
- REXX
- Ring
- Ruby
- Rust
- Scala
- Scheme
- Scheme/SRFIs
- Seed7
- Sidef
- Simula
- Smalltalk
- SNOBOL4
- Standard ML
- Stata
- Swift
- Tcl
- Toka
- Ursa
- VBA
- Vim Script
- Wren
- XPL0
- Zkl
- Zonnon
- Pages with too many expensive parser function calls