comp.lang.ada
 help / color / mirror / Atom feed
* Passing a 2d array into a package and returning the same processed 2d back to main
@ 2016-09-30 22:27 diane74
  2016-10-01  0:01 ` Jeffrey R. Carter
                   ` (2 more replies)
  0 siblings, 3 replies; 24+ messages in thread
From: diane74 @ 2016-09-30 22:27 UTC (permalink / raw)


Hello again, new Ada programmer with a hopefully easy question. 
I am trying to take a 2d array pass it to a package have the package change the data in the 2d array and send the same 2d array (with modified data) back to the main program. I am currently away from my computer or I would show you my broken code, though I doubt it would be useful. 

Thanks


^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-09-30 22:27 Passing a 2d array into a package and returning the same processed 2d back to main diane74
@ 2016-10-01  0:01 ` Jeffrey R. Carter
  2016-10-01 20:01 ` Aurele
  2016-10-03 18:36 ` James Brewer
  2 siblings, 0 replies; 24+ messages in thread
From: Jeffrey R. Carter @ 2016-10-01  0:01 UTC (permalink / raw)


On 09/30/2016 03:27 PM, diane74@gmail.com wrote:
> Hello again, new Ada programmer with a hopefully easy question.
> I am trying to take a 2d array pass it to a package have the package change the data in the 2d array and send the same 2d array (with modified data) back to the main program. I am currently away from my computer or I would show you my broken code, though I doubt it would be useful.

It sounds as if you might be interested in "Formal Parameter Modes", defined in 
ARM 6.2:

http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-6-2.html

The mode defines the direction of data flow between the subprogram and the 
caller: "in" means data flows from the caller into the subprogram; "out" means 
data flows from the subprogram back to the caller; what "in out" means is left 
as an exercise for the reader.

-- 
Jeff Carter
"Run away! Run away!"
Monty Python and the Holy Grail
58

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-09-30 22:27 Passing a 2d array into a package and returning the same processed 2d back to main diane74
  2016-10-01  0:01 ` Jeffrey R. Carter
@ 2016-10-01 20:01 ` Aurele
  2016-10-01 21:04   ` Jeffrey R. Carter
  2016-10-03 18:36 ` James Brewer
  2 siblings, 1 reply; 24+ messages in thread
From: Aurele @ 2016-10-01 20:01 UTC (permalink / raw)


Off the top of my head, maybe you can do something like this(simple example)...

package 2D_Array_Handler is   -- Your package....
  type 2D_Array is record 
    x : integer;
    y : integer;
  end record;
  type 2D_Array_Ptr is access all 2D_Array;
  function Modify_2D_Array( Data_Ptr : 2D_Array_Ptr ) return Boolean;
end 2D_Array_Handler;

package body 2D_Array_Handler is  -- Your package
  function Add( Data_Ptr : 2D_Array_Ptr ) return Boolean is
  begin
    if Data_Ptr /= null then 
      Data.X := Data_Ptr.X + Some_X_Value...
      Data.Y := Data_Ptr.Y + Some_Y_Value...
      return TRUE;
    else
      return FALSE;
    end if;
  end Add;
end 2D_Array_Handler;

use 2D_Array_Handler;
declare   -- The calling procedure...
  My_Data_Ptr : 2D_Array_Ptr := new 2D_Array'( 0, 0 );
  bResult     : Boolean;
begin
  bResult := Add( My_Data_Ptr );
end;


^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-01 20:01 ` Aurele
@ 2016-10-01 21:04   ` Jeffrey R. Carter
  2016-10-01 21:59     ` Aurele
  0 siblings, 1 reply; 24+ messages in thread
From: Jeffrey R. Carter @ 2016-10-01 21:04 UTC (permalink / raw)


On 10/01/2016 01:01 PM, Aurele wrote:
> Off the top of my head, maybe you can do something like this(simple example)...
>
> package 2D_Array_Handler is   -- Your package....
>   type 2D_Array is record
>     x : integer;
>     y : integer;
>   end record;
>   type 2D_Array_Ptr is access all 2D_Array;

There is no need for access types for this problem.

-- 
Jeff Carter
"My legs are gray, my ears are gnarled, my eyes are old and bent."
Monty Python's Life of Brian
81

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-01 21:04   ` Jeffrey R. Carter
@ 2016-10-01 21:59     ` Aurele
  2016-10-01 22:16       ` Jeffrey R. Carter
  0 siblings, 1 reply; 24+ messages in thread
From: Aurele @ 2016-10-01 21:59 UTC (permalink / raw)


> There is no need for access types for this problem.

That maybe true, but it nonetheless works and its fast. 

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-01 21:59     ` Aurele
@ 2016-10-01 22:16       ` Jeffrey R. Carter
  2016-10-02  0:09         ` Aurele
  0 siblings, 1 reply; 24+ messages in thread
