From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: New to Ada need help implementing Warshall's algorithm Date: Mon, 26 Sep 2016 21:40:50 +0100 Organization: A noiseless patient Spider Message-ID: References: <955937c4-b9f9-4e21-9d22-98382df2f45f@googlegroups.com> <0e690fe0-7ac8-4843-8792-50ae14729bcc@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="e5f31545d127a6738a4229c67d648b1c"; logging-data="11654"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18bp1MSJPwxdeyhijSsA4ONSPwfMVcozi4=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (darwin) Cancel-Lock: sha1:wQmfKbqAcqy6Ldoa+sR6TN/vANc= sha1:x+OdNGF2ByixgCAmU2iTpCHXDVE= Xref: news.eternal-september.org comp.lang.ada:31905 Date: 2016-09-26T21:40:50+01:00 List-Id: Stephen Leake writes: > On Friday, September 23, 2016 at 10:07:53 AM UTC-5, James Brewer wrote: >> >> It is three diffent sets of data. I think I may try to simplify the >> input data to a 0,1 2d array for each set, read the set in and then >> process each set writing it to an output file (the last two I will >> have to figure out). > > Here's a working program that does the IO, to get you started. It > reads from a file, writes to standard out. And here's a different one, which works out the size of the matrix by finding the number of unique names in the input. The structure supports strings for names, but the implementation only handles single-character names because I was being lazy. Input: e b a b b c b d d a d e c e Output: a 3 b 2 c 4 d 5 e 1 01000 00011 01000 10000 10100 Code: with Ada.Command_Line; with Ada.Containers.Indefinite_Ordered_Maps; with Ada.Text_IO; use Ada.Text_IO; procedure Brewer is File : File_Type; package Names_To_Indices is new Ada.Containers.Indefinite_Ordered_Maps (String, Positive); Names : Names_To_Indices.Map; procedure Add_If_New (Name : String) is use type Ada.Containers.Count_Type; begin if not Names.Contains (Name) then Names.Insert (Name, Positive (Names.Length + 1)); end if; end Add_If_New; begin Open (File, In_File, Ada.Command_Line.Argument (1)); -- each line is to have format Name_1 Name_2, implying there is a -- path from Name_1 to Name_2. loop begin declare Line : constant String := Get_Line (File); begin -- I'm being lazy here and assuming single-character Names. Add_If_New (Line (1 .. 1)); Add_If_New (Line (3 .. 3)); end; exception when others => exit; end; end loop; Reset (File); -- output name-to-index (ordered by name, not index, sorry) for J in Names.Iterate loop Put_Line (Names_To_Indices.Key (J) & " " & Names_To_Indices.Element (J)'Img); end loop; -- now we know the size of the matrix declare Size : constant Positive := Positive (Names.Length); Matrix : array (1 .. Size, 1 .. Size) of Boolean := (others => (others => False)); begin -- populate the matrix loop begin declare Line : constant String := Get_Line (File); From : constant Positive := Names.Element (Line (1 .. 1)); To : constant Positive := Names.Element (Line (3 .. 3)); begin Matrix (From, To) := True; end; exception when others => exit; end; end loop; Close (File); -- dump the matrix for J in Matrix'Range (1) loop for K in Matrix'Range (2) loop Put (if Matrix (J, K) then '1' else '0'); end loop; New_Line; end loop; end; end Brewer;