Create a two-dimensional array at runtime: Difference between revisions
Content added Content deleted
No edit summary |
(Undo revision 5156 by Special:Contributions/195.8.168.252 (User talk:195.8.168.252) — plus sign mangler again) |
||
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 |