From: Jeffrey R. Carter @ 2016-10-01 22:16 UTC (permalink / raw)


On 10/01/2016 02:59 PM, Aurele wrote:
>> There is no need for access types for this problem.
>
> That maybe true, but it nonetheless works and its fast.

You could write it in assembler and it would work and be fast. There are reasons 
not to use assembler, and there are reasons not to use access types. There are 
probably no applications that you, the OP, or I are likely to work on where 
access types and their associated errors are needed or the best choice. 
Certainly there should never be visible access types in a pkg spec.

-- 
Jeff Carter
"My legs are gray, my ears are gnarled, my eyes are old and bent."
Monty Python's Life of Brian
81

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-01 22:16       ` Jeffrey R. Carter
@ 2016-10-02  0:09         ` Aurele
  2016-10-02  7:31           ` Simon Wright
  0 siblings, 1 reply; 24+ messages in thread
From: Aurele @ 2016-10-02  0:09 UTC (permalink / raw)


Keep your shorts on Jeffrey and don't be defensive, I was just offering an alternate off the cuff Ada method. Now chill out !


^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-02  0:09         ` Aurele
@ 2016-10-02  7:31           ` Simon Wright
  0 siblings, 0 replies; 24+ messages in thread
From: Simon Wright @ 2016-10-02  7:31 UTC (permalink / raw)


Aurele <aurele.vitali@gmail.com> writes:

> Keep your shorts on Jeffrey and don't be defensive, I was just
> offering an alternate off the cuff Ada method. Now chill out !

But it's a very *bad* Ada method. It's the sort of method you'd have to
use in C, but I have to say that if someone offered that as a solution
in an Ada work environment it would be rejected outright.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-09-30 22:27 Passing a 2d array into a package and returning the same processed 2d back to main diane74
  2016-10-01  0:01 ` Jeffrey R. Carter
  2016-10-01 20:01 ` Aurele
@ 2016-10-03 18:36 ` James Brewer
  2016-10-03 19:43   ` Björn Lundin
  2016-10-03 20:16   ` Jeffrey R. Carter
  2 siblings, 2 replies; 24+ messages in thread
From: James Brewer @ 2016-10-03 18:36 UTC (permalink / raw)


On Friday, September 30, 2016 at 5:28:01 PM UTC-5, dia...@gmail.com wrote:
> Hello again, new Ada programmer with a hopefully easy question. 
> I am trying to take a 2d array pass it to a package have the package change the data in the 2d array and send the same 2d array (with modified data) back to the main program. I am currently away from my computer or I would show you my broken code, though I doubt it would be useful. 
> 
> Thanks

I may not have given enough information for help with this problem. So here is my attempt to clarify what I am trying to do by posting my nonfunctioning code.

                 -- Interface of WarshallsPkg

PACKAGE WarshallsPkg IS
   
   TYPE MY_ARRAY IS ARRAY(INTEGER RANGE <>, INTEGER RANGE <>) OF Boolean;  
   PROCEDURE WarshallExecute(InArray : IN MY_ARRAY; OutArray : OUT MY_ARRAY);
   
end WarshallsPkg;

                 -- Implementation of WarshallsPkg

PACKAGE BODY WarshallsPkg IS
   
   PROCEDURE WarshallExecute(InArray : IN MY_ARRAY; OutArray : OUT MY_ARRAY) IS

--   TestArray: ARRAY(InArray'FIRST..InArray'LAST) OF Integer;

   BEGIN
   FOR I IN InArray'FIRST..InArray'LAST LOOP
      FOR J IN InArray'FIRST..InArray'LAST LOOP
         IF InArray(J,I) = True THEN --true nodes connected
            FOR K IN InArray'FIRST..InArray'LAST LOOP
               OutArray(J,K) := InArray(J,K) OR InArray(I,K);
            END LOOP;
         END IF;
      END LOOP;
   END LOOP;

   END WarshallExecute;
   
END WarshallsPkg;

                      --The program i'm trying to implement it in

WITH Text_IO; USE Text_IO; -- This gets the IO facility.
WITH Ada.Integer_Text_IO; USE Ada.Integer_Text_IO; -- This gets the integer IO facility.
WITH WarshallsPkg; USE WarshallsPkg; --package setup


