How to resolve the algorithm Create a two-dimensional array at runtime step by step in the Erlang programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Create a two-dimensional array at runtime step by step in the Erlang programming language
Table of Contents
Problem Statement
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 output that element. Finally destroy the array if not done by the language itself.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Create a two-dimensional array at runtime step by step in the Erlang programming language
Source code in the erlang programming language
-module( two_dimensional_array ).
-export( [create/2, get/3, set/4, task/0] ).
create( X, Y ) -> array:new( [{size, X}, {default, array:new( [{size, Y}] )}] ).
get( X, Y, Array ) -> array:get( Y, array:get(X, Array) ).
set( X, Y, Value, Array ) ->
Y_array = array:get( X, Array ),
New_y_array = array:set( Y, Value, Y_array ),
array:set( X, New_y_array, Array ).
task() ->
{ok, [X, Y]} = io:fread( "Input two integers. Space delimited, please: ", "~d ~d" ),
Array = create( X, Y ),
New_array = set( X - 1, Y - 1, X * Y, Array ),
io:fwrite( "In position ~p ~p we have ~p~n", [X - 1, Y - 1, get( X - 1, Y - 1, New_array)] ).
You may also check:How to resolve the algorithm Zig-zag matrix step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Set step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Define a primitive data type step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Gaussian elimination step by step in the PARI/GP programming language