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

From Rosetta Code
Content added Content deleted
mNo edit summary
Line 1: Line 1:
{{task}}
[http://gabe95.ifrance.com/html/le-vent/ le vent nous portera mp3] [http://gabe95.ifrance.com/html/cartina-regione/ cartina regione italia] [http://lightmyfire0214.ifrance.com/topic/il-pugnale/ il pugnale e la croce libri] [http://horcrionebay.ifrance.com/resources/articles/grandi-domani.htm grandi domani] [http://trumanburb.ifrance.com/view/new/lettini-pali.htm lettini pali katy] [http://trumanburb.ifrance.com/view/new/dvdrw-mini.htm dvd-rw mini] [http://alishca-st.ifrance.com/text/directory/cultura-giuridica.htm cultura giuridica e attori] [http://alishca-st.ifrance.com/text/directory/toyota-yaris.htm toyota yaris torino] [http://trumanburb.ifrance.com/view/new/il-dizionario.htm il dizionario delle ragazze] [http://krychan.ifrance.com/content/view/tv-lcd.htm tv lcd akai 30] [http://mfirishka.ifrance.com/view/new/vivere-per.htm vivere per vivere] [http://gabe95.ifrance.com/html/oem-dvd/ oem dvd slim] [http://lightmyfire0214.ifrance.com/topic/la-camella/ la camella] [http://zeakk.ifrance.com/resources/articles/finestra-in.htm finestra in alluminio] [http://flawedamythyst.ifrance.com/img/styles/condizionatori-di.htm condizionatori di aria] [http://alishca-st.ifrance.com/text/directory/no-milk.htm no milk too day] [http://squoi-oop.ifrance.com/topic/immagini-folletti/ immagini folletti] [http://mfirishka.ifrance.com/view/new/nhra-com.htm nhra com] [http://lightmyfire0214.ifrance.com/topic/e-cellulari/ e300 cellulari samsung] [http://lightmyfire0214.ifrance.com/topic/cover-divx/ cover divx] [http://cocopuff66.ifrance.com/text/directory/flauto-pan/ flauto pan] [http://trumanburb.ifrance.com/view/new/cuffia-philips.htm cuffia philips wireless] [http://lekusya.ifrance.com/library/html/office-italiano/ office italiano] [http://lekusya.ifrance.com/library/html/sony-plasma/ sony plasma 1024x768] [http://cocopuff66.ifrance.com/text/directory/testo-giorgia/ testo giorgia] [http://atrayah.ifrance.com/topic/horst-wessel.htm horst wessel lied] [http://zeakk.ifrance.com/resources/articles/aci-merate.htm aci merate] [http://smegmacheez.ifrance.com/content/view/pci-express/ pci express 512 mb] [http://call-shotgun.ifrance.com/view/new/televisori-/ televisori 29 3 prese scart] [http://alishca-st.ifrance.com/text/directory/media-center.htm media center scaleo] [http://gajar.ifrance.com/html/duma-siria.htm duma (siria)] [http://atrayah.ifrance.com/topic/nuovi-dettagli.htm nuovi dettagli per le crociate] [http://cocopuff66.ifrance.com/text/directory/frozen-madonna/ frozen madonna] [http://smegmacheez.ifrance.com/content/view/cercare-uomo/ cercare uomo] [http://atrayah.ifrance.com/topic/radeon-.htm radeon 9800 pro agp] [http://nokros.ifrance.com/data/notaio-costa/ notaio costa] [http://smegmacheez.ifrance.com/content/view/ue-man/ ue man] [http://sm4.ifrance.com/lib/driver-scheda/ driver scheda video 0 9] [http://flawedamythyst.ifrance.com/img/styles/villaggio-tunisia.htm villaggio tunisia] [http://zeakk.ifrance.com/resources/articles/albatross.htm albatross] [http://joshuaw-wise.ifrance.com/img/styles/cultura-mixteca/ cultura mixteca] [http://flawedamythyst.ifrance.com/img/styles/ufficio-postali.htm ufficio postali] [http://flawedamythyst.ifrance.com/img/styles/video-di.htm video di ingoi] [http://gajar.ifrance.com/html/italia-ischia.htm italia ischia] [http://cocopuff66.ifrance.com/text/directory/biglietti-battesimi/ biglietti battesimi] [http://gajar.ifrance.com/html/dvd-home.htm dvd home theater] [http://cocopuff66.ifrance.com/text/directory/sono-sposata/ sono sposata] [http://smegmacheez.ifrance.com/content/view/decreto-legislativo/ decreto legislativo anno 2004] [http://krychan.ifrance.com/content/view/stephanie-de.htm stephanie de monaco] [http://dejablu503.ifrance.com/description/lib/fotografie-erotiche.htm fotografie erotiche amatoriali] [http://call-shotgun.ifrance.com/view/new/il-vento/ il vento amico] [http://trumanburb.ifrance.com/view/new/la-boheme.htm la boheme] [http://nokros.ifrance.com/data/videoregistrazione-per/ videoregistrazione per videosorveglianza] [http://lightmyfire0214.ifrance.com/topic/heavy-gear/ heavy gear] [http://cocopuff66.ifrance.com/text/directory/uomo-nudi/ uomo nudi gratis] [http://lightmyfire0214.ifrance.com/topic/passeggiata-margherita/ passeggiata margherita] [http://smegmacheez.ifrance.com/content/view/monitor-fujitsu/ monitor fujitsu siemens] [http://atrayah.ifrance.com/topic/foto-di.htm foto di irlanda] [http://smegmacheez.ifrance.com/content/view/hp-toner/ hp toner laser q2612a] [http://mfirishka.ifrance.com/view/new/lettori-da.htm lettori da tavolo cd per dj] [http://smegmacheez.ifrance.com/content/view/batteria-bnv/ batteria bn-v408] [http://cocopuff66.ifrance.com/text/directory/the-sains/ the sains] [http://krychan.ifrance.com/content/view/obiettivi-sigma.htm obiettivi sigma 18-200mm] [http://smegmacheez.ifrance.com/content/view/sesso-ciccione/ sesso ciccione] [http://dejablu503.ifrance.com/description/lib/ethernet-playstation.htm ethernet playstation 2] [http://zeakk.ifrance.com/resources/articles/bigliettini-di.htm bigliettini di auguri di compleanno] [http://krychan.ifrance.com/content/view/canoscan-f.htm canoscan 8400f] [http://gabe95.ifrance.com/html/al-di/ al di la del bene e male] [http://horcrionebay.ifrance.com/resources/articles/guillaume-de.htm guillaume de machaut o de mac...] [http://lekusya.ifrance.com/library/html/beethoven-bagatelle/ beethoven bagatelle] [http://flawedamythyst.ifrance.com/img/styles/sobrevivire-de.htm sobrevivire de celia cruz] [http://squoi-oop.ifrance.com/topic/relazione-frankestein/ relazione frankestein di mary shelley] [http://zeakk.ifrance.com/resources/articles/hotel-pineta.htm hotel pineta marina di massa] [http://gajar.ifrance.com/html/immegini-trans.htm immegini trans] [http://horcrionebay.ifrance.com/resources/articles/stile-css.htm stile css] [http://horcrionebay.ifrance.com/resources/articles/videos-de.htm videos de shakira] [http://mfirishka.ifrance.com/view/new/cipro-elenco.htm cipro elenco telefonico] [http://dejablu503.ifrance.com/description/lib/modulo-ddr.htm modulo ddr 333 sodimm 512] [http://mfirishka.ifrance.com/view/new/sobre-fuego.htm sobre fuego] [http://alishca-st.ifrance.com/text/directory/orario-ffss.htm orario ffss] [http://lightmyfire0214.ifrance.com/topic/altalex-com/ altalex com] [http://smegmacheez.ifrance.com/content/view/beatle-the/ beatle the wall] [http://call-shotgun.ifrance.com/view/new/sbocco-lg/ sbocco lg 8110] [http://call-shotgun.ifrance.com/view/new/telecel-bolivia/ telecel bolivia] [http://alishca-st.ifrance.com/text/directory/ripper-dvd.htm ripper dvd] [http://gajar.ifrance.com/html/schede-video.htm schede video nvidia 6600] [http://mfirishka.ifrance.com/view/new/foto-gratis.htm foto gratis sesso] [http://lightmyfire0214.ifrance.com/topic/hong-kong/ hong kong - colpo su colpo] [http://krychan.ifrance.com/content/view/ciao-tennessee.htm ciao tennessee] [http://horcrionebay.ifrance.com/resources/articles/www-eva.htm www eva henger it] [http://krychan.ifrance.com/content/view/topless-gratuiti.htm topless gratuiti] [http://smegmacheez.ifrance.com/content/view/cubo-rubick/ cubo rubick] [http://smegmacheez.ifrance.com/content/view/montgomerie-alexander/ montgomerie, alexander] [http://lightmyfire0214.ifrance.com/topic/planetesimale/ planetesimale] [http://lightmyfire0214.ifrance.com/topic/basi-musicali/ basi musicali] [http://squoi-oop.ifrance.com/topic/rume/ rume] [http://flawedamythyst.ifrance.com/img/styles/www-harrypotter.htm www harrypotter com] [http://horcrionebay.ifrance.com/resources/articles/profumi-alla.htm profumi alla frutta] [http://sm4.ifrance.com/lib/windows/ windows 2003] [http://smegmacheez.ifrance.com/content/view/comune-di/ comune di fabro] [http://dejablu503.ifrance.com/description/lib/mica.htm mica] [http://krychan.ifrance.com/content/view/esibizioniste-treviso.htm esibizioniste treviso] [http://alishca-st.ifrance.com/text/directory/sapphire-x.htm sapphire x800 xl 512mb pci-e vivo ati] [http://sm4.ifrance.com/lib/redondo-de/ redondo de ricota] [http://smegmacheez.ifrance.com/content/view/harman-kardon/ harman kardon 3480] [http://alishca-st.ifrance.com/text/directory/www-spiderman.htm www spiderman2] [http://atrayah.ifrance.com/topic/sony-cybershot.htm sony cyber-shot dsc-w7] [http://alishca-st.ifrance.com/text/directory/kalin-olson.htm kalin olson] [http://zeakk.ifrance.com/resources/articles/galeria-de.htm galeria de fotos de zoofilia de mujeres] [http://dejablu503.ifrance.com/description/lib/aumania.htm aumania] [http://mfirishka.ifrance.com/view/new/krital.htm krital] [http://gajar.ifrance.com/html/lagosanto.htm lagosanto] [http://nokros.ifrance.com/data/david-spencer/ david spencer group] [http://alishca-st.ifrance.com/text/directory/ps-slim.htm ps2 slim swap magic v3 6 plus slide card] [http://atrayah.ifrance.com/topic/amd-.htm amd 3200 barton] [http://lekusya.ifrance.com/library/html/cazzi-gay/ cazzi gay italiani] [http://alishca-st.ifrance.com/text/directory/audio-registratore.htm audio registratore] [http://sm4.ifrance.com/lib/sintoamplificatori-hi/ sintoamplificatori hi fi] [http://lightmyfire0214.ifrance.com/topic/video-de/ video de white flag de dido] [http://mfirishka.ifrance.com/view/new/chab-nasro.htm chab nasro] [http://trumanburb.ifrance.com/view/new/fle.htm fle] [http://atrayah.ifrance.com/topic/laserwriter-.htm laserwriter 4 600 toner] [http://call-shotgun.ifrance.com/view/new/speedy-j/ speedy j] [http://joshuaw-wise.ifrance.com/img/styles/www-affari/ www affari] [http://horcrionebay.ifrance.com/resources/articles/erick.htm erick] [http://flawedamythyst.ifrance.com/img/styles/saab.htm saab 9 3] [http://gajar.ifrance.com/html/beauty-point.htm beauty point] [http://horcrionebay.ifrance.com/resources/articles/frigorifero-anni.htm frigorifero anni 50] [http://nokros.ifrance.com/data/giochi-che/ giochi che ce editor] [http://trumanburb.ifrance.com/view/new/fotos-insolitas.htm fotos insolitas] [http://trumanburb.ifrance.com/view/new/condizionatore-dual.htm condizionatore dual] [http://smegmacheez.ifrance.com/content/view/roma-auto/ roma auto] [http://sm4.ifrance.com/lib/video-nuda/ video nuda] [http://mfirishka.ifrance.com/view/new/time-of.htm time of your life green day] [http://mfirishka.ifrance.com/view/new/recuerdos-de.htm recuerdos de alhambra] [http://sm4.ifrance.com/lib/tonga/ tonga] [http://lekusya.ifrance.com/library/html/taglia-del/ taglia del cane] [http://call-shotgun.ifrance.com/view/new/dual/ dual 7800] [http://nokros.ifrance.com/data/isof/ isof] [http://sm4.ifrance.com/lib/bouquet/ bouquet] [http://joshuaw-wise.ifrance.com/img/styles/praticante/ praticante] [http://cocopuff66.ifrance.com/text/directory/fell-in/ fell in love a boy] [http://trumanburb.ifrance.com/view/new/www-uu.htm www uu] [http://gajar.ifrance.com/html/classicheggiante.htm classicheggiante] [http://atrayah.ifrance.com/topic/www-sex.htm www sex toggolino de] [http://zeakk.ifrance.com/resources/articles/quello-che.htm quello che sento] [http://zeakk.ifrance.com/resources/articles/ricette-della.htm ricette della francia] [http://squoi-oop.ifrance.com/topic/bella-ciao/ bella ciao] [http://gabe95.ifrance.com/html/pluton-com/ pluton com] [http://cocopuff66.ifrance.com/text/directory/punto-km/ punto km0 multijet] [http://joshuaw-wise.ifrance.com/img/styles/razorback/ razorback2] [http://lightmyfire0214.ifrance.com/topic/grande-dizionario/ grande dizionario garzanti libri] [http://squoi-oop.ifrance.com/topic/film-cruel/ film cruel intention] [http://trumanburb.ifrance.com/view/new/dragostea-den.htm dragostea den tin] [http://cocopuff66.ifrance.com/text/directory/hamburg/ hamburg] [http://lightmyfire0214.ifrance.com/topic/non-guardarmi/ non guardarmi ti sento] [http://atrayah.ifrance.com/topic/chen-tuhsiu.htm chen tu-hsiu] [http://trumanburb.ifrance.com/view/new/rca-online.htm rca online] [http://alishca-st.ifrance.com/text/directory/cucuna-tu.htm cucuna tu] [http://atrayah.ifrance.com/topic/telecomando-ati.htm telecomando ati] [http://lightmyfire0214.ifrance.com/topic/tatoo-alfabeto/ tatoo alfabeto cinese] [http://sm4.ifrance.com/lib/curtis-george/ curtis, george william] [http://flawedamythyst.ifrance.com/img/styles/decreto-legge.htm decreto legge n 104] [http://zeakk.ifrance.com/resources/articles/palmari-fujitsu.htm palmari fujitsu siemens] [http://zeakk.ifrance.com/resources/articles/ya-nada.htm ya nada volvera a ser como antes el canto] [http://sm4.ifrance.com/lib/borsa-in/ borsa in pelle] [http://smegmacheez.ifrance.com/content/view/il-miglior/ il miglior controller per virtual on] [http://gabe95.ifrance.com/html/e-mail/ e mail natale] [http://call-shotgun.ifrance.com/view/new/tenda-/ tenda 6 posti campeggio] [http://trumanburb.ifrance.com/view/new/cartoni-cinesi.htm cartoni cinesi porno] [http://joshuaw-wise.ifrance.com/img/styles/hifi-italia/ hifi italia] [http://dejablu503.ifrance.com/description/lib/qware.htm qware 512] [http://atrayah.ifrance.com/topic/vacanze-di.htm vacanze di natale in casa disney] [http://horcrionebay.ifrance.com/resources/articles/farfield.htm farfield] [http://gabe95.ifrance.com/html/piscina-como/ piscina como] [http://sm4.ifrance.com/lib/prenotazione-hotel/ prenotazione hotel dublino] [http://nokros.ifrance.com/data/midland-texas/ midland (texas)] [http://alishca-st.ifrance.com/text/directory/scuole-serali.htm scuole serali a milano] [http://lekusya.ifrance.com/library/html/incontro-varese/ incontro varese] [http://alishca-st.ifrance.com/text/directory/girls-photos.htm girls photos] [http://cocopuff66.ifrance.com/text/directory/abito-da/ abito da lavoro] [http://joshuaw-wise.ifrance.com/img/styles/culo-della/ culo della unzicher] [http://squoi-oop.ifrance.com/topic/canzoni-remix/ canzoni remix] [http://sm4.ifrance.com/lib/nadiya/ nadiya] [http://mfirishka.ifrance.com/view/new/find-the.htm find the difference] [http://mfirishka.ifrance.com/view/new/cavo-component.htm cavo component scart] [http://gabe95.ifrance.com/html/migueli/ migueli] [http://mfirishka.ifrance.com/view/new/mylogin-host.htm mylogin host domain] [http://krychan.ifrance.com/content/view/ibm-portatile.htm ibm portatile centrino] [http://nokros.ifrance.com/data/linea-interrotta/ linea interrotta] [http://gabe95.ifrance.com/html/racconti-erotici/ racconti erotici clisteri] [http://gajar.ifrance.com/html/ennepetal.htm ennepetal] [http://lekusya.ifrance.com/library/html/gries-johann/ gries, johann dietrich] [http://mfirishka.ifrance.com/view/new/carta-gioco.htm carta gioco] [http://nokros.ifrance.com/data/eiffel-blue/ eiffel blue] [http://alishca-st.ifrance.com/text/directory/cd.htm cd6000] [http://horcrionebay.ifrance.com/resources/articles/dvd-hd.htm dvd hd lg] [http://lekusya.ifrance.com/library/html/lo-voglio/ lo voglio duro] [http://flawedamythyst.ifrance.com/img/styles/testo-ave.htm testo ave maria shubert] [http://flawedamythyst.ifrance.com/img/styles/ethernet-disk.htm ethernet disk mini 250] {{task}}
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 oputput that element. Finally destroy the array if not done by the language itself.
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 oputput that element. Finally destroy the array if not done by the language itself.


Line 27: Line 27:




==[[C plus plus|C ]]==
==[[C plus plus|C++]]==
[[Category:C plus plus]]
[[Category:C plus plus]]
With language built-in facilities:
With language built-in facilities:
Line 44: Line 44:
double* array_data = new double[dim1*dim2];
double* array_data = new double[dim1*dim2];
double** array = new double*[dim1];
double** array = new double*[dim1];
for (int i = 0; i < dim1; i)
for (int i = 0; i < dim1; ++i)
array[i] = array_data dim2*i;
array[i] = array_data + dim2*i;
// write element
// write element
Line 115: Line 115:
: cell-matrix
: cell-matrix
create ( width height "name" ) over , * cells allot
create ( width height "name" ) over , * cells allot
does> ( x y -- addr ) dup cell >r @ * cells r> ;
does> ( x y -- addr ) dup cell+ >r @ * + cells r> + ;
5 5 cell-matrix test
5 5 cell-matrix test
Line 137: Line 137:
Double[][] array = new Double[nbr1][nbr2];
Double[][] array = new Double[nbr1][nbr2];
array[0][0] = 42.0;
array[0][0] = 42.0;
System.out.println("The number at place [0 0] is " array[0][0]);
System.out.println("The number at place [0 0] is " + array[0][0]);
} catch(IOException e) { }
} catch(IOException e) { }
Line 152: Line 152:
sub make_array($ $){
sub make_array($ $){
# get array sizes from provided params, but force numeric value
# get array sizes from provided params, but force numeric value
my $x = ($_[0] =~ /^\d $/) ? shift : 0;
my $x = ($_[0] =~ /^\d+$/) ? shift : 0;
my $y = ($_[0] =~ /^\d $/) ? shift : 0;
my $y = ($_[0] =~ /^\d+$/) ? shift : 0;
# define array, then add multi-dimensional elements
# define array, then add multi-dimensional elements

Revision as of 23:29, 23 August 2007

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.

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 oputput that element. Finally destroy the array if not done by the language itself.

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;


C++

With language built-in facilities:

#include <iostream>
#include <istream>
#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;
}

Using std::vector from the standard library:

#include <iostream>
#include <istream>
#include <ostream>
#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()
}

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)

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.

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

Java

import java.io.*;

public class twoDimArray {
  public static void main(String[] args) {
     try {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        
        int nbr1 = Integer.parseInt(in.readLine());
        int nbr2 = Integer.parseInt(in.readLine());
        
        Double[][] array = new Double[nbr1][nbr2];
        array[0][0] = 42.0;
        System.out.println("The number at place [0 0] is " + array[0][0]);
        
     } catch(IOException e) { }
  }
} 


Perl

Interpreter: Perl 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.

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); # thirteeth 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";
  }
}

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 colleted 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.

Python

Interpreter: Python 2.5

 width = int(raw_input("Width of array: "))
 height = int(raw_input("Height of Array: "))
 array = [[0] * width for i in range(height)]
 array[0][0] = 3.5

Note: Some people may instinctively try to write array as [[0] * with] * height, but the * operator creates n references to [[0] * width]

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

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