-- * main procedure *
PROCEDURE WarshallsMain IS

   Output : File_Type; -- output file declaration
   N : Integer := 9; -- max size of array -- why does this overflow if not set?
   yOrN : character; -- user input yes or no
   OutputConvertion : Integer; -- used for conversion from true/false to 1/0
   Array2d: ARRAY (1..N, 1..N) OF Boolean;
      
--  PROCEDURE testExecute(InArray : IN MY_ARRAY; OutArray : OUT MY_ARRAY); -- renames warshall proceedure


-- * main procedure starts begins*
BEGIN
-- try to use io redirection to pull data from file

-- output to file creation section
   Create (File => Output,
      Mode => Out_File,
      Name => "output.txt");

-- input node connection data collection
   -- ask for size of array
   Put("How many nodes need to be processed? ");
   Get(N);
   New_Line(2);

   FOR X IN 1..N LOOP
      FOR Y IN 1..N LOOP
         Put("Is node");
         Put(X);
         Put(" connected to node ");
         Put(Y);
         Put(" y/n ");
         Get(YOrN);

         if yOrN = 'y' then
            Array2d(X,Y) := True;
         ELSE Array2d(X,Y) := False;
         END IF;
      END LOOP;
   END LOOP;


-- Warshall's alorithm

-- implement proceedure here for Array2d

   WarshallExecute(Array2d, Array2d);
 
-- to be removed   
--   FOR I IN 1..N LOOP
--      FOR J IN 1..N LOOP
--         IF Array2d(J,I) = True THEN --true nodes connected
--            FOR K IN 1..N LOOP
--               Array2d(J,K) := Array2d(J,K) OR Array2d(I,K);
--            END LOOP;
--         END IF;
--      END LOOP;
--   END LOOP;

-- end of code to be removed

-- *********** output to file ************

-- column label output
   New_Line(Output, 2);
   Put(Output, "           ");
   FOR I IN 1..N LOOP
      Put(Output, I);
   END LOOP;
   New_Line(Output, 1);

-- data matrix output
   FOR I IN 1..N LOOP
      Put(Output, I);
      FOR J IN 1..N LOOP
         IF Array2d(I,J) = True THEN
            OutputConvertion := 1;
         ELSE
            OutputConvertion := 0;
         END IF;
         Put(Output, OutputConvertion);
      END LOOP;
      New_Line(Output, 1);
   END LOOP;

END WarshallsMain;
-- * main procedure ends *

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-03 18:36 ` James Brewer
@ 2016-10-03 19:43   ` Björn Lundin
  2016-10-03 21:59     ` James Brewer
  2016-10-03 20:16   ` Jeffrey R. Carter
  1 sibling, 1 reply; 24+ messages in thread
From: Björn Lundin @ 2016-10-03 19:43 UTC (permalink / raw)


On 2016-10-03 20:36, James Brewer wrote:
> On Friday, September 30, 2016 at 5:28:01 PM UTC-5, dia...@gmail.com wrote:
>> Hello again, new Ada programmer with a hopefully easy question. 
>> I am trying to take a 2d array pass it to a package have the package change the data in the 2d array and send the same 2d array (with modified data) back to the main program. I am currently away from my computer or I would show you my broken code, though I doubt it would be useful. 
>>
>> Thanks
> 
> 
> PACKAGE BODY WarshallsPkg IS
>    
>    PROCEDURE WarshallExecute(InArray : IN MY_ARRAY; OutArray : OUT MY_ARRAY) IS
> 
> --   TestArray: ARRAY(InArray'FIRST..InArray'LAST) OF Integer;
> 
>    BEGIN
>    FOR I IN InArray'FIRST..InArray'LAST LOOP
>       FOR J IN InArray'FIRST..InArray'LAST LOOP
>          IF InArray(J,I) = True THEN --true nodes connected
>             FOR K IN InArray'FIRST..InArray'LAST LOOP
>                OutArray(J,K) := InArray(J,K) OR InArray(I,K);
>             END LOOP;
>          END IF;
>       END LOOP;
>    END LOOP;
> 
>    END WarshallExecute;
>    
> END WarshallsPkg;
> 

try

package body WarshallsPkg is
   procedure WarshallExecute(Arr : in out My_Array) is
   begin
   for I in Arr'range loop
      for J in Arr'range loop
         if Arr(J,I) then --true nodes connected
            for k in Arr'range loop
               Arr(J,K) := Arr(J,K) or Arr(I,K);
            end loop;
         end if;
      end loop;
   end loop;
 end WarshallExecute;
