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

From Rosetta Code
Content added Content deleted
(AutoIT marked incorrect.)
Line 77: Line 77:


=={{header|AutoIt}}==
=={{header|AutoIt}}==
<lang AutoIt>#include <Array.au3>
{{incorrect|AutoIt|This example does not receive integers from the user: they appear to be hardcoded.}}
$comp = InputBox ("Prompt","Input two positive whole numbers separated by a space")
<lang AutoIt>Global $array[1][1]
$compArray = StringSplit($comp, " ", 2)
$array[0][0] = 42
_ArrayDisplay($compArray)
MsgBox(0,"",$array[0][0])
</lang>
</lang>



Revision as of 16:47, 28 September 2011

Task
Create a two-dimensional array at runtime
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.

You may see other such structures in the Data Structures category.

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.

Ada

<lang ada>with Ada.Text_Io; use Ada.Text_Io; with Ada.Float_Text_Io; use Ada.Float_Text_Io; with Ada.Integer_Text_Io; use 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

  Get(Item => Dim_1);
  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;
     Put(Item => Matrix(1, Dim_2), Fore => 1, Aft => 5, Exp => 0);
     New_Line;
  end;
  -- The variable Matrix is popped off the stack automatically

end Two_Dimensional_Arrays;</lang>

ALGOL 68

<lang algol68>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])

)</lang>

APL

Arrays are an integral part of APL. Array size, shape, and data type can be easily manipulated at runtime.

<lang APL>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 </lang>

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.

<lang AppleScript>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 {} </lang>

AutoIt

<lang AutoIt>#include <Array.au3> $comp = InputBox ("Prompt","Input two positive whole numbers separated by a space") $compArray = StringSplit($comp, " ", 2) _ArrayDisplay($compArray) </lang>

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.

<lang awk>/[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]]
 }

}</lang>

BASIC

Works with: QuickBasic version 4.5
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

BBC BASIC

<lang bbcbasic> INPUT "Enter array dimensions separated by a comma: " a%, b%

     DIM array(a%, b%)
     array(1, 1) = PI
     PRINT array(1, 1)</lang>

C

C99

Works with: C99

<lang c>#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;

}</lang>

Traditional Style

This style is supported by all 'C' compilers. <lang c>#include <stdio.h>

  1. 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(user2*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;

}</lang> 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. <lang c>#include <stdio.h>

  1. include <stdlib.h>

int main(int argc, char **argv) {

  int user1 = 0;
  int space_needed;
  int *a1, **array;
  int row, col;
  printf("Enter size of array:  ");
  scanf("%d",&user1);
  space_needed = (user1+1)*user1/2;
  a1 = malloc(space_needed);
  array = malloc(user1*sizeof(int*));
  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;

}</lang>

This approach most closely matches the C99 example, as alloca allocates on the stack, rather than the heap, as malloc does.

<lang c>#include <stdio.h>

  1. 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;

}</lang>

C++

With language built-in facilities:

<lang cpp>#include <iostream>

  1. include <istream>
  2. include <ostream>

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;

}</lang>

Using std::vector from the standard library:

<lang cpp>#include <iostream>

  1. include <istream>
  2. include <ostream>
  3. 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()

}</lang>

C#

<lang csharp> 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();
       }
   }

</lang>

Clean

<lang 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)</lang>


Clojure

<lang 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)))</lang>

Common Lisp

<lang 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))))</lang>

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.

<lang oberon2> 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.</lang>

D

<lang d>import std.stdio, std.conv, std.string;

void main() {

   write("Give me the numer of rows: ");
   int nrow = to!int(readln().strip());
   write("Give me the numer of columns: ");
   int ncol = to!int(readln().strip());
   auto array = new float[][](nrow, ncol);
   array[0][0] = 3.5;
   writeln("The number at place [0, 0] is ", array[0][0]);

}</lang>

Delphi

Dimensions are generated randomly, not input by user. <lang delphi>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. </lang>

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


Euphoria

<lang 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]})</lang>

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 :) <lang factor>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 ;</lang>

Forth

<lang 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</lang>

<lang forth>INTEGER DMATRIX my-matrix{{ & my-matrixTemplate:8 9malloc

8 my-matrixTemplate:3 4 ! my-matrixTemplate:3 4 @ .

& my-matrix{{ }}free</lang>

Fortran

In Fortran 90 and later <lang fortran>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</lang>

F#

<lang fsharp>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]</lang>

GAP

<lang gap># Creating an array of 0 a := NullMat(2, 2);

  1. [ [ 0, 0 ], [ 0, 0 ] ]
  1. Some assignments

a[1][1] := 4; a[1][2] := 5; a[2][1] := 3; a[2][2] := 4;

a

  1. [ [ 4, 5 ], [ 3, 4 ] ]

Determinant(a);

  1. 1</lang>

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]. <lang go>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.

}</lang> 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, <lang go> // 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]
   }</lang>

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, <lang go> func get(r, c int) int {

   return e[r*cols+c]

}</lang>

