comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Array/Matrix Help
Date: Wed, 04 Apr 2012 23:00:31 +0300
Date: 2012-04-04T23:00:31+03:00	[thread overview]
Message-ID: <9u3nivFq18U1@mid.individual.net> (raw)
In-Reply-To: <eef91ea3-652f-47e4-8de2-f26a3d8ae1ce@f6g2000vbc.googlegroups.com>

On 12-04-04 05:55 , Will wrote:
> I need to create function Create_Matrix that  accepts four
> Int_Arrays.  It must determine if all 4 Int_Arrays are of equal length
> and then creates and returns an Int_Matrix of the 2 or more arrays
> with equal length.
>
> Here is what I am thinking so far, I believe it will work but seems
> inefficient and I am looking for efficiency in my coding.  I am not
> sure how to determine which ones are equal other than a bunch of if/
> elsif statement that cover every possible combination of them being
> equal then write the code that will create the matrix for that given
> combination of arrays.   Any ideas on something better than the
> following.  I am assuming that there will not be 2 sets of arrays that
> are equal (i.e. 2 arrays of length 5 , and another 2 of length 7)
> There would only be 1 common length.
>
>    function Create_Matrix (A, B, C, D : Int_Array) return Int_Matrix
>
>         begin
>
>             if A'Length = B'Length and A'Length = C'Length and A'Length
> = D'Length then
>
>   Then continue to do this all the way until I cover every combination
> of 2 or more arrays that are equal length and then whichever if
> statement it satisfies create the Int_Matrix by each array being its
> own line inside the matrix.
>
> Can anyone offer help or ideas.  Any hints/help is greatly
> appreciated... I prefer that over just giving me the code.

I always prefer systematic coding over efficiency. Here is what I would 
do. Put the four lengths into a 4-element array:

    Length : constant array (1 .. 4) of Natural := (
	A'Length, B'Length, C'Length, D'Length);

Then make another array that counts how many times each length occurs:

    Occurrences : array (1 .. 4) of Natural := (others => 0);

    for I in 1 .. 4 loop
       for J in 1 .. 4 loop
          if Length(J) = Length(I) then
             Occurrences(I) := Occurrences(I) + 1;
          end if;
       end loop;
    end loop;

Then find a maximal element of Occurrences, say Occurrences(K), and make 
a matrix of all the input vectors that have the length Length(K). 
(Finding a maximal value can be combined with the loops that compute 
Occurrences, of course.)

HTH,

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



      parent reply	other threads:[~2012-04-04 20:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-04  2:55 Array/Matrix Help Will
2012-04-04 16:20 ` Adam Beneschan
2012-04-04 20:00 ` Niklas Holsti [this message]
replies disabled

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