comp.lang.ada
 help / color / mirror / Atom feed
* Array/Matrix Help
@ 2012-04-04  2:55 Will
  2012-04-04 16:20 ` Adam Beneschan
  2012-04-04 20:00 ` Niklas Holsti
  0 siblings, 2 replies; 3+ messages in thread
From: Will @ 2012-04-04  2:55 UTC (permalink / raw)


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.


-Will







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

* Re: Array/Matrix Help
  2012-04-04  2:55 Array/Matrix Help Will
@ 2012-04-04 16:20 ` Adam Beneschan
  2012-04-04 20:00 ` Niklas Holsti
  1 sibling, 0 replies; 3+ messages in thread
From: Adam Beneschan @ 2012-04-04 16:20 UTC (permalink / raw)


On Tuesday, April 3, 2012 7:55:26 PM UTC-7, 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 don't think there's a good way to do this except with a bunch of if/then/elses.  If you're super-concerned about efficiency, you might want to create a 6-bit integer where each bit corresponds to one of the six pairs of comparisons (add 1 if A'Length=B'Length, add 2 if A'Length=C'Length, add 4 if something...) and then do a CASE on the integer to cover all 64 possibilities.  But that's an ugly way to write this just to eliminate a few nanoseconds.

                    -- Adam



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

* Re: Array/Matrix Help
  2012-04-04  2:55 Array/Matrix Help Will
  2012-04-04 16:20 ` Adam Beneschan
@ 2012-04-04 20:00 ` Niklas Holsti
  1 sibling, 0 replies; 3+ messages in thread
From: Niklas Holsti @ 2012-04-04 20:00 UTC (permalink / raw)


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
       .      @       .



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

end of thread, other threads:[~2012-04-04 20:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-04  2:55 Array/Matrix Help Will
2012-04-04 16:20 ` Adam Beneschan
2012-04-04 20:00 ` Niklas Holsti

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