Create a two-dimensional array at runtime: Difference between revisions
Content added Content deleted
m (→{{header|AppleScript}}: Typo correction.) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 554: | Line 554: | ||
return 0; |
return 0; |
||
}</lang> |
}</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++}}== |
=={{header|C++}}== |
||
Line 655: | Line 672: | ||
return EXIT_FAILURE; |
return EXIT_FAILURE; |
||
}</lang> |
}</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}}== |
=={{header|Clean}}== |
||
Line 682: | Line 682: | ||
(_, dim2, console) = freadi console |
(_, dim2, console) = freadi console |
||
= createArray dim1 (createArray dim2 1.0)</lang> |
= createArray dim1 (createArray dim2 1.0)</lang> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 904: | Line 903: | ||
In position 3 4 we have 20 |
In position 3 4 we have 20 |
||
</pre> |
</pre> |
||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
In ERRE language arrays created at run-time is "dynamic arrays". For this task will be enough this code: |
In ERRE language arrays created at run-time is "dynamic arrays". For this task will be enough this code: |
||
Line 940: | Line 940: | ||
printf(1,"array[%d][%d] is %d\n", {i,j,array[i][j]})</lang> |
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}}== |
=={{header|Factor}}== |
||
Line 1,008: | Line 1,017: | ||
</lang> |
</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}}== |
=={{header|GAP}}== |
||
<lang gap># Creating an array of 0 |
<lang gap># Creating an array of 0 |
||
Line 1,284: | Line 1,285: | ||
would produce: <lang jq>[[0,0,0,0],[0,0,99,0],[0,0,0,0]]</lang> |
would produce: <lang jq>[[0,0,0,0],[0,0,99,0],[0,0,0,0]]</lang> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 1,387: | Line 1,387: | ||
array[[1,1]] |
array[[1,1]] |
||
]</lang> |
]</lang> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
Line 1,605: | Line 1,604: | ||
Arr.1.1 := 42 |
Arr.1.1 := 42 |
||
{Show Arr.1.1}</lang> |
{Show Arr.1.1}</lang> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
<lang parigp>tmp(m,n)={ |
<lang parigp>tmp(m,n)={ |
||
Line 1,691: | Line 1,690: | ||
> 0 1 0 |
> 0 1 0 |
||
> 0 0 0</lang> |
> 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}}== |
=={{header|Phix}}== |
||
Line 1,954: | Line 1,918: | ||
'#(#(1 0 0) #(0 0 0) #(0 0 9)) |
'#(#(1 0 0) #(0 0 0) #(0 0 9)) |
||
</pre> |
</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}}== |
=={{header|REXX}}== |
||
Line 2,038: | Line 2,038: | ||
arr[1][3] = 5 |
arr[1][3] = 5 |
||
p arr[1][3]</lang> |
p arr[1][3]</lang> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 2,321: | Line 2,320: | ||
Array2.update (array, 0, 0, 3.5); |
Array2.update (array, 0, 0, 3.5); |
||
print (Real.toString (Array2.sub (array, 0, 0)) ^ "\n");</lang> |
print (Real.toString (Array2.sub (array, 0, 0)) ^ "\n");</lang> |
||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
<lang stata>display "Number of rows?" _request(nr) |
<lang stata>display "Number of rows?" _request(nr) |
||
Line 2,389: | Line 2,389: | ||
unset arr |
unset arr |
||
</lang> |
</lang> |
||
=={{header|Toka}}== |
=={{header|Toka}}== |
||
Line 2,552: | Line 2,550: | ||
123 |
123 |
||
</pre> |
</pre> |
||
=={{header|zonnon}}== |
=={{header|zonnon}}== |
||
<lang zonnon> |
<lang zonnon> |