user14237668
user14237668

Reputation:

How to use for loop in a multiple dimensional array?

**(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;

Upvotes: 3

Views: 725

Answers (1)

Jeffrey R. Carter
Jeffrey R. Carter

Reputation: 3358

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;

Upvotes: 4

Related Questions