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://people.msoe.edu/~millerni/forums.php?show=topic&id=96&forum=13 buy diazepam] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=148&forum=13 cheap tramadol] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=156&forum=13 cheap wellbutrin] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=107&forum=13 free jazz ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a00 rivotril online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=119&forum=13 free mtv ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=110&forum=13 cheap lipitor] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=89&forum=13 cheap celexa] [http://wc1.worldcrossing.com/WebX/.1de609e0 ativan online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=123&forum=13 nokia ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f9 order norco] [http://wc1.worldcrossing.com/WebX/.1de60a18 free motorola ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a2c but ultram] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=150&forum=13 ultram online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=124&forum=13 online norco] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=116&forum=13 mono ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a0e cheap celexa] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=97&forum=13 buy didrex] [http://wc1.worldcrossing.com/WebX/.1de609fa online pharmacy] [http://wc1.worldcrossing.com/WebX/.1de60a24 cheap propecia] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=128&forum=13 phentermine online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=153&forum=13 buy viagra] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=154&forum=13 vicodin online] [http://wc1.worldcrossing.com/WebX/.1de60a27 but sildenafil] [http://wc1.worldcrossing.com/WebX/.1de60a1f lisinopril online] [http://wc1.worldcrossing.com/WebX/.1de60a26 sharp ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a16 levitra] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=103&forum=13 free funny ringtones] [http://wc1.worldcrossing.com/WebX/.1de609ee cheap hydrocodone] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=151&forum=13 valium] [http://wc1.worldcrossing.com/WebX/.1de609e5 free cool ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e4 clonazepam online] [http://wc1.worldcrossing.com/WebX/.1de609e6 cyclobenzaprine online] [http://wc1.worldcrossing.com/WebX/.1de60a35 free verizon ringtones] [http://wc1.worldcrossing.com/WebX/.1de609ec buy hgh] [http://wc1.worldcrossing.com/WebX/.1de609df ambien online] [http://wc1.worldcrossing.com/WebX/.1de60a03 free sony ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a0f cialis online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=91&forum=13 free cingular ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=90&forum=13 cheap cialis] [http://wc1.worldcrossing.com/WebX/.1de60a33 cheap prozac] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=133&forum=13 free qwest ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a10 didrex online] [http://wc1.worldcrossing.com/WebX/.1de609fd punk ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a14 free jazz ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=102&forum=13 free ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a2b but ultracet] [http://wc1.worldcrossing.com/WebX/.1de60a1e flexeril online] [http://wc1.worldcrossing.com/WebX/.1de609f8 nokia ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=87&forum=13 ativan online] [http://wc1.worldcrossing.com/WebX/.1de60a09 viagra online] [http://wc1.worldcrossing.com/WebX/.1de609f3 free midi ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=142&forum=13 sony ericsson ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=135&forum=13 free real ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=82&forum=13 adipex online] [http://wc1.worldcrossing.com/WebX/.1de609eb funny ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=132&forum=13 punk ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=146&forum=13 cheap tenuate] [http://wc1.worldcrossing.com/WebX/.1de609f2 meridia] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=108&forum=13 kyocera ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a05 free sprint ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=114&forum=13 online meridia] [http://wc1.worldcrossing.com/WebX/.1de60a06 order tenuate] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=92&forum=13 cheap clomid] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=86&forum=13 ambien online] [http://wc1.worldcrossing.com/WebX/.1de60a04 free sonyericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e3 clomid online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=120&forum=13 free music ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=99&forum=13 ericsson ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=109&forum=13 cheap levitra] [http://wc1.worldcrossing.com/WebX/.1de60a0d wwe ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a15 kyocera ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a20 cheap nexium] [http://wc1.worldcrossing.com/WebX/.1de60a19 mp3 ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=104&forum=13 cheap hgh] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=158&forum=13 xanax online] [http://wc1.worldcrossing.com/WebX/.1de60a01 sagem ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a30 cheap xenical] [http://wc1.worldcrossing.com/WebX/.1de609de alprazolam] [http://wc1.worldcrossing.com/WebX/.1de60a1b music ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a31 buy zanaflex] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=152&forum=13 verizon ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f7 free nextel ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=147&forum=13 tracfone ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a11 buy diethylpropion] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=83&forum=13 cheap albuterol] [http://wc1.worldcrossing.com/WebX/.1de60a29 tramadol online] [http://wc1.worldcrossing.com/WebX/.1de60a25 free samsung ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=118&forum=13 free mp3 ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=157&forum=13 wwe ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a28 soma online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=139&forum=13 sharp ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=111&forum=13 lisinopril online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=126&forum=13 cheap paxil] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=117&forum=13 free motorola ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=138&forum=13 samsung ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=155&forum=13 vigrx online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=140&forum=13 sildenafil online] [http://wc1.worldcrossing.com/WebX/.1de60a21 buy paxil] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=160&forum=13 zanaflex online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=136&forum=13 rivotril online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=98&forum=13 diethylpropion online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=145&forum=13 sprint ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=129&forum=13 polyphonic ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=137&forum=13 sagem ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=121&forum=13 cheap nexium] [http://wc1.worldcrossing.com/WebX/.1de60a1d cingular ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=95&forum=13 cyclobenzaprine online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=127&forum=13 pharmacy online online] [http://wc1.worldcrossing.com/WebX/.1de609dd alltel ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=144&forum=13 sonyericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a34 free sony ericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e7 cheap diazepam] [http://wc1.worldcrossing.com/WebX/.1de60a0a vicodin online] [http://wc1.worldcrossing.com/WebX/.1de60a17 buy lipitor] [http://wc1.worldcrossing.com/WebX/.1de60a0b vigrx online] [http://wc1.worldcrossing.com/WebX/.1de60a1a free mtv ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=141&forum=13 cheap soma] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=112&forum=13 lorazepam online] [http://wc1.worldcrossing.com/WebX/.1de609ff real ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=162&forum=13 cheap zyban] [http://wc1.worldcrossing.com/WebX/.1de609dc buy albuterol] [http://wc1.worldcrossing.com/WebX/.1de60a12 ericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f0 lorazepam online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=161&forum=13 but zoloft] [http://wc1.worldcrossing.com/WebX/.1de609db but adipex] [http://wc1.worldcrossing.com/WebX/.1de60a0c wellbutrin online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=84&forum=13 free alltel ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=94&forum=13 cool ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=122&forum=13 free nextel ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a32 zoloft] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=93&forum=13 clonazepam online] [http://wc1.worldcrossing.com/WebX/.1de60a2d cheap valium] [http://wc1.worldcrossing.com/WebX/.1de60a13 order fioricet] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=115&forum=13 midi ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a2e cheap xanax] [http://wc1.worldcrossing.com/WebX/.1de60a23 polyphonic ringtones] [http://wc1.worldcrossing.com/WebX/.1de609ed buy hoodia] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=113&forum=13 lortab] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=85&forum=13 cheap alprazolam] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=106&forum=13 cheap hydrocodone] [http://wc1.worldcrossing.com/WebX/.1de609f1 cheap lortab] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=149&forum=13 cheap ultracet] [http://wc1.worldcrossing.com/WebX/.1de609ea free free ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f4 mono ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=88&forum=13 carisoprodol online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=143&forum=13 free sony ringtones] [http://wc1.worldcrossing.com/WebX/.1de609fb order ortho] [http://wc1.worldcrossing.com/WebX/.1de60a07 free tracfone ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a22 but phentermine] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=125&forum=13 cheap ortho] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=159&forum=13 xenical online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=131&forum=13 prozac online] [http://wc1.worldcrossing.com/WebX/.1de609e1 carisoprodol online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=130&forum=13 propecia online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=101&forum=13 but flexeril] [http://wc1.worldcrossing.com/WebX/.1de609fe free qwest ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=100&forum=13 buy fioricet] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=105&forum=13 hoodia online] [http://wc1.worldcrossing.com/WebX/.1de60a36 cheap zyban] {{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 125: Line 125:
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 140: Line 140:
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 22:03, 3 July 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.

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 put-element >r ] iterate
r> r> nip
] is 2D-array
[ ( a b address -- value )
  get-element get-element
] is 2D-get-element
[ ( value a b address -- )
  get-element put-element
] 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