打印整数数组在Ada中生成奇怪的输出 [英] Printing array of integers generates weird output in Ada
问题描述
我创建了一个简单的Ada程序,该程序允许用户用最多100个非负整数和非零整数填充数组,然后将其打印出来。当我调用该函数以打印数字时,它会打印出它们,但同时也会打印出一堆奇怪的,看似随机的数字。我在代码中犯了什么错误,导致程序输出了如此奇怪的结果?这是我第一次在Ada写作。例如,当我用数字[1,2,3,4,5]填充空数组时,将输出以下内容:
I created a simple Ada program that allows a user to populate an array with a maximum of 100 non negative and non zero integers and then prints them out. When I call the function to print out the numbers it prints them out but at the same time it also prints out a bunch of strange and seemingly random numbers. What mistake have I made in my code that is causing the program to output such strange results? This is my first time writing in Ada. For example when I populate the empty array with the numbers [1,2,3,4,5] is prints out this:
1
2
3
4
5
32624
911328835
32624
911328836
32624
67043328
134217726
134217726
2013265921
134217726
134217726
134217726
67043328
909181968
32624
2114692683
89452
914381552
32624
1543503876
2
14
2
14
我在ubuntu上使用gnatmake编译器,并且在编译源代码时不会给我任何错误/警告消息。
I am using the gnatmake compiler on ubuntu and when compiling the source code it doesn't give me any error/warning messages.
这是我的源代码,我知道我可能不需要使用单独的函数,但是我还是出于学习目的而实现了它们。
Here is my source code, I know that I probably don't need to use seperate functions but I implemented them anyways for learning purposes.
with Ada.Containers.Vectors;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
use Ada.Containers;
procedure thing is
type Vector is array (1..100) of Integer;
A: Vector;--array doesn't need to be completely filled up
K: Integer;
--array filling function below
function mkarr return Vector is --asks user to fill empty array with positive and non zero integers
begin
Ada.Text_IO.Put ("enter numbers to fill array, negative or 0 will stop process: ");
for I in 1..100 loop
Ada.Integer_Text_IO.Get(K);
if K>0 then
A(I) := K;
end if;
if K<=0 then
return A;
end if;
end loop;
return A;
end;
--array printing prodcedure below
procedure printarr(A: in out Vector) is
begin
for I in A'Range loop
if A(I)>0 then
Ada.Integer_Text_IO.Put(A(I));
New_Line(1);
end if;
end loop;
end printarr;
B: Vector := mkarr;
--main method below
begin
printarr(A);
end thing;
推荐答案
在 mkarr
,您可以使用0或负值来标记输入的结尾,但不要将该值存储在数组中。如果在您输入的值的结尾之后数组中存储了任何垃圾,而这些垃圾恰好是正数,则无法分辨出它不是有效值。
In mkarr
, you use a 0 or negative value to mark the end of the input, but you don't store that value in the array. If whatever garbage is stored in the array after the end of the values you entered happens to be positive, there's no way to tell that it's not a valid value.
In printarr
,如果遇到0或负值,则不会打印该值,而是继续打印其余的正值(这是垃圾)。
In printarr
, if you encounter a 0 or negative value you don't print it -- but you continue printing the remaining positive values (which are garbage).
如果将哨兵值存储在 mkarr
中,并且在 printarr
时退出打印
If you store a sentinel value in mkarr
, and quit printing in printarr
when you encounter the sentinel, the program should work.
其他注意事项:
A
和 K
仅在 mkarr
内部使用。它们应该是 mkarr
的本地变量。
A
and K
are used only inside mkarr
. They should be local to mkarr
.
您从不使用 Ada.Containers.Vectors
。您可以使用和 use
指令删除相应的。
You never use Ada.Containers.Vectors
. You can delete the corresponding with
and use
directives.
对于更高级的用法,可以使 Vector
不受约束,使 mkarr
返回仅包含输入数据的数组。数组类型并返回切片。在 mkarr
中定义固定长度的数组仍然更加容易。允许任意输入很多都是很麻烦的-但是 Ada.Containers
可能是一个很好的方法。 (免责声明:我没看过 Ada.Containers
。)
For more advanced usage, you can have mkarr
return an array containing only the entered data, by making Vector
an unconstrained array type and returning a slice. It's still easier to define a fixed-length array inside mkarr
. Allowing arbitrarily many inputs is tricker -- but Ada.Containers
is probably a good way to do that. (Disclaimer: I haven't looked at Ada.Containers
.)
这篇关于打印整数数组在Ada中生成奇怪的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!