end WarshallsPkg;


and then



>   WarshallExecute(Array2d, Array2d);

WarshallsPkg.WarshallExecute(Array2d);


You pass the same array both as in and out to WarshallExecute.
Why not pass it as in out ?




-- 
--
Björn

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-03 18:36 ` James Brewer
  2016-10-03 19:43   ` Björn Lundin
@ 2016-10-03 20:16   ` Jeffrey R. Carter
  1 sibling, 0 replies; 24+ messages in thread
From: Jeffrey R. Carter @ 2016-10-03 20:16 UTC (permalink / raw)


On 10/03/2016 11:36 AM, James Brewer wrote:
>
>    TYPE MY_ARRAY IS ARRAY(INTEGER RANGE <>, INTEGER RANGE <>) OF Boolean;
>
>    Array2d: ARRAY (1..N, 1..N) OF Boolean;

Your code has lots of problems, but an obvious reason why it doesn't work is 
that your procedure takes parameters of type My_Array, but the object you pass 
in twice has the anonymous array type of its declaration. These are 2 different 
types, and you can't use one where the other is required.

-- 
Jeff Carter
"We'll make Rock Ridge think it's a chicken
that got caught in a tractor's nuts!"
Blazing Saddles
87

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-03 19:43   ` Björn Lundin
@ 2016-10-03 21:59     ` James Brewer
  2016-10-03 23:16       ` Anh Vo
  2016-10-04 11:29       ` Brian Drummond
  0 siblings, 2 replies; 24+ messages in thread
From: James Brewer @ 2016-10-03 21:59 UTC (permalink / raw)


On Monday, October 3, 2016 at 2:44:01 PM UTC-5, björn lundin wrote:
> On 2016-10-03 20:36, James Brewer wrote:
> > On Friday, September 30, 2016 at 5:28:01 PM UTC-5, dia...@gmail.com wrote:
> >> Hello again, new Ada programmer with a hopefully easy question. 
> >> I am trying to take a 2d array pass it to a package have the package change the data in the 2d array and send the same 2d array (with modified data) back to the main program. I am currently away from my computer or I would show you my broken code, though I doubt it would be useful. 
> >>
> >> Thanks
> > 
> > 
> > PACKAGE BODY WarshallsPkg IS
> >    
> >    PROCEDURE WarshallExecute(InArray : IN MY_ARRAY; OutArray : OUT MY_ARRAY) IS
> > 
> > --   TestArray: ARRAY(InArray'FIRST..InArray'LAST) OF Integer;
> > 
> >    BEGIN
> >    FOR I IN InArray'FIRST..InArray'LAST LOOP
> >       FOR J IN InArray'FIRST..InArray'LAST LOOP
> >          IF InArray(J,I) = True THEN --true nodes connected
> >             FOR K IN InArray'FIRST..InArray'LAST LOOP
> >                OutArray(J,K) := InArray(J,K) OR InArray(I,K);
> >             END LOOP;
> >          END IF;
> >       END LOOP;
> >    END LOOP;
> > 
> >    END WarshallExecute;
> >    
> > END WarshallsPkg;
> > 
> 
> try
> 
> package body WarshallsPkg is
>    procedure WarshallExecute(Arr : in out My_Array) is
>    begin
>    for I in Arr'range loop
>       for J in Arr'range loop
>          if Arr(J,I) then --true nodes connected
>             for k in Arr'range loop
>                Arr(J,K) := Arr(J,K) or Arr(I,K);
>             end loop;
>          end if;
>       end loop;
>    end loop;
>  end WarshallExecute;
> end WarshallsPkg;
> 
> 
> and then
> 
> 
> 
> >   WarshallExecute(Array2d, Array2d);
> 
> WarshallsPkg.WarshallExecute(Array2d);
> 
> 
> You pass the same array both as in and out to WarshallExecute.
> Why not pass it as in out ?
> 
> 
> 
> 
> -- 
> --
> Björn

Thanks for the help. It's hard to know what a language can and cannot do when your just starting out. Unfortunately, I'm still getting an error saying [expected type"MY_ARRAY" defined at warshallspkg.ads:5 warshallmain.adb:60:20: found type Array2d declare at line 19]. It may have something to do with what Jeffery was alluding too but I don't know enough to say for sure.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-03 21:59     ` James Brewer
@ 2016-10-03 23:16       ` Anh Vo
  2016-10-04 11:29       ` Brian Drummond
  1 sibling, 0 replies; 24+ messages in thread
