Create a two-dimensional array at runtime: Difference between revisions
mNo edit summary |
(Undo revision 6101 by Special:Contributions/ZawUjj (User talk:ZawUjj)) |
||
Line 1: | Line 1: | ||
{{task}} |
|||
[http://people.msoe.edu/~millerni/forums.php?show=topic&id=114&forum=13 meridia] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=157&forum=13 wwe ringtones] [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=158&forum=13 xanax online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=106&forum=13 hydrocodone online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=160&forum=13 cheap zanaflex] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=102&forum=13 free ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=97&forum=13 buy didrex] [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=100&forum=13 buy fioricet] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=96&forum=13 diazepam 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=87&forum=13 ativan online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=144&forum=13 sonyericsson ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=137&forum=13 free sagem ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=152&forum=13 free verizon ringtones] [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=125&forum=13 ortho online] [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=151&forum=13 valium online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=121&forum=13 cheap nexium] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=147&forum=13 free tracfone ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=142&forum=13 free sony ericsson 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=161&forum=13 but zoloft] [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=133&forum=13 qwest ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=138&forum=13 free samsung ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=122&forum=13 free nextel ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=89&forum=13 cheap celexa] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=99&forum=13 free ericsson ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=115&forum=13 free midi ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=146&forum=13 cheap tenuate] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=109&forum=13 cheap levitra] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=82&forum=13 adipex online] [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=130&forum=13 cheap propecia] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=83&forum=13 albuterol online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=85&forum=13 alprazolam 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=148&forum=13 tramadol online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=143&forum=13 sony ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=86&forum=13 ambien online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=103&forum=13 funny 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=129&forum=13 free polyphonic ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=90&forum=13 cheap cialis] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=123&forum=13 nokia ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=101&forum=13 flexeril] [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=149&forum=13 ultracet online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=88&forum=13 cheap carisoprodol] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=108&forum=13 kyocera ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=93&forum=13 clonazepam online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=127&forum=13 cheap pharmacy online] [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=150&forum=13 cheap ultram] [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=159&forum=13 xenical online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=162&forum=13 zyban online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=116&forum=13 free mono ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=112&forum=13 lorazepam online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=145&forum=13 free sprint ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=110&forum=13 lipitor online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=131&forum=13 prozac online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=120&forum=13 music ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=105&forum=13 hoodia online] [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=98&forum=13 cheap diethylpropion] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=113&forum=13 but lortab] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=154&forum=13 vicodin 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=84&forum=13 alltel ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=141&forum=13 soma online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=107&forum=13 jazz ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=140&forum=13 cheap sildenafil] [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=118&forum=13 free mp3 ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=92&forum=13 clomid online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=91&forum=13 free cingular ringtones] {{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 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 " |
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 |
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 17:33, 30 June 2007
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