[Ada] How do I create and use multi-dimensional arrays in Ada?

To create and use multi-dimensional arrays in Ada, you can follow these steps:

  1. Declare the multi-dimensional array: Start by declaring the array with the desired dimensions. For example, to create a 2-dimensional array with dimensions A x B, you would declare it as follows:
1type Array_Type is array (Integer range <>, Integer range <>) of Integer;

Here, "Integer range <>" represents the range of indices for each dimension.

  1. Declare an instance of the multi-dimensional array: Declare a variable of the array type to create an instance of the array. For example:
1Array_Var : Array_Type(1..A, 1..B);

Here, "1..A" and "1..B" represent the ranges for the respective dimensions.

  1. Accessing and manipulating the array elements: Use the indexing notation to access and manipulate individual elements. For example, to access an element at position (i, j), use:
1Array_Var(i, j);

You can assign values to the elements or perform any other operations on them.

Here's a complete example that demonstrates the creation and usage of a 2-dimensional array in Ada:

 1with Ada.Text_IO;
 2use Ada.Text_IO;
 3
 4procedure Multi_Dimensional_Array is
 5   type Array_Type is array (Integer range <>, Integer range <>) of Integer;
 6
 7   -- Create an instance of the multi-dimensional array
 8   Array_Var : Array_Type(1..3, 1..4);
 9begin
10   -- Assign values to array elements
11   Array_Var(1, 1) := 10;
12   Array_Var(2, 2) := 20;
13   Array_Var(3, 4) := 30;
14
15   -- Access and print array elements
16   Put_Line(Integer'Image(Array_Var(1, 1))); -- Output: 10
17   Put_Line(Integer'Image(Array_Var(2, 2))); -- Output: 20
18   Put_Line(Integer'Image(Array_Var(3, 4))); -- Output: 30
19end Multi_Dimensional_Array;

In this example, a 2-dimensional array Array_Var is declared and initialized with values at specific indices. The values are then accessed and printed using the Put_Line procedure.

You can extend this example to include more dimensions and perform various operations on the multi-dimensional array as per your requirements.