Create a two-dimensional array at runtime: Difference between revisions
mNo edit summary |
(revert spam) |
||
Line 1: | Line 1: | ||
{{task}} |
|||
[http://kalian42.ifrance.com/text/directory/tequila-baby/ tequila baby 51] [http://tizolaa.ifrance.com/web/pastiglie-leone/ pastiglie leone] [http://kalian42.ifrance.com/text/directory/incontro-amore/ incontro amore] [http://demurediablo.ifrance.com/library/html/coppie-non/ coppie non certificate] [http://akmokanzen.ifrance.com/images/small/disegno-di.htm disegno di case] [http://singaporepets.ifrance.com/library/html/zyxel-switch.htm zyxel switch gs-1116] [http://yeeden.ifrance.com/img/styles/nicotina-stop/ nicotina stop torino] [http://voltia.ifrance.com/content/view/hd.htm hd 120] [http://deviantrus.ifrance.com/topic/la-strega/ la strega di classe] [http://singaporepets.ifrance.com/library/html/offerta-regali.htm offerta regali parma] [http://noxuhax.ifrance.com/content/view/carrozzine-jane.htm carrozzine jane matrix slalom] [http://deviantrus.ifrance.com/topic/run-time/ run time 0701] [http://lordsander.ifrance.com/topic/tisuby-y.htm tisuby y georgina] [http://akmokanzen.ifrance.com/images/small/oslo-ristorante.htm oslo ristorante] [http://highbulp.ifrance.com/images/small/streets/ streets] [http://voltia.ifrance.com/content/view/samsung-digimax.htm samsung digimax 250] [http://yeeden.ifrance.com/img/styles/residenze-in/ residenze in costa smeralda] [http://noxuhax.ifrance.com/content/view/fotos-de.htm fotos de espiritus] [http://singaporepets.ifrance.com/library/html/porno-casalinghe.htm porno casalinghe] [http://noxuhax.ifrance.com/content/view/www-concorsi.htm www concorsi guardia forestale it] [http://yeeden.ifrance.com/img/styles/playboy-the/ playboy the mansion] [http://oiyaoi.ifrance.com/images/small/india-import/ india import] [http://tat-ooin.ifrance.com/resources/articles/bici-elettrica.htm bici elettrica alluminio] [http://demurediablo.ifrance.com/library/html/christina-aguilera/ christina aguilera lingerie] [http://lordsander.ifrance.com/topic/tv-.htm tv 30 lcd] [http://voltia.ifrance.com/content/view/marinai-in.htm marinai in coperta] [http://voltia.ifrance.com/content/view/ras-al.htm ras al khaimah (emirato)] [http://girlwho-is.ifrance.com/img/styles/sigla-th.htm sigla 7th heaven] [http://kalian42.ifrance.com/text/directory/annuncio-lavoro/ annuncio lavoro bergamo] [http://oiyaoi.ifrance.com/images/small/aconaemys/ aconaemys] [http://igaros.ifrance.com/lib/lc/ lc5] [http://speedofsoun.ifrance.com/text/directory/tempo-it.htm tempo it] [http://singaporepets.ifrance.com/library/html/el-departamento.htm el departamento de ayacucho] [http://igaros.ifrance.com/lib/domiciliazione-bancaria/ domiciliazione bancaria] [http://tizolaa.ifrance.com/web/tflash-card/ t-flash card] [http://oiyaoi.ifrance.com/images/small/will-be/ will be loved] [http://highbulp.ifrance.com/images/small/california-ristorante/ california ristorante] [http://voltia.ifrance.com/content/view/agrotis.htm agrotis] [http://toofarfrommaybe.ifrance.com/view/new/cover-cd/ cover cd musicale] [http://demurediablo.ifrance.com/library/html/investimento-immobiliare/ investimento immobiliare] [http://igaros.ifrance.com/lib/da-pavimento/ da pavimento] [http://deviantrus.ifrance.com/topic/www-tapiceria/ www tapiceria con] [http://akmokanzen.ifrance.com/images/small/manuela-arcuri.htm manuela arcuri prende cazzi] [http://lovereceier.ifrance.com/lib/offerta-pc.htm offerta pc antivirus] [http://oiyaoi.ifrance.com/images/small/porsche-carrera/ porsche carrera 4 s] [http://tizolaa.ifrance.com/web/tarfaya/ tarfaya] [http://highbulp.ifrance.com/images/small/corrado-pani/ corrado pani] [http://deviantrus.ifrance.com/topic/auto-noleggio/ auto noleggio faenza] [http://kalian42.ifrance.com/text/directory/palmare-con/ palmare con gps e kit auto] [http://noxuhax.ifrance.com/content/view/yamaha-motor.htm yamaha motor] [http://lovereceier.ifrance.com/lib/fujifilm-a.htm fujifilm a 210] [http://singaporepets.ifrance.com/library/html/lady-b.htm lady b] [http://demurediablo.ifrance.com/library/html/agriturismo-cagliari/ agriturismo cagliari] [http://voltia.ifrance.com/content/view/netsky-p.htm netsky p] [http://broadwaylili.ifrance.com/content/view/il-maresciallo.htm il maresciallo rocca. stagione 4. episodio 2] [http://demurediablo.ifrance.com/library/html/rete-pesca/ rete pesca] [http://kalian42.ifrance.com/text/directory/golden-miners/ golden miners] [http://tizolaa.ifrance.com/web/trittico-d/ trittico d amore] [http://girlwho-is.ifrance.com/img/styles/badescu-sexy.htm badescu sexy] [http://kalian42.ifrance.com/text/directory/canzoni-i/ canzoni i cento passi] [http://singaporepets.ifrance.com/library/html/karaoke-nannini.htm karaoke nannini i maschi] [http://tizolaa.ifrance.com/web/kochaj-mnie/ kochaj mnie] [http://toofarfrommaybe.ifrance.com/view/new/mi-piace/ mi piace guardare le donne in gonna] [http://akmokanzen.ifrance.com/images/small/foto-befane.htm foto befane] [http://tizolaa.ifrance.com/web/piedi-sexi/ piedi sexi donne] [http://broadwaylili.ifrance.com/content/view/dvd-.htm dvd - rw] [http://akmokanzen.ifrance.com/images/small/ghiaccio-princess.htm ghiaccio princess] [http://toofarfrommaybe.ifrance.com/view/new/scandali-sex/ scandali sex vip] [http://girlwho-is.ifrance.com/img/styles/dlink.htm d-link 604] [http://jdawsona.ifrance.com/data/nano-/ nano 2 gb] [http://deviantrus.ifrance.com/topic/arredamento-ufficio/ arredamento ufficio negozi] [http://singaporepets.ifrance.com/library/html/km-stilo.htm km0 stilo auto km 0] [http://lordsander.ifrance.com/topic/textos-informativos.htm textos informativos de la literatura] [http://igaros.ifrance.com/lib/gioco-di/ gioco di enigmi] [http://lovereceier.ifrance.com/lib/netgear-wgpsfs.htm netgear wgps606fs] [http://akmokanzen.ifrance.com/images/small/x-a.htm x 43a] [http://igaros.ifrance.com/lib/gloge-com/ gloge com] [http://lordsander.ifrance.com/topic/www-foto.htm www foto alessandrapierelli com] [http://akmokanzen.ifrance.com/images/small/lettore-dvd.htm lettore dvd disney] [http://singaporepets.ifrance.com/library/html/tutti-i.htm tutti i giorni] [http://tat-ooin.ifrance.com/resources/articles/finanziamento-catanzaro.htm finanziamento catanzaro] [http://akmokanzen.ifrance.com/images/small/cartiere-burgo.htm cartiere burgo] [http://singaporepets.ifrance.com/library/html/headset-hs.htm headset hs-300] [http://yeeden.ifrance.com/img/styles/lady-chatterley/ lady chatterley story] [http://yeeden.ifrance.com/img/styles/geosat-/ geosat 2 navigatore satellitare] [http://toofarfrommaybe.ifrance.com/view/new/samsung-c/ samsung c100] [http://oiyaoi.ifrance.com/images/small/you-get/ you get what you give] [http://lordsander.ifrance.com/topic/hotel-in.htm hotel in tunisia] [http://jdawsona.ifrance.com/data/televisori-terrestre/ televisori terrestre integrato] [http://deviantrus.ifrance.com/topic/ricevitore-/ ricevitore - gps] [http://deviantrus.ifrance.com/topic/adult-comics/ adult comics] [http://voltia.ifrance.com/content/view/radio-am.htm radio am fm lw] [http://singaporepets.ifrance.com/library/html/gullo.htm gullo] [http://deviantrus.ifrance.com/topic/caroline-hard/ caroline hard] [http://tizolaa.ifrance.com/web/lavori-in/ lavori in pasta sale] [http://singaporepets.ifrance.com/library/html/valgrana.htm valgrana] [http://tat-ooin.ifrance.com/resources/articles/craig-amstrong.htm craig amstrong] [http://tat-ooin.ifrance.com/resources/articles/elettromedicale.htm elettromedicale] [http://lordsander.ifrance.com/topic/max-scopate.htm max scopate pompini inculate] [http://highbulp.ifrance.com/images/small/sony-mavica/ sony mavica mvc fd92] [http://tizolaa.ifrance.com/web/piliere/ piliere] [http://tat-ooin.ifrance.com/resources/articles/lcd-da.htm lcd da 20] [http://deviantrus.ifrance.com/topic/salemme-films/ salemme films] [http://girlwho-is.ifrance.com/img/styles/www-sexo.htm www sexo sexo com mx] [http://kalian42.ifrance.com/text/directory/vaio-vgnshp/ vaio vgn-s4hp b] [http://akmokanzen.ifrance.com/images/small/polmoni.htm polmoni] [http://broadwaylili.ifrance.com/content/view/dvd-portatile.htm dvd portatile tv fm] [http://jdawsona.ifrance.com/data/sata-pata/ sata pata] [http://igaros.ifrance.com/lib/vendita-gioielli/ vendita gioielli] [http://singaporepets.ifrance.com/library/html/hard-disk.htm hard disk maxtor max line iii] [http://girlwho-is.ifrance.com/img/styles/ra-srl.htm r2a srl] [http://jdawsona.ifrance.com/data/un-controller/ un controller universale per xbox 2 e pc] [http://broadwaylili.ifrance.com/content/view/tiziano-ferro.htm tiziano ferro feat jamelia universal] [http://tizolaa.ifrance.com/web/goliath/ goliath] [http://kalian42.ifrance.com/text/directory/nike-lethal/ nike lethal] [http://oiyaoi.ifrance.com/images/small/www-bravoclub/ www bravoclub it] [http://singaporepets.ifrance.com/library/html/mozzarelle-fior.htm mozzarelle fior di salento] [http://highbulp.ifrance.com/images/small/primavera-calcio/ primavera calcio] [http://noxuhax.ifrance.com/content/view/dragonstea-din.htm dragonstea din dia] [http://yeeden.ifrance.com/img/styles/pornofree/ pornofree] [http://tizolaa.ifrance.com/web/tromba-cubana/ tromba cubana] [http://toofarfrommaybe.ifrance.com/view/new/titjob/ titjob] [http://lordsander.ifrance.com/topic/asus-pps.htm asus p4p800s x] [http://speedofsoun.ifrance.com/text/directory/bigmamas-com.htm bigmamas com] [http://girlwho-is.ifrance.com/img/styles/storie-di.htm storie di montagna] [http://broadwaylili.ifrance.com/content/view/tone-mai.htm tone mai] [http://girlwho-is.ifrance.com/img/styles/abit-is.htm abit is10] [http://tizolaa.ifrance.com/web/mastri/ mastri] [http://deviantrus.ifrance.com/topic/spartaco-lega/ spartaco, lega di] [http://jdawsona.ifrance.com/data/video-di/ video di ragazze con tette grosse] [http://noxuhax.ifrance.com/content/view/mp-player.htm mp3 player 1g] [http://broadwaylili.ifrance.com/content/view/counting-crows.htm counting crows blof holiday in spain] [http://lovereceier.ifrance.com/lib/stampante-fax.htm stampante fax telefono scanner laser] [http://noxuhax.ifrance.com/content/view/dediche-e.htm dediche e poesie] [http://noxuhax.ifrance.com/content/view/dolce-gabbana.htm dolce gabbana tailleur] [http://demurediablo.ifrance.com/library/html/discoteche-lombarde/ discoteche lombarde] [http://speedofsoun.ifrance.com/text/directory/anydvd-v.htm anydvd v3 6 1 1] [http://igaros.ifrance.com/lib/gps-compact/ gps compact] [http://tizolaa.ifrance.com/web/qtek-/ qtek 9090 supporto] [http://lovereceier.ifrance.com/lib/fujifilm-.htm fujifilm - xd picture card 512mb] [http://jdawsona.ifrance.com/data/provincia-vi/ provincia vi] [http://noxuhax.ifrance.com/content/view/lettori-mp.htm lettori mp3 20 gb acer] [http://jdawsona.ifrance.com/data/candice-cardinelle/ candice cardinelle pics] [http://voltia.ifrance.com/content/view/meri-naseeb.htm meri naseeb] [http://lordsander.ifrance.com/topic/mito-immobiliare.htm mito immobiliare] [http://yeeden.ifrance.com/img/styles/iuniverse-com/ iuniverse com] [http://kalian42.ifrance.com/text/directory/testo-zombie/ testo zombie dei cramberries] [http://broadwaylili.ifrance.com/content/view/models-nude.htm models nude] [http://kalian42.ifrance.com/text/directory/fotocamere-reflex/ fotocamere reflex digitali pentax] [http://yeeden.ifrance.com/img/styles/danzel-pump/ danzel pump in up] [http://lovereceier.ifrance.com/lib/gaetano-donizetti.htm gaetano donizetti. linda di chamounix] [http://lovereceier.ifrance.com/lib/concorsi-a.htm concorsi a palermo] [http://speedofsoun.ifrance.com/text/directory/benq-pe.htm benq pe7700] [http://demurediablo.ifrance.com/library/html/lavatrice-alto/ lavatrice alto] [http://igaros.ifrance.com/lib/sa-illetta/ sa illetta] [http://singaporepets.ifrance.com/library/html/www-funda.htm www funda nl] [http://girlwho-is.ifrance.com/img/styles/berlin.htm berlin] [http://akmokanzen.ifrance.com/images/small/nintendo.htm nintendo 64] [http://voltia.ifrance.com/content/view/ragazzi-teramo.htm ragazzi teramo] [http://singaporepets.ifrance.com/library/html/nubiani.htm nubiani] [http://yeeden.ifrance.com/img/styles/opel-astra/ opel astra 1.9 cdti cosmo] [http://igaros.ifrance.com/lib/contedracula/ contedracula] [http://girlwho-is.ifrance.com/img/styles/asciugabiancheria-zoppas.htm asciugabiancheria zoppas] [http://broadwaylili.ifrance.com/content/view/nicolas-maquiavelo.htm nicolas maquiavelo] [http://girlwho-is.ifrance.com/img/styles/this-love.htm this love maron 5] [http://singaporepets.ifrance.com/library/html/dvd-verbatim.htm dvd verbatim 16x vergini] [http://akmokanzen.ifrance.com/images/small/www-cartoonnetwork.htm www cartoonnetwork it] [http://demurediablo.ifrance.com/library/html/breath-of/ breath of fire iv] [http://jdawsona.ifrance.com/data/evangelisti/ evangelisti] [http://tat-ooin.ifrance.com/resources/articles/sabrina-paravicini.htm sabrina paravicini] [http://singaporepets.ifrance.com/library/html/palomita.htm palomita] [http://akmokanzen.ifrance.com/images/small/chiavi-inglesi.htm chiavi inglesi] [http://lordsander.ifrance.com/topic/auto-noleggio.htm auto noleggio orvieto] [http://speedofsoun.ifrance.com/text/directory/cosoleto.htm cosoleto] [http://girlwho-is.ifrance.com/img/styles/la-cosa.htm la cosa leader] [http://voltia.ifrance.com/content/view/divx-codec.htm divx codec for linux 5.0.5] [http://lovereceier.ifrance.com/lib/archos.htm archos] [http://kalian42.ifrance.com/text/directory/programmi-enalotto/ programmi enalotto] [http://deviantrus.ifrance.com/topic/bocche/ bocche] [http://tizolaa.ifrance.com/web/acer-cs/ acer cs 5530] [http://oiyaoi.ifrance.com/images/small/ponte-stretto/ ponte stretto] [http://yeeden.ifrance.com/img/styles/morti-violente/ morti violente] [http://noxuhax.ifrance.com/content/view/lucy-lee.htm lucy lee] [http://lovereceier.ifrance.com/lib/elba-vacanza.htm elba vacanza] [http://yeeden.ifrance.com/img/styles/girmi-gl/ girmi gl] [http://yeeden.ifrance.com/img/styles/palle/ palle] [http://deviantrus.ifrance.com/topic/usb-memoria/ usb memoria ram] [http://girlwho-is.ifrance.com/img/styles/emas-com.htm emas com mx] [http://oiyaoi.ifrance.com/images/small/cz-zen/ cz zen] [http://tat-ooin.ifrance.com/resources/articles/don-t.htm don t go tuu fast] [http://voltia.ifrance.com/content/view/flush.htm flush] [http://deviantrus.ifrance.com/topic/tejo/ tejo] [http://demurediablo.ifrance.com/library/html/sunfire/ sunfire] [http://yeeden.ifrance.com/img/styles/e/ e 700] [http://lovereceier.ifrance.com/lib/zuigen-v.htm zuigen v] [http://kalian42.ifrance.com/text/directory/carrera-/ carrera 4 s] [http://yeeden.ifrance.com/img/styles/michele-cassano/ michele cassano] [http://akmokanzen.ifrance.com/images/small/capo-verde.htm capo verde villaggio] [http://lovereceier.ifrance.com/lib/rettore-nuda.htm rettore nuda] [http://speedofsoun.ifrance.com/text/directory/notti-oriente.htm notti oriente] {{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; |
for (int i = 0; i < dim1; ++i) |
||
array[i] = array_data |
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 |
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 " |
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 |
my $x = ($_[0] =~ /^\d+$/) ? shift : 0; |
||
my $y = ($_[0] =~ /^\d |
my $y = ($_[0] =~ /^\d+$/) ? shift : 0; |
||
# define array, then add multi-dimensional elements |
# define array, then add multi-dimensional elements |
Revision as of 16:40, 19 August 2007
![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.
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