Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use for loop in a multiple dimensional array?

Tags:

ada

**(Beginner question)! I have used multiple dimensional array in my data type (Float_Type) and I'm wondering how can I loop through them. I tried using "Put(A(I, I))" but it is not correct. My code: **

with Ada.Float_Text_IO;              use Ada.Float_Text_IO;
with Ada.Text_IO;                    use Ada.Text_IO;
with Ada.Integer_Text_IO;            use Ada.Integer_Text_IO;

procedure Test is
type Float_Type is
 array (0 .. 4,1 ..3) of Float;   --Total float numbers: 15

A: Float_Type;
 begin
 Put(" Write in your float nums: ");
 for I in Float_Type'Range loop
  Get(A(I, I));     -- Not correct
end loop; Skip_Line;

Put(" Your float nums: ");
for I in Float_Type'Range loop
  Put(A(I, I));          --Not correct
end loop;
end Test;

1 Answers

All arrays have a dimension number, D. A one-dimensional array, such as String, has D = 1; A two-dimensional array, such as Float_Type, has D = 2; and so on. The dimensions of an array are numbered 1 .. D, with 1 referring to the 1st range given (0 .. 4 for your case), 2 to the 2nd (1 ..3 for your case), and so on. The range of the Nth dimension of an array can be obtained with 'range (N) (for N in 1 .. D). For one-dimensional arrays, the form 'range may be used as shorthand for 'range (1).

So, as Drummond has said, you need two nested loops:

for I in A'range (1) loop
   for J in A'range (2) loop
      Get (Item => A (I, J) );
   end loop;
end loop;
like image 97
Jeffrey R. Carter Avatar answered Oct 17 '25 21:10

Jeffrey R. Carter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!