Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

dynamic array size determined at runtime in ada

Is it possible to have an array with size that is determined at runtime like so,

Procedure prog is
   type myArray is array(Integer range <>) of Float;
   arraySize : Integer := 0;
   theArray : myArray(0..arraySize);
Begin
   -- Get Array size from user.
   put_line("How big would you like the array?");
   get(arraySize);

   For I in 0..arraySize Loop
      theArray(I) := 1.2 * I;
   End Loop;
End prog;

Is there a way to achieve this result other than using dynamically Linked Lists or another similar structure? Or is there a simple built in data structure that would be simpler than using dynamically linked lists?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sure, declare it in a block as follows:

procedure prog is
   arraySize : Integer := 0;
   type myArray is array(Integer range <>) of Float;
begin
   -- Get Array size from user.
   put_line("How big would you like the array?");
   get(arraySize);

   declare
      theArray : myArray(0..arraySize);
   begin
      for I in 0..arraySize Loop
         theArray(I) := 1.2 * I;
      end Loop;
   end;
end prog;

or pass the arraySize as an argument into a subprogram and declare and operate on it in that subprogram:

procedure Process_Array (arraySize : Integer) is

    theArray : myArray(0..arraySize);

begin
   for I in arraySize'Range Loop
      theArray(I) := 1.2 * I;
   end Loop;
end;

This is just illustrative (and not compiled :-), as you need to deal with things like an invalid array size and such.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...