4

I am trying to write an Ada equivalent of the following statement in Python: L = [[] for i in range(n)]

I am solving a dynamic programming problem and my plan is to eventually copy the contents of the jth array inside L into the ith array (j < i) if the ith array has fewer elements than the jth array.

I have found how to create an empty array by defining its range in the reverse order. So, for instance, arr2 would be an empty array created as follows:

arr2: array(2 .. 1) of Integer;

My question is, how do I define the bigger array L to include n of such arr2 arrays?

Please let me know.

Update: I was able to get it correctly working using the answers below. Here is my code.

package Integer_Vectors is new Ada.Containers.Vectors
    (Index_Type   => Natural,
    Element_Type => Integer);

    N: Integer;

    Array_Of_Vectors : array(1 .. N) of Integer_Vectors.Vector := (others => Integer_Vectors.Empty_Vector);

    Input_Sequence: Integer_Vectors.Vector;

    Max: Integer_Vectors.Vector;

    Input_List : String := Get_Line;

    IntCast : Integer;

    Last : Positive := 1;
    
    begin

    while Last < Input_List'Last loop
        Get(Input_List(Last..Input_List'Last),IntCast,Last);
        Input_Sequence.Append(IntCast);
        Last := Last + 1;
    end loop;
    
    N := 0;

    for i of Input_Sequence loop
        N := N + 1;
    end loop;
  • 5
    In Ada you can define custom types: `type Arr2_Type is array(2 .. 1) of Integer`. Then you can have arrays of your custom type (and so on...). But probably you don't want the "Arr2" arrays to stay empty. Then you'll prefer to use vectors (they are extensible) instead of arrays (they have a fixed size) in that case. – Zerte Sep 10 '20 at 06:10

1 Answers1

7

In Ada L will be

L : array (1 .. N, 1 .. 0) of Integer;

But it's useless, because you won't be able to extend it latter. @Zerte is right.

You can use a vector on indefinite elements, for instance. Like this

with Ada.Containers.Indefinite_Vectors;

type Integer_Array is array (Positive range <>) of Integer;

Empty : constant Integer_Array := (1 .. 0 => <>);

package Integer_Array_Vectors is new
 Ada.Containers.Indefinite_Vectors
   (Index_Type => Positive, Element_Type => Integer_Array);

L : Integer_Array_Vectors.Vector;

L.Append (Empty, N);

if L (J)'Length > L (I)'Length then
   L.Replace_Element (I, L(J));
end if;
Maxim Reznik
  • 1,216
  • 9
  • 12
  • 2
    Using Indefinite vectors here seems kind of overkill to me. n, the size of L, is known and fixed: L is an array. Arrays in L have varying length: they are vectors. Vectors can be used inside an array. Another solution could be `L : array(1 .. n) of Integer_Vectors.Vector := (others => Integer_Vectors.Empty_Vector);` – G_Zeus Sep 10 '20 at 14:57
  • @Maxim, thanks for your suggestion. Unfortunately, L.Append(Empty, N) gave me a declaration expected error. – Yellowjacket11 Sep 11 '20 at 20:14
  • @Yellowjacket11 declaration of what? From what I can see N is not declared... – darkestkhan Sep 13 '20 at 09:13