From: Anh Vo @ 2016-10-03 23:16 UTC (permalink / raw)


On Monday, October 3, 2016 at 2:59:02 PM UTC-7, James Brewer wrote:
> On Monday, October 3, 2016 at 2:44:01 PM UTC-5, björn lundin wrote:
> > On 2016-10-03 20:36, James Brewer wrote:
> > > On Friday, September 30, 2016 at 5:28:01 PM UTC-5, dia...@gmail.com 
> Thanks for the help. It's hard to know what a language can and cannot do when your just starting out. Unfortunately, I'm still getting an error saying [expected type"MY_ARRAY" defined at warshallspkg.ads:5 warshallmain.adb:60:20: found type Array2d declare at line 19]. It may have something to do with what Jeffery was alluding too but I don't know enough to say for sure.

I suggest that you post what you currently have. So, help can be provided quicker.

Anh Vo

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-03 21:59     ` James Brewer
  2016-10-03 23:16       ` Anh Vo
@ 2016-10-04 11:29       ` Brian Drummond
  2016-10-05 16:16         ` James Brewer
  1 sibling, 1 reply; 24+ messages in thread
From: Brian Drummond @ 2016-10-04 11:29 UTC (permalink / raw)


On Mon, 03 Oct 2016 14:59:01 -0700, James Brewer wrote:

> On Monday, October 3, 2016 at 2:44:01 PM UTC-5, björn lundin wrote:
>> On 2016-10-03 20:36, James Brewer wrote:

> 
> Thanks for the help. It's hard to know what a language can and cannot do
> when your just starting out. Unfortunately, I'm still getting an error
> saying [expected type"MY_ARRAY" defined at warshallspkg.ads:5
> warshallmain.adb:60:20: found type Array2d declare at line 19]. It may
> have something to do with what Jeffery was alluding too but I don't know
> enough to say for sure.

The error message pretty much tells you : you have created a new 
anonymous type of boolean array instead of the type you created earlier.

So, declare the actual parameter using that type:

   Array2d: WarshallsPkg.MY_ARRAY(1..N,   1..N);

-- Brian


^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-04 11:29       ` Brian Drummond
@ 2016-10-05 16:16         ` James Brewer
  2016-10-05 17:19           ` James Brewer
  0 siblings, 1 reply; 24+ messages in thread
From: James Brewer @ 2016-10-05 16:16 UTC (permalink / raw)


On Tuesday, October 4, 2016 at 6:29:51 AM UTC-5, Brian Drummond wrote:
> On Mon, 03 Oct 2016 14:59:01 -0700, James Brewer wrote:
> 
> > On Monday, October 3, 2016 at 2:44:01 PM UTC-5, björn lundin wrote:
> >> On 2016-10-03 20:36, James Brewer wrote:
> 
> > 
> > Thanks for the help. It's hard to know what a language can and cannot do
> > when your just starting out. Unfortunately, I'm still getting an error
> > saying [expected type"MY_ARRAY" defined at warshallspkg.ads:5
> > warshallmain.adb:60:20: found type Array2d declare at line 19]. It may
> > have something to do with what Jeffery was alluding too but I don't know
> > enough to say for sure.
> 
> The error message pretty much tells you : you have created a new 
> anonymous type of boolean array instead of the type you created earlier.
> 
> So, declare the actual parameter using that type:
> 
>    Array2d: WarshallsPkg.MY_ARRAY(1..N,   1..N);
> 
> -- Brian

I tried it and it seems to be working.
 
Thank you to everyone helped.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-05 16:16         ` James Brewer
@ 2016-10-05 17:19           ` James Brewer
  2016-10-05 18:57             ` Jeffrey R. Carter
  2016-10-05 19:47             ` Anh Vo
  0 siblings, 2 replies; 24+ messages in thread
From: James Brewer @ 2016-10-05 17:19 UTC (permalink / raw)



Well, I thought it worked by I'm just getting 1's for the output.

I'll post the new code if you all are still interested in helping me out.

                 -- Interface of WarshallsPkg

PACKAGE WarshallsPkg IS

   TYPE MY_ARRAY IS ARRAY(INTEGER RANGE <>, INTEGER RANGE <>) OF Boolean;

   PROCEDURE WarshallExecute(Arr : IN OUT MY_ARRAY);

end WarshallsPkg;

                 -- Implementation of WarshallsPkg

PACKAGE BODY WarshallsPkg IS
   PROCEDURE WarshallExecute(Arr : IN OUT My_Array) IS
   BEGIN
      FOR I IN Arr'RANGE LOOP
         FOR J IN Arr'RANGE LOOP
            IF Arr(J,I) THEN --true nodes connected
               FOR K IN Arr'RANGE LOOP
                  Arr(J,K) := Arr(J,K) OR Arr(I,K);
               END LOOP;
            END IF;
         END LOOP;
      END LOOP;
   END WarshallExecute;
END WarshallsPkg;

-- Lab 1 C version read and write to file use package
-- Programmed by James Brewer
-- Version 12

--put this in every program for now
--WITH Ada.Command_Line;
WITH Text_IO; USE Text_IO; -- This gets the IO facility.
WITH Ada.Integer_Text_IO; USE Ada.Integer_Text_IO; -- This gets the integer IO facility.
WITH WarshallsPkg; USE WarshallsPkg; --package setup


-- * main procedure *
PROCEDURE WarshallsMain IS

   Input : File_Type; -- input file declaration
   Output : File_Type; -- output file declaration
   N : Integer := 9; -- max size of array -- why does this overflow if not set?
   YOrN : String(1..1); -- y or n for node connections
   OutputConvertion : Integer; -- used for conversion from true/false to 1/0

   TestString : String(1..1);

-- highlight section in code that creates the array
--   Array2d: ARRAY (1..N, 1..N) OF Boolean; --****** highlight this section ********
   Array2d: WarshallsPkg.MY_ARRAY(1..N,   1..N);



-- * main procedure starts begins*
BEGIN
-- try to use io redirection to pull data from file
-- open input.txt file
   Open (File => Input,
         Mode => In_File,
         Name => "input.txt");


-- output to file creation section
   Create (File => Output,
      Mode => Out_File,
      Name => "output.txt");

-- input node connection data collection
   -- ask for size of array
   Put("How many nodes need to be processed? ");
   TestString := Get_Line (Input); -- get number of nodes
   N := Integer'Value(TestString); -- convert number of nodes to Integer
   put(N); -- display n on screen
--   Get(N); -- ** not used
   New_Line(2);

   FOR X IN 1..N LOOP
      FOR Y IN 1..N LOOP
         Put("Is node");
         Put(X);
         Put(" connected to node ");
         Put(Y);
         Put(" y/n ");
         YOrN := Get_Line(Input);
 --        Get(YOrN); -- ** not used
         put(YOrN); new_line(1); -- display choice on screen
         if yOrN = "y" then
            Array2d(X,Y) := True;
         ELSE Array2d(X,Y) := False;
         END IF;
      END LOOP;
   END LOOP;


-- Warshallpkg implementation

   WarshallExecute(Array2d);
--      FOR I IN 1..N LOOP
--      FOR J IN 1..N LOOP
--         IF Array2d(J,I) = True THEN --true nodes connected
--            FOR K IN 1..N LOOP
--               Array2d(J,K) := Array2d(J,K) OR Array2d(I,K);
--            END LOOP;
--         END IF;
--      END LOOP;
--   END LOOP;


-- print each transaction as it is processed

-- *********** output to file ************

-- column label output
   New_Line(Output, 2);
   Put(Output, "           ");
   FOR I IN 1..N LOOP
      Put(Output, I);
   END LOOP;
   New_Line(Output, 1);

-- data matrix output
   FOR I IN 1..N LOOP
      Put(Output, I);
      FOR J IN 1..N LOOP
         IF Array2d(I,J) = True THEN
            OutputConvertion := 1;
         ELSE
            OutputConvertion := 0;
         END IF;
         Put(Output, OutputConvertion);
      END LOOP;
      New_Line(Output, 1);
   END LOOP;

--close files
   Close (Input);
   Close (Output);
exception
   when End_Error =>
      if Is_Open(Input) then
         Close (Input);
      end if;
      if Is_Open(Output) then
         Close (Output);
      end if;

END WarshallsMain;
-- * main procedure ends *


^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-05 17:19           ` James Brewer
@ 2016-10-05 18:57             ` Jeffrey R. Carter
  2016-10-05 19:53               ` James Brewer
  2016-10-05 19:47             ` Anh Vo
  1 sibling, 1 reply; 24+ messages in thread
From: Jeffrey R. Carter @ 2016-10-05 18:57 UTC (permalink / raw)


On 10/05/2016 10:19 AM, James Brewer wrote:
>
> Well, I thought it worked by I'm just getting 1's for the output.

How value do you input when asked "How many nodes need to be processed? " If 
it's less than 9, the remaining elements in the array will have random values 
("stack junk") which will probably be True at least half the time. Since your 
algorithm processes all the elements in the 9x9 array, the odds are good that 
every node will end up being True.

The correction is not to declare the array until you know how big it needs to be.

-- 
Jeff Carter
"You can never forget too much about C++."
115

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-05 17:19           ` James Brewer
  2016-10-05 18:57             ` Jeffrey R. Carter
@ 2016-10-05 19:47             ` Anh Vo
  2016-10-05 20:30               ` James Brewer
  1 sibling, 1 reply; 24+ messages in thread
From: Anh Vo @ 2016-10-05 19:47 UTC (permalink / raw)


On Wednesday, October 5, 2016 at 10:19:31 AM UTC-7, James Brewer wrote:
> Well, I thought it worked by I'm just getting 1's for the output.
> 
> I'll post the new code if you all are still interested in helping me out.

Please do. Do not forget to include input file, input.txt.

Anh Vo

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-05 18:57             ` Jeffrey R. Carter
@ 2016-10-05 19:53               ` James Brewer
  2016-10-05 19:57                 ` AdaMagica
  2016-10-05 20:17                 ` Jeffrey R. Carter
  0 siblings, 2 replies; 24+ messages in thread
From: James Brewer @ 2016-10-05 19:53 UTC (permalink / raw)


On Wednesday, October 5, 2016 at 1:57:08 PM UTC-5, Jeffrey R. Carter wrote:
> On 10/05/2016 10:19 AM, James Brewer wrote:
> >
> > Well, I thought it worked by I'm just getting 1's for the output.
> 
> How value do you input when asked "How many nodes need to be processed? " If 
> it's less than 9, the remaining elements in the array will have random values 
> ("stack junk") which will probably be True at least half the time. Since your 
> algorithm processes all the elements in the 9x9 array, the odds are good that 
> every node will end up being True.
> 
> The correction is not to declare the array until you know how big it needs to be.
> 
> -- 
> Jeff Carter
> "You can never forget too much about C++."
> 115

Please remember I'm just starting in this language. So if I say anything ridiculous it is not intentional. It doesn't seem to allow for declarations after the "begin" statement. Also, it worked without the package implementation as best as I could tell with my test data.


^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-05 19:53               ` James Brewer
@ 2016-10-05 19:57                 ` AdaMagica
  2016-10-05 20:17                 ` Jeffrey R. Carter
  1 sibling, 0 replies; 24+ messages in thread
From: AdaMagica @ 2016-10-05 19:57 UTC (permalink / raw)


Am Mittwoch, 5. Oktober 2016 21:53:07 UTC+2 schrieb James Brewer:
> Please remember I'm just starting in this language. So if I say anything ridiculous it is not intentional. It doesn't seem to allow for declarations after the "begin" statement. Also, it worked without the package implementation as best as I could tell with my test data.

Of course not. The declare block is used for this purpose:

N := Some_Value;

declare
  My_Array: My_Array_Type (1 .. N);
begin
  ...
end;


^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-05 19:53               ` James Brewer
  2016-10-05 19:57                 ` AdaMagica
@ 2016-10-05 20:17                 ` Jeffrey R. Carter
  2016-10-05 20:58                   ` James Brewer
  1 sibling, 1 reply; 24+ messages in thread
From: Jeffrey R. Carter @ 2016-10-05 20:17 UTC (permalink / raw)


On 10/05/2016 12:53 PM, James Brewer wrote:
>
> Please remember I'm just starting in this language. So if I say anything
> ridiculous it is not intentional. It doesn't seem to allow for declarations
> after the "begin" statement. Also, it worked without the package
> implementation as best as I could tell with my test data.

Yes, after "begin" but before "exception" or "end" you can only have statements. 
But it would be a good idea to be familiar with the kinds of statements, which 
are given in ARM 5

http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-5.html

ARM 5.1 lists all the kinds of statements. Of particular interest might be the 
block statement, described in more detail in ARM 5.6.

-- 
Jeff Carter
"You can never forget too much about C++."
115


^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-05 19:47             ` Anh Vo
@ 2016-10-05 20:30               ` James Brewer
  0 siblings, 0 replies; 24+ messages in thread
From: James Brewer @ 2016-10-05 20:30 UTC (permalink / raw)


