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

Content added Content deleted
m (→‎{{header|AppleScript}}: Typo correction.)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 554:
return 0;
}</lang>
 
=={{header|C sharp|C#}}==
<lang csharp>
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter two integers. Space delimited please: ");
string s = Console.ReadLine();
int[,] myArray=new int[(int)s[0],(int)s[2]];
myArray[0, 0] = 2;
Console.WriteLine(myArray[0, 0]);
 
Console.ReadLine();
}
}</lang>
 
=={{header|C++}}==
Line 655 ⟶ 672:
return EXIT_FAILURE;
}</lang>
 
=={{header|C sharp|C#}}==
<lang csharp>
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter two integers. Space delimited please: ");
string s = Console.ReadLine();
int[,] myArray=new int[(int)s[0],(int)s[2]];
myArray[0, 0] = 2;
Console.WriteLine(myArray[0, 0]);
 
Console.ReadLine();
}
}</lang>
 
=={{header|Clean}}==
Line 682:
(_, dim2, console) = freadi console
= createArray dim1 (createArray dim2 1.0)</lang>
 
 
=={{header|Clojure}}==
Line 904 ⟶ 903:
In position 3 4 we have 20
</pre>
 
=={{header|ERRE}}==
In ERRE language arrays created at run-time is "dynamic arrays". For this task will be enough this code:
Line 940:
 
printf(1,"array[%d][%d] is %d\n", {i,j,array[i][j]})</lang>
 
=={{header|F Sharp|F#}}==
<lang fsharp>open System
 
let width = int( Console.ReadLine() )
let height = int( Console.ReadLine() )
let arr = Array2D.create width height 0
arr.[0,0] <- 42
printfn "%d" arr.[0,0]</lang>
 
=={{header|Factor}}==
Line 1,008 ⟶ 1,017:
</lang>
 
=={{header|F Sharp|F#}}==
<lang fsharp>open System
 
let width = int( Console.ReadLine() )
let height = int( Console.ReadLine() )
let arr = Array2D.create width height 0
arr.[0,0] <- 42
printfn "%d" arr.[0,0]</lang>
=={{header|GAP}}==
<lang gap># Creating an array of 0
Line 1,284 ⟶ 1,285:
 
would produce: <lang jq>[[0,0,0,0],[0,0,99,0],[0,0,0,0]]</lang>
 
 
=={{header|Julia}}==
Line 1,387:
array[[1,1]]
]</lang>
 
 
=={{header|MATLAB}} / {{header|Octave}}==
Line 1,605 ⟶ 1,604:
Arr.1.1 := 42
{Show Arr.1.1}</lang>
 
=={{header|PARI/GP}}==
<lang parigp>tmp(m,n)={
Line 1,691 ⟶ 1,690:
> 0 1 0
> 0 0 0</lang>
 
=={{header|Perl 6}}==
{{works with|rakudo|2015-11-28}}
Line 1: The input parse doesn't care how you separate the dimensions as long as there are two distinct numbers.
 
Line 2: The list replication operator <tt>xx</tt> will automatically thunkify its left side so this produces new subarrays for each replication.
 
Line 3: Subscripting with a closure automatically passes the size of the dimension to the closure, so we pick an appropriate random index on each level.
 
Line 4: Print each line of the array.
<lang perl6>my ($major,$minor) = prompt("Dimensions? ").comb(/\d+/);
my @array = [ '@' xx $minor ] xx $major;
@array[ *.rand ][ *.rand ] = ' ';
.say for @array;</lang>
 
Typical run:
<lang>Dimensions? 5x35
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
</lang>
 
The most recent versions of Rakudo have preliminary support for 'shaped arrays'. Natively shaped arrays are a flexible feature for declaring typed, potentially multi-dimensional arrays, potentially with pre-defined
dimensions. They will make memory-efficient matrix storage and matrix operations possible.
 
<lang perl6>my ($major,$minor) = +«prompt("Dimensions? ").comb(/\d+/);
my Int @array[$major;$minor] = (7 xx $minor ) xx $major;
@array[$major div 2;$minor div 2] = 42;
say @array;</lang>
 
Typical run:
<lang>Dimensions? 3 x 10
[[7 7 7 7 7 7 7 7 7 7] [7 7 7 7 7 42 7 7 7 7] [7 7 7 7 7 7 7 7 7 7]]</lang>
 
=={{header|Phix}}==
Line 1,954 ⟶ 1,918:
'#(#(1 0 0) #(0 0 0) #(0 0 9))
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-11-28}}
Line 1: The input parse doesn't care how you separate the dimensions as long as there are two distinct numbers.
 
Line 2: The list replication operator <tt>xx</tt> will automatically thunkify its left side so this produces new subarrays for each replication.
 
Line 3: Subscripting with a closure automatically passes the size of the dimension to the closure, so we pick an appropriate random index on each level.
 
Line 4: Print each line of the array.
<lang perl6>my ($major,$minor) = prompt("Dimensions? ").comb(/\d+/);
my @array = [ '@' xx $minor ] xx $major;
@array[ *.rand ][ *.rand ] = ' ';
.say for @array;</lang>
 
Typical run:
<lang>Dimensions? 5x35
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
[@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @]
</lang>
 
The most recent versions of Rakudo have preliminary support for 'shaped arrays'. Natively shaped arrays are a flexible feature for declaring typed, potentially multi-dimensional arrays, potentially with pre-defined
dimensions. They will make memory-efficient matrix storage and matrix operations possible.
 
<lang perl6>my ($major,$minor) = +«prompt("Dimensions? ").comb(/\d+/);
my Int @array[$major;$minor] = (7 xx $minor ) xx $major;
@array[$major div 2;$minor div 2] = 42;
say @array;</lang>
 
Typical run:
<lang>Dimensions? 3 x 10
[[7 7 7 7 7 7 7 7 7 7] [7 7 7 7 7 42 7 7 7 7] [7 7 7 7 7 7 7 7 7 7]]</lang>
 
=={{header|REXX}}==
Line 2,038:
arr[1][3] = 5
p arr[1][3]</lang>
 
 
=={{header|Rust}}==
Line 2,321 ⟶ 2,320:
Array2.update (array, 0, 0, 3.5);
print (Real.toString (Array2.sub (array, 0, 0)) ^ "\n");</lang>
 
=={{header|Stata}}==
<lang stata>display "Number of rows?" _request(nr)
Line 2,389:
unset arr
</lang>
 
 
 
=={{header|Toka}}==
Line 2,552 ⟶ 2,550:
123
</pre>
 
=={{header|zonnon}}==
<lang zonnon>