I am trying to pass my array of integers called "Arr" to a function "collect". inside of collect, I am collecting an int from the user. once the user puts in 100 ints or gives a value of 0 or a negative number the array will stop collecting. What am I doing wrong?
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; Terminal errors:
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
function collect (A : array) return arraywon’t even compile. You need to declare an array type.collect: make it a procedure with anIn Outmode parameter, (No need for two arrays : simpler.) 2) Also : initialise the array in its declaration, e.g. by:= (others => 0)so it doesn't contain garbage. 3) Don't use magic numbers when looping over the array (you have beautifully illustrated why!) : usefor i in Arr'rangeinstead.