On Wednesday, October 5, 2016 at 2:47:36 PM UTC-5, Anh Vo wrote:
> On Wednesday, October 5, 2016 at 10:19:31 AM UTC-7, James Brewer wrote:
> > Well, I thought it worked by I'm just getting 1's for the output.
> > 
> > I'll post the new code if you all are still interested in helping me out.
> 
> Please do. Do not forget to include input file, input.txt.
> 
> Anh Vo

here is the current input file. (it's very rudimentary)

4
n
y
n
n
n
n
y
n
n
n
n
n
n
n
y
n

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-05 20:17                 ` Jeffrey R. Carter
@ 2016-10-05 20:58                   ` James Brewer
  2016-10-05 22:11                     ` Anh Vo
  0 siblings, 1 reply; 24+ messages in thread
From: James Brewer @ 2016-10-05 20:58 UTC (permalink / raw)


On Wednesday, October 5, 2016 at 3:18:01 PM UTC-5, Jeffrey R. Carter wrote:
> On 10/05/2016 12:53 PM, James Brewer wrote:
> >
> > Please remember I'm just starting in this language. So if I say anything
> > ridiculous it is not intentional. It doesn't seem to allow for declarations
> > after the "begin" statement. Also, it worked without the package
> > implementation as best as I could tell with my test data.
> 
> Yes, after "begin" but before "exception" or "end" you can only have statements. 
> But it would be a good idea to be familiar with the kinds of statements, which 
> are given in ARM 5
> 
> http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-5.html
> 
> ARM 5.1 lists all the kinds of statements. Of particular interest might be the 
> block statement, described in more detail in ARM 5.6.
> 
> -- 
> Jeff Carter
> "You can never forget too much about C++."
> 115

Implementing the block statement looks like it may have solved the problem. I will test it further.

Thanks for the help

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Passing a 2d array into a package and returning the same processed 2d back to main
  2016-10-05 20:58                   ` James Brewer
@ 2016-10-05 22:11                     ` Anh Vo
  0 siblings, 0 replies; 24+ messages in thread
From: Anh Vo @ 2016-10-05 22:11 UTC (permalink / raw)


On Wednesday, October 5, 2016 at 1:58:40 PM UTC-7, James Brewer wrote:
> On Wednesday, October 5, 2016 at 3:18:01 PM UTC-5, Jeffrey R. Carter wrote:
> > On 10/05/2016 12:53 PM, James Brewer wrote:
> > >
> > > Please remember I'm just starting in this language. So if I say anything
> > > ridiculous it is not intentional. It doesn't seem to allow for declarations
> > > after the "begin" statement. Also, it worked without the package
> > > implementation as best as I could tell with my test data.
> > 
> > Yes, after "begin" but before "exception" or "end" you can only have statements. 
> > But it would be a good idea to be familiar with the kinds of statements, which 
> > are given in ARM 5
> > 
> > http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-5.html
> > 
> > ARM 5.1 lists all the kinds of statements. Of particular interest might be the 
> > block statement, described in more detail in ARM 5.6.
> > 
> > -- 
> > Jeff Carter
> > "You can never forget too much about C++."
> > 115
> 
> Implementing the block statement looks like it may have solved the problem. I will test it further.

I make it work, also. Just like all mentioned, your array object does not match with array dimension in input.txt file. Since you have solved your problem, my solution is not necessary.

Anh Vo


^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2016-10-05 22:11 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-30 22:27 Passing a 2d array into a package and returning the same processed 2d back to main diane74
2016-10-01  0:01 ` Jeffrey R. Carter
2016-10-01 20:01 ` Aurele
2016-10-01 21:04   ` Jeffrey R. Carter
2016-10-01 21:59     ` Aurele
2016-10-01 22:16       ` Jeffrey R. Carter
2016-10-02  0:09         ` Aurele
2016-10-02  7:31           ` Simon Wright
2016-10-03 18:36 ` James Brewer
2016-10-03 19:43   ` Björn Lundin
2016-10-03 21:59     ` James Brewer
2016-10-03 23:16       ` Anh Vo
2016-10-04 11:29       ` Brian Drummond
2016-10-05 16:16         ` James Brewer
2016-10-05 17:19           ` James Brewer
2016-10-05 18:57             ` Jeffrey R. Carter
2016-10-05 19:53               ` James Brewer
2016-10-05 19:57                 ` AdaMagica
2016-10-05 20:17                 ` Jeffrey R. Carter
2016-10-05 20:58                   ` James Brewer
2016-10-05 22:11                     ` Anh Vo
2016-10-05 19:47             ` Anh Vo
2016-10-05 20:30               ` James Brewer
2016-10-03 20:16   ` Jeffrey R. Carter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox