Anonymous user
Create a two-dimensional array at runtime: Difference between revisions
Create a two-dimensional array at runtime (view source)
Revision as of 17:12, 23 July 2015
, 8 years ago→{{header|jq}}
(Added an Algol W sample) |
|||
Line 1,006:
would produce: <lang jq>[[0,0,0,0],[0,0,99,0],[0,0,0,0]]</lang>
=={{header|Julia}}==
Julia supports n-dimensional arrays as native datatypes: Array{T, N}, where T is the type of it´s elements and N is the number of dimensions.
<lang julia>
julia> "Inspired by Python's `input function."
function input(prompt::AbstractString="")
print(prompt)
chomp(readline())
end
input (generic function with 2 methods)
julia> n = parse(Int, input("Upper bound for dimension 1: ")) # parse as `Int`
Upper bound for dimension 1: 5
5
julia> m = parse(Int, input("Upper bound for dimension 2: "))
Upper bound for dimension 2: 5
5
julia> x = rand(n, m) # create an n·m random matrix
5x5 Array{Float64,2}:
0.80217 0.422318 0.594049 0.45547 0.208822
0.0533981 0.304858 0.0276755 0.797732 0.828796
0.522506 0.563856 0.216759 0.865961 0.034306
0.792363 0.815744 0.868697 0.42509 0.588946
0.112034 0.539611 0.674581 0.508299 0.939373
julia> x[3, 3] # overloads `getindex` generic function
0.21675944652281487
julia> x[3, 3] = 5 # overloads `setindex!` generic function
5
julia> x::Matirx # `Matrix{T}` is an alias for `Array{T, 2}`
5x5 Array{Float64,2}:
0.80217 0.422318 0.594049 0.45547 0.208822
0.0533981 0.304858 0.0276755 0.797732 0.828796
0.522506 0.563856 5.0 0.865961 0.034306
0.792363 0.815744 0.868697 0.42509 0.588946
0.112034 0.539611 0.674581 0.508299 0.939373
julia> x = 0; gc() # Julia has no `del` command, rebind `x` and call the garbage collector
</lang>
=={{header|Liberty BASIC}}==
|