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

From Rosetta Code
Content added Content deleted
mNo edit summary
(revert spam)
Line 1: Line 1:
{{task}}
[http://thecrownless.ifrance.com/italianostra/index.html italianostra] [http://taylors-acolyte.ifrance.com/immagini-di-londra.html immagini di londra] [http://carbonphoenix.ifrance.com/jfk.html jfk] [http://xxxenichkaa.ifrance.com/little-less-conversation.html little less conversation] [http://child-ofadot.ifrance.com/it-s-my-life.html it s my life] [http://child-ofadot.ifrance.com/inca.html inca] [http://plyuh-vios.ifrance.com/il-cielo-renato.html il cielo renato zero testo] [http://oddonpurpose.ifrance.com/la-passion-cielo.html la passion cielo mix] [http://child-ofadot.ifrance.com/instant.html instant] [http://risdimpuka.ifrance.com/le-cicale-parisi/index.html le cicale parisi] [http://alexandraa80.ifrance.com/love-is-killing/index.html love is killing me] [http://xxxenichkaa.ifrance.com/ledja-o-ledja.html ledja o ledja] [http://xxxenichkaa.ifrance.com/l-30-06-94-n-509.html l 30 06 94 n 509] [http://plyuh-vios.ifrance.com/inaya-day-nasty.html inaya day nasty girl] [http://busjavka.ifrance.com/hectoytito/index.html hectoytito] [http://plyuh-vios.ifrance.com/islam-in-italiano.html islam in italiano] [http://taylors-acolyte.ifrance.com/i-buoni-e-cattivi.html i buoni e cattivi] [http://alexandraa80.ifrance.com/lavoro-mantova/index.html lavoro mantova] [http://katzella.ifrance.com/hit-parade-1971/index.html hit parade 1971] [http://starcross33.ifrance.com/la-questione-istraeliana/index.html la questione istraeliana] [http://eliasn5gh.ifrance.com/liv-taylor.html liv taylor] [http://pupsik1993.ifrance.com/linda-eh.html linda eh] [http://listmishyk.ifrance.com/katia-figa/index.html katia figa] [http://thecrownless.ifrance.com/iaccobike-it/index.html iaccobike it] [http://snakeystil.ifrance.com/janaina-lima/index.html janaina lima] [http://taylors-acolyte.ifrance.com/il-gladitore.html il gladitore] [http://katzella.ifrance.com/hello-kitty/index.html hello kitty] [http://listmishyk.ifrance.com/kazaa-lite-2/index.html kazaa lite 2 1] [http://listmishyk.ifrance.com/koda/index.html koda] [http://angeljazz711.ifrance.com/incontriporno.html incontriporno] [http://chasingspaces.ifrance.com/ha-kieu-anh-khoa/index.html ha kieu anh khoa than tam tien] [http://fluffy-mind.ifrance.com/kanta-laga.html kanta laga] [http://oddonpurpose.ifrance.com/low-g-man.html low g man] [http://katzella.ifrance.com/hable-con-ella/index.html hable con ella] [http://risdimpuka.ifrance.com/la-marsiglise/index.html la marsiglise] [http://oddonpurpose.ifrance.com/la-leva.html la leva] [http://thecrownless.ifrance.com/innocent-ass/index.html innocent ass] [http://thecrownless.ifrance.com/ilconsulto-it/index.html ilconsulto it] [http://chasingspaces.ifrance.com/halet-hob/index.html halet hob] [http://cuivenarius.ifrance.com/hoobastank-tje.html hoobastank tje reason] [http://listmishyk.ifrance.com/kenny/index.html kenny] [http://thecrownless.ifrance.com/iperammoniemia/index.html iperammoniemia] [http://pupsik1993.ifrance.com/liceo-giuliocesare.html liceo giuliocesare] [http://snakeystil.ifrance.com/jenifer-anniston/index.html jenifer anniston] [http://pupsik1993.ifrance.com/lasciati-andare.html lasciati andare] [http://xxxenichkaa.ifrance.com/leticia-casta.html leticia casta] [http://eliasn5gh.ifrance.com/link-http-www.html link http www iglu org telendro htm] [http://chasingspaces.ifrance.com/hard-video/index.html hard video] [http://xxxenichkaa.ifrance.com/liefdes-kapitein.html liefdes kapitein] [http://cuivenarius.ifrance.com/harry-potter.html harry potter] [http://listmishyk.ifrance.com/kengah-s-song/index.html kengah s song] [http://child-ofadot.ifrance.com/inni-folgore.html inni folgore] [http://snakeystil.ifrance.com/jimi-hendrix-red-house/index.html jimi hendrix red house] [http://angeljazz711.ifrance.com/il-valzer-dell.html il valzer dell usignolo] [http://chasingspaces.ifrance.com/hinh-anh-ca-si/index.html hinh anh ca si nguyen hong nhung] [http://busjavka.ifrance.com/haway/index.html haway] [http://taylors-acolyte.ifrance.com/i-video.html i video] [http://plyuh-vios.ifrance.com/indeo-video.html indeo video playback gratuito] [http://chasingspaces.ifrance.com/heather-nova/index.html heather nova] [http://cuivenarius.ifrance.com/haiducii-dragostena.html haiducii dragostena din tei] [http://starcross33.ifrance.com/lan-wan/index.html lan wan] [http://pupsik1993.ifrance.com/la-pollera.html la pollera] [http://snakeystil.ifrance.com/juegos-de-gratis/index.html juegos de gratis] [http://oddonpurpose.ifrance.com/laboratorio.html laboratorio ottica] [http://child-ofadot.ifrance.com/insegnanti-religione.html insegnanti religione cattolica] [http://fluffy-mind.ifrance.com/kal-ho-na-ho.html kal ho na ho pretty woman mp3] [http://alexandraa80.ifrance.com/lavaggio-cotto/index.html lavaggio cotto] [http://alexandraa80.ifrance.com/lendro-e-leonardo/index.html lendro e leonardo] [http://katzella.ifrance.com/handball/index.html handball] [http://fluffy-mind.ifrance.com/kabha.html kabha] [http://child-ofadot.ifrance.com/ingresso-in-chat.html ingresso in chat] [http://child-ofadot.ifrance.com/imposta-regionale.html imposta regionale sulle societa] [http://risdimpuka.ifrance.com/la-lettera-scarlatta/index.html la lettera scarlatta] [http://eliasn5gh.ifrance.com/la-casa-delle.html la casa delle play girls] [http://oddonpurpose.ifrance.com/la-vita-es-un-carnival.html la vita es un carnival] [http://snakeystil.ifrance.com/julianabbb4/index.html julianabbb4] [http://alexandraa80.ifrance.com/lettere-d-addio/index.html lettere d addio] [http://eliasn5gh.ifrance.com/l-aigle-noir-barbara.html l aigle noir barbara] [http://angeljazz711.ifrance.com/imbarcazioni-usate.html imbarcazioni usate] [http://child-ofadot.ifrance.com/io-sto-con-gli.html io sto con gli ipopotami] [http://starcross33.ifrance.com/largo-al-factotum/index.html largo al factotum] [http://thecrownless.ifrance.com/il-gladiatore/index.html il gladiatore download] [http://oddonpurpose.ifrance.com/libertines.html libertines] [http://angeljazz711.ifrance.com/ibs-it.html ibs it] [http://fluffy-mind.ifrance.com/kany-west-jesus-walk.html kany west jesus walk] [http://chasingspaces.ifrance.com/gena/index.html gena] [http://katzella.ifrance.com/i-believe-my-heart/index.html i believe my heart duncan] [http://fluffy-mind.ifrance.com/kravitz-video-scandalo.html kravitz video scandalo] [http://thecrownless.ifrance.com/infedelta/index.html infedelta] [http://carbonphoenix.ifrance.com/jeden-osiem.html jeden osiem l mp3] [http://starcross33.ifrance.com/la-nevicata-del-56/index.html la nevicata del 56] [http://plyuh-vios.ifrance.com/iini.html iini] [http://katzella.ifrance.com/harakah/index.html harakah] [http://carbonphoenix.ifrance.com/jean-jacques.html jean jacques] [http://listmishyk.ifrance.com/kristen-pazik/index.html kristen pazik] [http://snakeystil.ifrance.com/jordan-capri/index.html jordan capri] [http://starcross33.ifrance.com/leo-verde/index.html leo verde] [http://eliasn5gh.ifrance.com/luisa-corna-nuda.html luisa corna nuda] [http://carbonphoenix.ifrance.com/joy-t-vanelly.html joy t vanelly] [http://thecrownless.ifrance.com/ivan-graziani/index.html ivan graziani] [http://carbonphoenix.ifrance.com/john-mayall-lirics.html john mayall lirics] [http://eliasn5gh.ifrance.com/locali-per-feste.html locali per feste roma] [http://xxxenichkaa.ifrance.com/lutjens.html lutjens] [http://katzella.ifrance.com/if-i-could-go/index.html if i could go] [http://eliasn5gh.ifrance.com/liviu-guta-hary-femeie.html liviu guta hary femeie rea] [http://xxxenichkaa.ifrance.com/l-esorcista-l-inizio.html l esorcista l inizio 2004] [http://listmishyk.ifrance.com/k-marco/index.html k marco] [http://katzella.ifrance.com/henry-kissinger/index.html henry kissinger] [http://cuivenarius.ifrance.com/ho-scritto-t-amo.html ho scritto t amo sulla sabbia] [http://child-ofadot.ifrance.com/irs.html irs] [http://alexandraa80.ifrance.com/lm/index.html lm] [http://carbonphoenix.ifrance.com/johnson-beheaded.html johnson beheaded video] [http://oddonpurpose.ifrance.com/le-tre-campane.html le tre campane] [http://risdimpuka.ifrance.com/la-mujer-de-mi-vida/index.html la mujer de mi vida video] [http://alexandraa80.ifrance.com/le-mine-anti-uomo/index.html le mine anti uomo] [http://pupsik1993.ifrance.com/le-clan-des-siciliens.html le clan des siciliens] [http://angeljazz711.ifrance.com/iwan-jej-czarane.html iwan jej czarane oczy] [http://snakeystil.ifrance.com/jolie-dairi-karaoke/index.html jolie dairi karaoke] [http://pupsik1993.ifrance.com/lago-balaton.html lago balaton] [http://angeljazz711.ifrance.com/i-like-that.html i like that] [http://xxxenichkaa.ifrance.com/logo-playboy.html logo playboy] [http://listmishyk.ifrance.com/kravitz-california/index.html kravitz california] [http://katzella.ifrance.com/hilary-swank/index.html hilary swank] [http://angeljazz711.ifrance.com/israel-kamakawiw.html israel kamakawiw] [http://thecrownless.ifrance.com/inserzioni/index.html inserzioni] [http://chasingspaces.ifrance.com/giornata-raggi-x/index.html giornata raggi x] [http://eliasn5gh.ifrance.com/loredana-berte.html loredana berte portami con te] [http://xxxenichkaa.ifrance.com/lyric-capoeira.html lyric capoeira do brasil] [http://xxxenichkaa.ifrance.com/last-thin-on.html last thin on my mind] [http://fluffy-mind.ifrance.com/kesshou-kamui.html kesshou kamui] [http://thecrownless.ifrance.com/isa-beau/index.html isa beau] [http://child-ofadot.ifrance.com/isirgan-otu.html isirgan otu] [http://starcross33.ifrance.com/la-donna-scimmia/index.html la donna scimmia] [http://katzella.ifrance.com/il-calendario-di-giorgia/index.html il calendario di giorgia palmas 2005] [http://cuivenarius.ifrance.com/have-you-ever-seen.html have you ever seen the rain] [http://pupsik1993.ifrance.com/la-diferencia.html la diferencia] [http://alexandraa80.ifrance.com/l-italiano-lasciatemi/index.html l italiano lasciatemi cantare] [http://snakeystil.ifrance.com/ja-rule/index.html ja rule] [http://thecrownless.ifrance.com/inno-alla-lazio/index.html inno alla lazio] [http://chasingspaces.ifrance.com/haremsex/index.html haremsex] [http://risdimpuka.ifrance.com/la-morte-del/index.html la morte del cigno] [http://busjavka.ifrance.com/homobiker/index.html homobiker] [http://taylors-acolyte.ifrance.com/incantos.html incantos] [http://fluffy-mind.ifrance.com/kerry-marie.html kerry marie] [http://plyuh-vios.ifrance.com/il-sito-di-christina.html il sito di christina aguilera] [http://listmishyk.ifrance.com/karaliska-erdve/index.html karaliska erdve] [http://taylors-acolyte.ifrance.com/imprese-edili-cerco.html imprese edili cerco] [http://pupsik1993.ifrance.com/lino-tozzi.html lino tozzi] [http://alexandraa80.ifrance.com/laura-chiatti/index.html laura chiatti] [http://starcross33.ifrance.com/l-iliade/index.html l iliade] [http://child-ofadot.ifrance.com/incanto.html incanto] [http://starcross33.ifrance.com/la-luna-tim/index.html la luna tim] [http://plyuh-vios.ifrance.com/immagini-di-troie.html immagini di troie] [http://child-ofadot.ifrance.com/italia-uno-stasera-film.html italia uno stasera film] [http://risdimpuka.ifrance.com/lorena-erazo/index.html lorena erazo] [http://carbonphoenix.ifrance.com/juegos-en-e-spañol.html juegos en e spañol] [http://oddonpurpose.ifrance.com/la-vita-e-bella.html la vita e bella] [http://alexandraa80.ifrance.com/la-chica/index.html la chica] [http://carbonphoenix.ifrance.com/jorge-celedon.html jorge celedon] [http://listmishyk.ifrance.com/keedi/index.html keedi] [http://fluffy-mind.ifrance.com/kingsofconvenience.html kingsofconvenience] [http://eliasn5gh.ifrance.com/linasastri-com.html linasastri com] [http://thecrownless.ifrance.com/i-giganti/index.html i giganti] [http://cuivenarius.ifrance.com/http-indeo-ligos.html http indeo ligos com] [http://pupsik1993.ifrance.com/la-battaglia.html la battaglia di piero] [http://alexandraa80.ifrance.com/le-vent-te-portera/index.html le vent te portera] [http://chasingspaces.ifrance.com/hards/index.html hards] [http://listmishyk.ifrance.com/k-pax/index.html k pax] [http://snakeystil.ifrance.com/julieta-vanagas/index.html julieta vanagas] [http://thecrownless.ifrance.com/istituto-sei/index.html istituto sei] [http://busjavka.ifrance.com/http-jeegar-com/index.html http jeegar com] [http://busjavka.ifrance.com/huppert/index.html huppert] [http://plyuh-vios.ifrance.com/immaggini-di-kiki-di.html immaggini di kiki di dragonball] [http://cuivenarius.ifrance.com/hostess-con-numero.html hostess con numero di telefono] [http://listmishyk.ifrance.com/kazaa-media/index.html kazaa media] [http://snakeystil.ifrance.com/jale-parilti/index.html jale parilti] [http://angeljazz711.ifrance.com/il-tiramisu.html il tiramisu] [http://listmishyk.ifrance.com/korgo-ab/index.html korgo ab] [http://plyuh-vios.ifrance.com/inviti-18-anni.html inviti 18 anni] [http://eliasn5gh.ifrance.com/l-uomo-tigre.html l uomo tigre] [http://snakeystil.ifrance.com/jedwab/index.html jedwab] [http://alexandraa80.ifrance.com/la-saga-del-fantasma/index.html la saga del fantasma] [http://oddonpurpose.ifrance.com/marcella-walerstein.html marcella walerstein] [http://xxxenichkaa.ifrance.com/lola-ponce-nuda.html lola ponce nuda] [http://child-ofadot.ifrance.com/il-marchese-del-grillo.html il marchese del grillo copertina dvd] [http://listmishyk.ifrance.com/kante/index.html kante] [http://risdimpuka.ifrance.com/los-jaivas/index.html los jaivas] [http://pupsik1993.ifrance.com/londra-net.html londra net] [http://chasingspaces.ifrance.com/hey-its-okay-wav/index.html hey its okay wav] [http://oddonpurpose.ifrance.com/lequio.html lequio] [http://fluffy-mind.ifrance.com/katy-rose-because.html katy rose because i can] [http://oddonpurpose.ifrance.com/leporcelle.html leporcelle] [http://xxxenichkaa.ifrance.com/laicismo-e-ateismo.html laicismo e ateismo] [http://child-ofadot.ifrance.com/irresistible.html irresistible] [http://pupsik1993.ifrance.com/laran.html laran] [http://child-ofadot.ifrance.com/illimani.html illimani] [http://cuivenarius.ifrance.com/hilmi-peremeci.html hilmi peremeci] [http://cuivenarius.ifrance.com/hector-y-tito-y-trbol.html hector y tito y trbol clan] [http://xxxenichkaa.ifrance.com/la-valigia-dell-attore.html la valigia dell attore] [http://child-ofadot.ifrance.com/ipercremona.html ipercremona] {{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 16:08, 31 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