How to resolve the algorithm Create a two-dimensional array at runtime step by step in the Crystal 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 Crystal 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 Crystal programming language

Source code in the crystal programming language

require "random"

first = gets.not_nil!.to_i32
second = gets.not_nil!.to_i32

arr = Array(Array(Int32)).new(first, Array(Int32).new second, 0)

random = Random.new

first = random.rand 0..(first - 1)
second = random.rand 0..(second - 1)

arr[first][second] = random.next_int
puts arr[first][second]


  

You may also check:How to resolve the algorithm Draw a rotating cube step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Top rank per group step by step in the Nim programming language
You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the VBA programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the HicEst programming language
You may also check:How to resolve the algorithm Arrays step by step in the BML programming language