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=0.4 required=5.0 tests=BAYES_00,FORGED_MUA_MOZILLA autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,3cb31983495c2fde X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.227.230 with SMTP id sd6mr6374462pbc.8.1334945526189; Fri, 20 Apr 2012 11:12:06 -0700 (PDT) Path: r9ni78876pbh.0!nntp.google.com!news1.google.com!volia.net!news2.volia.net!feed-A.news.volia.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Ada95 Help 2D arrays Date: Fri, 20 Apr 2012 21:12:03 +0300 Organization: Tidorum Ltd Message-ID: <9vdn7jFdmeU1@mid.individual.net> References: <31137140.191.1334922395977.JavaMail.geo-discussion-forums@ynjb15> <5774709.1285.1334943237337.JavaMail.geo-discussion-forums@vbbfk16> Mime-Version: 1.0 X-Trace: individual.net C997lSNq9ykKK0gljEUWfQjEShUMS7Rv6flbmSw37e7dwubRGt Cancel-Lock: sha1:+flexs7GchKcBv6Gt75Yaa0MNfY= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:11.0) Gecko/20120327 Thunderbird/11.0.1 In-Reply-To: <5774709.1285.1334943237337.JavaMail.geo-discussion-forums@vbbfk16> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Date: 2012-04-20T21:12:03+03:00 List-Id: On 12-04-20 20:33 , Will wrote: > Here is my solution and no doubt it does work! Looks pretty good to me. I have taken the liberty of cleaning up the formatting a bit, replacing the TABs with spaces. I have added some remarks. > type Int_Array is array(Positive range<>) of Integer; > type Int_Matrix is array(Positive range<>, Positive range<>) of Integer; > > > function Best_Combined_Score(Grid : Int_Matrix) return Integer is The problem statement said that the numbers in the Grid are positive integers. I would therefore use the type Natural instead of Integer for the function return type and the local variables. > Total : integer := 0; > RowTotal : integer := 0; > ColumnTotal : integer := 0; The initializations of Total, RowTotal, and ColumnTotal are unnecessary: the variables are initialized below before being used. > Sum : Integer := 0; > begin > > for R in Grid'Range(1) loop IMO it would be more balanced to call this (outermost) row index "Row", since it plays the same role as the "Column" index below, and use the identifier "R" in the inner loop over row indices. > RowTotal := 0; > for C in Grid'Range(2) loop > Rowtotal := Rowtotal +Grid(R,C); > end loop; > > for Column in Grid'Range(2) loop > ColumnTotal := 0; > for Row in Grid'Range(1)loop > ColumnTotal := ColumnTotal + Grid(Row,Column); > end loop; > > Total := RowTotal + ColumnTotal - Grid(R, Column); > > if Total > Sum then > Sum := Total; > end if; > > end loop; > end loop; > > return Sum; > end Best_Combined_Score; I think it works. But it is a cubic-complexity algorithm (loops nested three deep). A quadratic-complexity algorithm (loops nested two deep) would be more efficient for large grids. Of course, premature optimization is the root of much evil, but the quadratic algorithm is not much more difficult and demonstrates some nice aspects of local array variables in Ada. -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .