问题陈述
我想迭代矩阵中的每个元素并显示:
下面的代码给出了以下输出:
(-2147483648, -2147483648) = 1.00000E+00
(-2147483648, -2147483647) = 2.00000E+00
(-2147483648, -2147483646) = 3.00000E+00
(-2147483647, -2147483648) = 4.00000E+00
(-2147483647, -2147483647) = 5.00000E+00
(-2147483647, -2147483646) = 6.00000E+00
我希望看到1而不是-2147483648和2而不是-2147483647.
示例代码
with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;
with Ada.Text_IO; use Ada.Text_IO;
procedure Index is
Matrix : Real_Matrix := (( 1.0, 2.0, 3.0 ),
( 4.0, 5.0, 6.0 ));
begin
for I in Matrix'Range(1) loop
for J in Matrix'Range(2) loop
Put_Line("(" & Integer'Image(I) & ", " &
Integer'Image(J) & ") = " &
Float'Image(Matrix(I, J)));
end loop;
end loop;
end Index;
答案 0 :(得分:5)
Real_Matrix
的索引类型为Integer
,从您平台上的-2147483648
开始,它解释了您看到的数字。但是,由于类型不受约束,您可以在数组聚合中指定自己的索引:
Matrix : Real_Matrix := ( 1 => ( 1 => 1.0, 2 => 2.0, 3 => 3.0 ),
2 => ( 1 => 4.0, 2 => 5.0, 3 => 6.0 ));