Haskell

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

HicEst

<lang 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) </lang>

Icon and Unicon

All Icon and Unicon data objects are automatically reclaimed. Multiply dimensioned arrays are arrays of arrays in both languages.

<lang icon>procedure main(args)

   nr := integer(args[1]) | 3  # Default to 3x3
   nc := integer(args[2]) | 3
   A := list(nr)
   every !A := list(nc)
   x := ?nr    # Select a random element
   y := ?nc
   A[x][y] := &pi
   write("A[",x,"][",y,"] -> ",A[x][y])

end</lang>

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.

<lang idl>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</lang>

J

The natural ways of creating a two dimensional array, from array dimensions, in J are i. and $

<lang j> 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</lang>

To update the upper left corner of the array with the value 99, you might use }

<lang j> array1=: 99 (<0 0)} array1</lang>

And, to retrieve that value you might use {

<lang j> (<0 0) { array1</lang>

Finally, J manages storage for you, so to delete the array, you could either have the name refer to a new value

<lang j> array1=: 0</lang>

or you could remove the name itself:

<lang j> erase'array1'</lang>

Putting these ideas together and adding a few frills:

<lang j>task=: 3 : 0 'init new' =. 0;1 NB. values for initialization and alteration array =. y $ init NB. create array of shape y element =. < ? $ array NB. pick an atom of the array at random array =. new element } array NB. amend that element to new value element { array NB. return value of changed element )</lang> 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.

The type of the array is determined by the type of the values used in filling the array. E.g., alternate data types are obtained by substituting any of the following lines: <lang j>'init new' =. ' ';'x' NB. literals 'init new' =. 1r2;2r3 NB. fractions 'init new' =. a: ; <<'Rosetta' NB. boxes</lang>

Java

<lang 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]);
 }

}</lang>

JavaScript

For the user input, requires JScript. The array processing is implementation agnostic. <lang javascript>var w = parseInt( get_input("Enter a width:") ); var w = parseInt( get_input("Enter a height:") );

// create the 2-D array var a = new Array(h); for (var i = 0; i < h; i++)

 a[i] = new Array(w);

a[0][0] = 'foo'; WScript.Echo('a[0][0] = ' + a[0][0]);

a = null;

function get_input(prompt) {

   output(prompt);
   try {
       return WScript.StdIn.readLine();
   } catch(e) {
       return readline();
   }

} function output(prompt) {

   try {
       return WScript.echo(prompt);
   } catch(e) {
       return print(prompt);
   }

}</lang>

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. <lang lb> 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 </lang>


Works with: UCB Logo

<lang logo>make "a2 mdarray [5 5] mdsetitem [1 1] :a2 0  ; by default, arrays are indexed starting at 1 print mditem [1 1] :a2  ; 0</lang>

Lua

<lang 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])</lang>

Mathematica

<lang Mathematica> arrayFun[m_Integer,n_Integer]:=Module[{array=ConstantArray[0,{m,n}]},

  array1,1=RandomReal[];
  array1,1

]</lang>


MATLAB

<lang MATLAB>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))]);</lang>

Sample Output: <lang MATLAB>Array Width: 18 Array Height: 12 Array element (1,1) = 12</lang>

MAXScript

<lang 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]</lang>

MUMPS

This example uses a two level tree to mimic an 2D array. <lang MUMPS> 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

</lang>

Objeck

<lang 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();
     };	
   }
 }

} </lang>

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.

Works with: GNUstep
Works with: Cocoa

<lang objc>#import <Foundation/Foundation.h>

int main() {

 int num1, num2, i, j;
 NSMutableArray *arr;
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 scanf("%d %d", &num1, &num2);
 NSLog(@"%d %d", num1, num2);
 
 arr = [NSMutableArray arrayWithCapacity: (num1*num2)];
 // initialize it with 0s
 for(i=0; i < (num1*num2); i++) [arr addObject: [NSNumber numberWithInt: 0]];
 // replace 0s with something more interesting
 for(i=0; i < num1; i++) {
   for(j=0; j < num2; j++) {
     [arr replaceObjectAtIndex: (j*num1+i) withObject: [NSNumber numberWithInt: (i*j)]];
   }
 }
 // access a value: I*num1+J, where I,J are the indexes for the bidimensional array
 NSLog(@"%@", [arr objectAtIndex: (1*num1+3)]);
 [pool release];
 return 0;

}</lang>

OCaml

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

or using the module Bigarray:

<lang ocaml>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 ();;</lang>

Oz

Oz does not have multi-dimensional arrays. But we can create an array of arrays (similarly to most examples on this page): <lang oz>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}</lang>
 

PARI/GP

<lang parigp>tmp(m,n)={

 my(M=matrix(m,n,i,j,0));
 M[1,1]=1;
 M[1,1]

};</lang>

Pascal

Works with: GNU Pascal version 20060325, based on gcc-3.4.4

The following code is standard Extended Pascal (tested with gpc --extended-pascal):

<lang 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.</lang>


Perl

Works with: Perl version 5.x

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

<lang perl>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";
 }

}</lang>

The above is a bit verbose, here is a simpler implementation:

<lang perl>sub array {

   my ($x, $y) = @_;
   map {[ (0) x $x ]} 1 .. $y

}

my @square = array 3, 3;

  1. everything above this line is mostly redundant in perl,
  2. since perl would have created the array automatically when used.
  3. however, the above function initializes the array elements to 0,
  4. while perl would have used undef
  5. $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</lang>

Perl 6

<lang perl6>my ($major,$minor) = prompt("Dimensions? ").comb(/\d+/); my @array := [ for ^$major { [ for ^$minor {'@'} ] } ]; @array[ pick 1, ^$major ][ pick 1, ^$minor ] = ' '; .say for @array;</lang> Line 1: The input parse doesn't care how you separate the dimensions as long as there are two distinct numbers.

Line 2: Any bracketing construct in Perl 6 may contain a statement. Also, any loop returns a list of each of its iteration's results. Together these make it trivial to write most kinds of list comprehensions. In this case all the loop iterations return the identical initial value for that array element. The ^$major construct is short for 0..^$major, that is, all the integers up to (but not including) $major.

Line 3: Again we use the "upto" operator, but this time just pick one of the numbers for each dimension.

Line 4: Print each line of the array. When each subarray is converted to a string, it automatically gets a space between elements. Any method call without an object defaults to the current topic, so .say really means $_.say. There is no need in Perl 6 to remember which functions default to $_.

Typical run:

<lang>$ ./two-dee Dimensions? 5x35 @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @</lang>

PicoLisp

<lang 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)</lang> 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

<lang> /* 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; </lang>

<lang PL/I> 6.00000E+0000 5.00000E+0000 4.00000E+0000 3.00000E+0000 2.00000E+0000

1.00000E+0000 

</lang>

<lang PL/I> /* 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 then the block terminates. */ </lang>

<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+0002

</lang>

<lang PL/I> /* 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. */ </lang>

<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 

</lang>

Pop11

<lang 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;</lang>

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

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

PureBasic

<lang 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</lang>

Python

Works with: Python version 2.5

<lang python>width = int(raw_input("Width of myarray: ")) height = int(raw_input("Height of Array: ")) myarray = [[0] * width for i in xrange(height)] myarray[0][0] = 3.5 print myarray[0][0]</lang>

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:

<lang python>myarray = {(w,h): 0 for w in range(width) for h in range(height)}

  1. 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)]</lang>

R

Translation of: C

<lang 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="")</lang>

REXX

<lang rexx> /*REXX program to allocate/populate/display a two-dimensional array. */

call bloat

                            /*no more array named  A.  at this point.*/

exit


bloat: procedure

say say 'Enter two positive integers (a 2-dimensional array will be created).' say pull n m . /*there is no way to pre-allocate an array,*/

                            /*REXX will allocate each element when it's*/
                            /*assigned.                                */

a.='~' /*default value for all elements so far. */

                            /*this ensures every element has a value.  */
 do j=1 for n
   do k=1 for m
   if random()//7==0 then a.j.k=j'_'k       /*populate every 7th random*/
   end
 end


 do jj=1 for n              /*now, display the array to the console.   */
 _=
   do kk=1 for m
   _=_ right(a.jj.kk,4)     /*display one row at a time, align the vals*/
   end
 say _
 end
                            /*when the RETURN is executed (from a      */
                            /*PROCEDURE in this case), the array  A. is*/
                            /*"de-allocated", that is, it's no longer  */
                            /*defined, and the memory that the array   */
                            /*has is now free for other REXX variables.*/
                            /*If the  BLOAT:   subroutine didn't have a*/
                            /*"PROCEDURE" on that statement, the array */
                            /*would be left intact.                    */
                            /*The same effect is performed by a DROP.  */

drop a. return </lang> Output when

30 15

