In Ada programming, What is a best practice way to create a 2D array of a size that is specified by the user?I'm teaching myself Ada for work (after many years of C programming), and I'm having difficulty understanding how to create an array of a size that is only known at runtime...My simple program needs to do the following.Ask the user to type in two numbers, for the width (X) and height(Y) of a 2 dimensional character array.Use these values as the upper bounds of the array (0..X-1, 0..Y-1).Initialise the array with zeroes.After quite a bit of googling I think I'm almost there, if I hard code the array to a fixed size then the program works fine, but I'd like to get the array sized as per the user's desire.My full program is as follows, it doesn't compile, but I hope it is enough to demonstrate what I'm trying to do.with Ada.Text_IO; use Ada.Text_IO;with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;with Ada.Characters; use Ada.Characters;procedure test is  type Two_Dimensional_Char_Array is array (Integer range <>, Integer range <>) of character;  --grid : Two_Dimensional_Char_Array (0..59, 0..29) := (others => (others => ' '));  grid : Two_Dimensional_Char_Array;  procedure Draw_Grid(scr : Two_Dimensional_Char_Array) is    X, Y : Integer := 0;  begin    Put(Width? );    Get(X);    Put(Height? );    Get(Y);    declare      grid : Two_Dimensional_Char_Array(0..X-1, 0..Y-1);    begin      grid := (others => (others => 0));    end;    Put(+);    for X in scr'First(1)..scr'Last(1) loop      Put(-);    end loop;    Put_Line(+);    for Y in scr'First(2)..scr'Last(2) loop      Put(|);      for X in scr'First(1)..scr'Last(1) loop        Put(scr(X, Y));      end loop;      Put_Line(|);    end loop;    Put(+);    for X in scr'First(1)..scr'Last(1) loop      Put(-);    end loop;    Put_Line(+);  end Draw_Grid;begin  grid(0,0) := 'a';  grid(0,1) := 'b';  grid(1,1) := 'c';  grid(20,10) := 'd';  grid(59,29) := 'X';  Draw_Grid(grid);end test;
How to create a 2D array at runtime in Ada?
syntax;array;ada
The best practice is to use constructor function.While the type can be unconstrained, the actual object has to posses some compile time bounds on size (unless you allocate it on heap).You should create additional function for creating this array. Here is how I modified your code and works just fine (do note that I changed indices to be 1 based, as I hate 0 based array indices).Additionally in your sample code you tried to initialize your Grid with Integers... (despite the fact that Grid is 2D array of Character)with Ada.Text_IO; use Ada.Text_IO;with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;procedure test is  type Two_Dimensional_Char_Array is    array (Integer range <>, Integer range <>) of Character;  -- constructor function.  function Create_Grid return Two_Dimensional_Char_Array  is    X, Y: Natural := 0;  begin    Put (Width? );    Get (X);    Put (Height? );    Get (Y);    declare      Grid: constant Two_Dimensional_Char_Array (1 .. X, 1 .. Y) :=        (others => (others => ' '));    begin      return Grid;    end;  end Create_Grid;  procedure Draw_Grid(scr : Two_Dimensional_Char_Array) is  begin    Put (+);    for X in scr'Range (1) loop      Put (-);    end loop;    Put_Line (+);    for Y in scr'Range (2) loop      Put (|);      for X in scr'Range (1) loop        Put (scr (X, Y));      end loop;      Put_Line (|);    end loop;    Put (+);    for X in scr'Range (1) loop      Put (-);    end loop;    Put_Line (+);  end Draw_Grid;  -- Create grid at runtime using function.  Grid: Two_Dimensional_Char_Array := Create_Grid;begin  Grid (1,1)    := 'a';  Grid (1,2)    := 'b';  Grid (2,2)    := 'c';  Grid (21,11)  := 'd';  Grid (60,30)  := 'X';  Draw_Grid (Grid);end test;In addition there is nice attribute in Ada: 'Range - used in for loops when you are iterating over entire range (which is just a shortcut for 'First .. 'Last)PS: Why are you, C programmers, so averse of using spaces in code? It makes it hard to read [as in: scr'First(1)..scr'Last(1)].