在Ada中将数组传递给函数
我试图将名为Arr的整数数组传递给collect函数。在collect内部,我正在从用户那里收集int。一旦用户输入100整数或给出0或负数,数组将停止收集。我做错了什么在Ada中将数组传递给函数,ada,Ada,我试图将名为Arr的整数数组传递给collect函数。在collect内部,我正在从用户那里收集int。一旦用户输入100整数或给出0或负数,数组将停止收集。我做错了什么 with Ada.Text_IO, Ada.Integer_Text_IO; use Ada.Text_IO; procedure helloworld is procedure PrintHelloWorld is begin Put_Line ("Enter Integers, up to 10
with Ada.Text_IO, Ada.Integer_Text_IO; use Ada.Text_IO;
procedure helloworld is
procedure PrintHelloWorld is
begin
Put_Line ("Enter Integers, up to 100 individual ints.");
end PrintHelloWorld;
function collect (A : array) return array is
A: array (1 .. 100) of integer;
begin
Ada.Text_IO.Put ("Enter an integer: ");
Ada.Integer_Text_IO.Get(I);
Ada.Text_IO.Put_Line (Integer'Image(I));
if I > 0 then
A(i) := I;
end if;
while I > 0 loop
Ada.Text_IO.Put ("Enter an integer: ");
Ada.Integer_Text_IO.Get(I);
Ada.Text_IO.Put_Line (Integer'Image(I));
if I > 0 then
A(i) := I;
end if;
end loop;
return A;
end collect;
procedure printArr is
begin
for j in Arr'range loop
Ada.Text_IO.Put_Line(Integer'Image(Arr(j)));
end loop;
end printArr;
Arr: array (1 .. 100) of integer;
Arr := (others => 0);
I: Integer;
begin
Put_Line ("Hello World!");
PrintHelloWorld;
Arr := collect(Arr);
printArr;
end helloworld;
终端错误:
gnatmake helloworld.adb
x86_64-linux-gnu-gcc-8 -c helloworld.adb
helloworld.adb:13:26: anonymous array definition not allowed here
helloworld.adb:13:46: reserved word "is" cannot be used as identifier
helloworld.adb:15:08: missing ")"
gnatmake: "helloworld.adb" compilation error
您的思维似乎停留在C/C++/Java语法和Ada之间。试试下面的例子
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Main is
type Data_Array is array(Positive range <>) of Integer;
function Collect return Data_Array is
Count : Natural := 0;
begin
Put_Line("Enter the number of data values you will input");
Get(Count);
declare
Nums : Data_Array(1..Count);
begin
Put_Line("Enter" & Count'Image &" values");
for Value of Nums loop
Get(Value);
end loop;
return Nums;
end;
end Collect;
procedure Print(Item : in Data_Array) is
begin
Put_Line("Output of array values:");
for Value of Item loop
Put_Line(Value'Image);
end loop;
end Print;
begin
Print(Collect);
end Main;
过程Print采用Data_Array类型的参数,该参数由函数Collect的返回值提供。
函数collect只返回所需大小的数组。“打印”过程将获取一个大小不一的数组,并打印该数组的内容。您的思维似乎停留在C/C++/Java语法和Ada之间。试试下面的例子
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Main is
type Data_Array is array(Positive range <>) of Integer;
function Collect return Data_Array is
Count : Natural := 0;
begin
Put_Line("Enter the number of data values you will input");
Get(Count);
declare
Nums : Data_Array(1..Count);
begin
Put_Line("Enter" & Count'Image &" values");
for Value of Nums loop
Get(Value);
end loop;
return Nums;
end;
end Collect;
procedure Print(Item : in Data_Array) is
begin
Put_Line("Output of array values:");
for Value of Item loop
Put_Line(Value'Image);
end loop;
end Print;
begin
Print(Collect);
end Main;
过程Print采用Data_Array类型的参数,该参数由函数Collect的返回值提供。
函数collect只返回所需大小的数组。“打印”过程将获取一个任意大小的数组,并打印该数组的内容。
给你我也做了,所以你可以加负数。
如果这不是您想要的,请根据需要将向量的元素类型更改为“自然”或“正”。
请注意-'注释是错误语法highlighter的一个解决方法
布拉格马阿达_2012;
Pragma断言(u Policy Ignore);
Pragma限制无实现方面规范;
Pragma限制不执行Pragma;
具有
Ada.Strings.Equal_不区分大小写,
Ada.Text_IO.Text_流;
程序示例为
-计数和索引类型。
类型计数的范围为0到100;
子类型索引是计数范围Count'succount'First..Count'Last;-'
-数组类型;其范围受索引限制。
类型向量是整数的数组索引范围;
-这里我们构建输入向量。
函数Get_输入返回向量为
-这里我们得到用户输入;我们将工作集作为参数传递,所以
-它可以就地建造;这也确保了我们只有
-生成适当长度的结果。
函数Get_Working:Vector:=2..1=>返回向量为
函数=左,右:字符串返回布尔值
重命名Ada.Strings.Equal_不区分大小写;
开始
-我们从不尝试添加到完整数组中。
如果工作“长度=索引”最后一个
返回工作;
如果结束;
-我们得到一个整数,否则就完成了。
环
Ada.Text_IO.Put输入一个整数或键入“Done”:;
读/写输入:
声明
用户输入:常量字符串:=Ada.Text\u IO.Get\u行;
开始
返回Get_Working&Integer'ValueUser_Input;-'
例外
当约束_错误=>
如果用户输入=完成,则
返回工作;
如果结束;
结束读取输入;
端环;
结束工作;
开始
返回工作;
结束Get_输入;
程序打印对象:向量为
开始
Ada.Text_IO.Put'[';
对于对象“范围循环-”中的索引
Ada.Text\u IO.Put Integer'ImageObjectIndex;-'
如果Index/=Object'Last then-'
Ada.Text_IO.Put',';
如果结束;
端环;
Ada.Text_IO.Put'];
结束打印;
开始
声明
V:常数向量:=获取_输入;
开始
印刷V;
Ada.Text_IO.New_第2行;
终止
Ada.Text\u IO.Put\u Line完成;
结束示例;
运行示例:
输入整数或类型“完成”:21
输入整数或类型“完成”:4
输入整数或类型“完成”:18
输入整数或类型“完成”:完成
[ 21, 4, 18]
完成了
给你我也做了,所以你可以加负数。
如果这不是您想要的,请根据需要将向量的元素类型更改为“自然”或“正”。
请注意-'注释是错误语法highlighter的一个解决方法
布拉格马阿达_2012;
Pragma断言(u Policy Ignore);
Pragma限制无实现方面规范;
Pragma限制不执行Pragma;
具有
Ada.Strings.Equal_不区分大小写,
Ada.Text_IO.Text_流;
程序示例为
-计数和索引类型。
类型计数的范围为0到100;
子类型索引是计数范围Count'succount'First..Count'Last;-'
-数组类型;其范围受索引限制。
类型向量是整数的数组索引范围;
-这里我们构建输入向量。
函数Get_输入返回向量为
-这里我们得到用户输入;我们将工作集作为参数传递,所以
-它可以就地建造;这也确保了我们只有
-生成适当长度的结果。
函数Get_Working:Vector:=2..1=>返回向量为
函数=左,右:字符串返回布尔值
重命名为Ada.S
trings.Equal不区分大小写;
开始
-我们从不尝试添加到完整数组中。
如果工作“长度=索引”最后一个
返回工作;
如果结束;
-我们得到一个整数,否则就完成了。
环
Ada.Text_IO.Put输入一个整数或键入“Done”:;
读/写输入:
声明
用户输入:常量字符串:=Ada.Text\u IO.Get\u行;
开始
返回Get_Working&Integer'ValueUser_Input;-'
例外
当约束_错误=>
如果用户输入=完成,则
返回工作;
如果结束;
结束读取输入;
端环;
结束工作;
开始
返回工作;
结束Get_输入;
程序打印对象:向量为
开始
Ada.Text_IO.Put'[';
对于对象“范围循环-”中的索引
Ada.Text\u IO.Put Integer'ImageObjectIndex;-'
如果Index/=Object'Last then-'
Ada.Text_IO.Put',';
如果结束;
端环;
Ada.Text_IO.Put'];
结束打印;
开始
声明
V:常数向量:=获取_输入;
开始
印刷V;
Ada.Text_IO.New_第2行;
终止
Ada.Text\u IO.Put\u Line完成;
结束示例;
运行示例:
输入整数或类型“完成”:21
输入整数或类型“完成”:4
输入整数或类型“完成”:18
输入整数或类型“完成”:完成
[ 21, 4, 18]
完成了 您可能从编译器收到消息,表示函数不返回任何内容。嗯,没有。此外,还有一个全局变量、一个函数的形式参数和一个局部变量,它们都具有相同的名称。猜猜哪一个是可见的。。。另外,请参见:可能是您的一位同学在做相同的作业?函数collect A:array返回数组甚至不会编译。您需要声明数组类型。@BrianDrummond感谢您的回复。我更新了我的代码以及终端的错误。@SimonWright这样的东西是数组类型吗?类型向量是整数的数组整数范围;1如果要传入一个数组进行收集:使其成为一个带有IN-Out模式参数的过程,不需要两个数组:更简单。2另外:在声明中初始化数组,例如by:=others=>0,这样它就不包含垃圾。3在阵列上循环时,不要使用幻数,因为您已经很好地说明了原因!:改为在Arr'range中使用i。您可能从编译器收到消息,该函数不返回任何内容。嗯,没有。此外,还有一个全局变量、一个函数的形式参数和一个局部变量,它们都具有相同的名称。猜猜哪一个是可见的。。。另外,请参见:可能是您的一位同学在做相同的作业?函数collect A:array返回数组甚至不会编译。您需要声明数组类型。@BrianDrummond感谢您的回复。我更新了我的代码以及终端的错误。@SimonWright这样的东西是数组类型吗?类型向量是整数的数组整数范围;1如果要传入一个数组进行收集:使其成为一个带有IN-Out模式参数的过程,不需要两个数组:更简单。2另外:在声明中初始化数组,例如by:=others=>0,这样它就不包含垃圾。3在阵列上循环时,不要使用幻数,因为您已经很好地说明了原因!:改为在Arr'范围内用于i。原始代码中函数collect内部的数组定义可能来自Fortran 77语法。原始代码中函数collect内部的数组定义可能来自Fortran 77语法。