is entered after the prompt message.

    ~  1_2    ~    ~    ~    ~    ~  1_8  1_9 1_10 1_11    ~    ~    ~    ~
  2_1  2_2    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~ 2_13    ~    ~
    ~    ~  3_3  3_4    ~    ~    ~    ~    ~ 3_10    ~    ~    ~    ~    ~
    ~    ~    ~    ~    ~    ~  4_7    ~    ~    ~ 4_11    ~    ~    ~    ~
    ~    ~    ~    ~  5_5    ~    ~    ~    ~    ~    ~    ~    ~    ~ 5_15
    ~    ~  6_3    ~    ~    ~  6_7    ~    ~ 6_10    ~    ~    ~    ~    ~
  7_1    ~    ~  7_4    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~
    ~  8_2    ~    ~    ~    ~  8_7    ~    ~    ~    ~    ~    ~    ~    ~
    ~  9_2    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~ 9_15
    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~
 11_1    ~    ~    ~ 11_5    ~    ~ 11_8 11_9    ~    ~    ~    ~    ~    ~
    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~
    ~ 13_2    ~ 13_4    ~    ~    ~    ~    ~    ~    ~    ~ 3_13 3_14    ~
    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~ 4_11    ~    ~    ~    ~
    ~ 15_2    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~
    ~ 16_2    ~ 16_4    ~    ~    ~    ~    ~    ~ 6_11    ~    ~    ~    ~
 17_1    ~ 17_3    ~    ~ 17_6    ~    ~    ~ 7_10    ~    ~    ~    ~ 7_15
    ~    ~    ~    ~ 18_5    ~    ~    ~    ~ 8_10    ~    ~    ~ 8_14    ~
    ~ 19_2    ~    ~    ~    ~    ~    ~    ~    ~ 9_11    ~    ~    ~    ~
    ~    ~    ~    ~    ~    ~ 20_7    ~    ~    ~    ~    ~    ~    ~    ~
    ~    ~    ~    ~ 21_5    ~    ~    ~    ~ 1_10    ~    ~    ~    ~ 1_15
    ~ 22_2    ~    ~    ~    ~    ~    ~ 22_9    ~ 2_11 2_12    ~    ~    ~
    ~    ~    ~    ~ 23_5    ~    ~    ~    ~    ~ 3_11 3_12    ~ 3_14    ~
    ~    ~    ~    ~    ~ 24_6    ~ 24_8 24_9    ~    ~ 4_12    ~    ~    ~
 25_1    ~    ~    ~    ~ 25_6 25_7    ~    ~ 5_10    ~    ~    ~    ~    ~
    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~    ~
    ~    ~    ~    ~    ~    ~ 27_7    ~ 27_9    ~    ~    ~ 7_13    ~    ~
    ~    ~    ~    ~    ~    ~    ~    ~    ~ 8_10    ~    ~    ~    ~    ~
    ~ 29_2    ~    ~ 29_5    ~    ~    ~    ~ 9_10    ~    ~    ~    ~    ~
    ~ 30_2 30_3    ~ 30_5    ~    ~ 30_8 30_9    ~    ~    ~ 0_13    ~    ~

Ruby

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

Scala

<lang 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))
  }

}</lang>

Seed7

<lang 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;</lang>

Output:

Give me the numer of rows: 5
Give me the numer of columns: 7
The number at place [1, 1] is 3

Smalltalk

Works with: Pharo

<lang 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. </lang>

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.

<lang smalltalk>|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
 ]

].</lang>

A possible implementation for a BidimensionalArray class is the following (changing Multi into Bi and using this class, the previous code runs fine):

<lang smalltalk>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
 ]

].</lang>

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.

<lang smalltalk>|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.
 ]

].</lang>

SNOBOL4

Works with: Macro Spitbol
Works with: Snobol4+
Works with: CSnobol

Note: trim(input) is needed for Snobol4+.

<lang 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</lang>

Output:

Enter X,Y:
5,5
Prototype: 5,5
arr[5,5] = 99
arr[1,1] = dog

Standard ML

<lang sml>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");</lang>

Tcl

Works with: Tcl version 8.5

<lang tcl>package require Tcl 8.5

puts "enter X dimension:" set dim2 [gets stdin] puts "enter Y dimension:" set dim1 [gets stdin]

  1. Make the "array"; we'll keep it in row-major form

set l [lrepeat $dim1 [lrepeat $dim2 {}]]

  1. Select a point at around the middle of the "array"

set y [expr {$dim1>>1}] set x [expr {$dim2>>1}]

  1. Set the value at that point

lset l $y $x aValue

  1. Read the value at that point

puts [lindex $l $y $x]

  1. Delete the "array"

unset l</lang>

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.

<lang toka>[ ( 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</lang>

And a short test:

<lang toka>5 5 2D-array >r #! Create an array and save the pointer to it 10 2 3 r@ 2D-put-element #! Set element 2,3 to 10 2 3 r@ 2D-get-element #! Get the element at 2,3 r> drop #! Discard the pointer to the array</lang>