comp.lang.ada
 help / color / mirror / Atom feed
* Two Dimensional Array
@ 2002-02-15 12:30 Brian A Crawford
  2002-02-15 17:28 ` Jeffrey Carter
  2002-02-15 18:15 ` Stephen Leake
  0 siblings, 2 replies; 7+ messages in thread
From: Brian A Crawford @ 2002-02-15 12:30 UTC (permalink / raw)


I have a program to read  two numbers at a time from a file and then
use a two dimensional array to look at that reference to see if a
number 1 or 0 in assigned to the cell referenced.
For example if the numbers are 14,20 it would look up this in the
array and return a 1 say.
I have already got it to work (see fragment below) however the real
world problem is that the numbers 1 to 50 are not actually in
numerical order but jumbled up in a different sequence.

The code fragment shows the end result of my efforts so far.  
My problem is associating the values 1 or 0 to each cell of the array.
By having the numbers 1 to 50 in numerical order the numbers 1 and 0
are very haphazard placed and are difficult to define and update.  

If the array was firstly defined as say 1,21,34,50,17,4, etc, 48,16
then the 1 and 0 are placed together in the array and are very easy to
associate quickly in groups of 5. 
Example 
Event :=  (1 => (1,1,1,1,1,0,0,0,0,0,1,1,1,1,1 etc 
	21 => (0,0,0,0,0,1,1,1,1,1,0,0,0,0,0, etc
	34 => (0,0,0,0,0,0,0,0,0,0,1,1,1,1,1, etc
and so on.

This is a code fragment. 
      Number1	: Integer;
      Number2	: Integer;
        
subtype Firstevent is Integer range 1..50;
subtype Secondevent is Integer range 1..50;
      type Two_Events_Type is array (Firstevent, Secondevent) of
Integer;
Event : Two_Events_Type; 
   
begin
   --get input data
   
      Event :=   (0 =>
(1,0,0,1,0,1,1,0,0,0,0,1,1,0,1,1,1,0,1,etc,0,1,0,1,1,1,0,0,1,1),
1=>(1,1,0,1,0,0,1,0,0,1,1,1,etc
2=> and so forth

as you can see the 1's and 0's are all over the place.
How can I define subtypes FirstEvent and SecondEvent out of sequence?

Secondly then can I make an abbreviation to the array referencing eg
if AN is the sequence of 5 x 1's at {3,4,5,6,7} and BN is  is the
sequence of 5 x 1's at {17,18,19,20,21} position and similar for CN DN
EN etc how may I use that abbreviation in some way like this?

Event := (0 => (AN, BN, others => 0),
	21 => (CN, others =>0),
	34 => (DN, AN, CN, others 0)) etc

Thank you for any pointers.

Regards
Brian
          



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

* Re: Two Dimensional Array
  2002-02-15 12:30 Two Dimensional Array Brian A Crawford
@ 2002-02-15 17:28 ` Jeffrey Carter
  2002-02-15 18:15 ` Stephen Leake
  1 sibling, 0 replies; 7+ messages in thread
From: Jeffrey Carter @ 2002-02-15 17:28 UTC (permalink / raw)


Brian A Crawford wrote:
> 
> The code fragment shows the end result of my efforts so far.
> My problem is associating the values 1 or 0 to each cell of the array.
> By having the numbers 1 to 50 in numerical order the numbers 1 and 0
> are very haphazard placed and are difficult to define and update.
> 
> If the array was firstly defined as say 1,21,34,50,17,4, etc, 48,16
> then the 1 and 0 are placed together in the array and are very easy to
> associate quickly in groups of 5.
> Example
> Event :=  (1 => (1,1,1,1,1,0,0,0,0,0,1,1,1,1,1 etc
>         21 => (0,0,0,0,0,1,1,1,1,1,0,0,0,0,0, etc
>         34 => (0,0,0,0,0,0,0,0,0,0,1,1,1,1,1, etc
> and so on.
> 
> This is a code fragment.
>       Number1   : Integer;
>       Number2   : Integer;
> 
> subtype Firstevent is Integer range 1..50;
> subtype Secondevent is Integer range 1..50;
>       type Two_Events_Type is array (Firstevent, Secondevent) of
> Integer;
> Event : Two_Events_Type;
> 
> begin
>    --get input data
> 
>       Event :=   (0 =>
> (1,0,0,1,0,1,1,0,0,0,0,1,1,0,1,1,1,0,1,etc,0,1,0,1,1,1,0,0,1,1),
> 1=>(1,1,0,1,0,0,1,0,0,1,1,1,etc
> 2=> and so forth
> 
> as you can see the 1's and 0's are all over the place.
> How can I define subtypes FirstEvent and SecondEvent out of sequence?

First, since zero is not a value of Firstevent, you may have problems
with assigning this aggregate to Event.

Second, since the components of Two_Events_Type can only have the values
0 and 1, it is probably a good idea to specify this:

subtype Bit is Integer range 0 .. 1;
type Two_Events_Type is array (Firstevent, Secondevent) of Bit;

Third, since Event is a constant, it is probably a good idea to define
it as such:

Event : constant Two_Events_Type := (...);

Fourth, you probably ought to review ARM 4.3.3 on named array aggregates
and ARM 3.8.1 on discrete choices.

You can't define an out of order numeric (sub)type, but you can order
things any way you like in a named aggregate. So you can say

Event : constant Two_Events_Type :=
   (01 => (01 | 07 | 13 | ... => 0, others => 1),
    02 => (...),
    ...);

> 
> Secondly then can I make an abbreviation to the array referencing eg
> if AN is the sequence of 5 x 1's at {3,4,5,6,7} and BN is  is the
> sequence of 5 x 1's at {17,18,19,20,21} position and similar for CN DN
> EN etc how may I use that abbreviation in some way like this?
> 
> Event := (0 => (AN, BN, others => 0),
>         21 => (CN, others =>0),
>         34 => (DN, AN, CN, others 0)) etc

No, but a named aggregate has the same effect.

-- 
Jeffrey Carter



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

* Re: Two Dimensional Array
  2002-02-15 12:30 Two Dimensional Array Brian A Crawford
  2002-02-15 17:28 ` Jeffrey Carter
@ 2002-02-15 18:15 ` Stephen Leake
  2002-02-15 22:50   ` Brian A Crawford
  1 sibling, 1 reply; 7+ messages in thread
From: Stephen Leake @ 2002-02-15 18:15 UTC (permalink / raw)


brianc@billybob.demon.co.uk (Brian A Crawford) writes:

> I have a program to read  two numbers at a time from a file and then
> use a two dimensional array to look at that reference to see if a
> number 1 or 0 in assigned to the cell referenced.
> <snip>

Try this (I made the array smaller for simplicity):

with Ada.Text_IO; use Ada.Text_IO;
procedure Misc_Array_Aggregate
is
   subtype Firstevent is Integer range 1 .. 5;
   subtype Secondevent is Integer range 1 .. 5;
   type Two_Events_Type is array (Firstevent, Secondevent) of Integer;

   Event : Two_Events_Type :=
      (1      => (1 | 3 | 5 => 1, others => 0),
       2      => (2 | 4 => 1, others => 0),
       others => (others => 0));

begin
   Ada.Text_IO.Put_Line ("It works!");
end Misc_Array_Aggregate;

-- 
-- Stephe



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

* Re: Two Dimensional Array
  2002-02-15 18:15 ` Stephen Leake
@ 2002-02-15 22:50   ` Brian A Crawford
  2002-02-16  2:05     ` Mike Silva
  0 siblings, 1 reply; 7+ messages in thread
From: Brian A Crawford @ 2002-02-15 22:50 UTC (permalink / raw)


On 15 Feb 2002 13:15:13 -0500, Stephen Leake
<stephen.a.leake.1@gsfc.nasa.gov> wrote:


>brianc@billybob.demon.co.uk (Brian A Crawford) writes:
>
>> I have a program to read  two numbers at a time from a file and then
>> use a two dimensional array to look at that reference to see if a
>> number 1 or 0 in assigned to the cell referenced.
>> <snip>
>
>Try this (I made the array smaller for simplicity):
>
>with Ada.Text_IO; use Ada.Text_IO;
>procedure Misc_Array_Aggregate
>is
>   subtype Firstevent is Integer range 1 .. 5;
>   subtype Secondevent is Integer range 1 .. 5;
>   type Two_Events_Type is array (Firstevent, Secondevent) of Integer;
>
>   Event : Two_Events_Type :=
>      (1      => (1 | 3 | 5 => 1, others => 0),
>       2      => (2 | 4 => 1, others => 0),
>       others => (others => 0));
>
>begin
>   Ada.Text_IO.Put_Line ("It works!");
>end Misc_Array_Aggregate;
>
>-- 
>-- Stephe
Hi Stephen
I have also experimented with your notation above.  It is still too
unweildly in a very large array that is not consecutive.    To use
your example the order would need to be be 3,1,2,5,4 for both
firstevent and secondevent.
My problem is the array is so big and is out of sequence.

If I had to just place the ones and zeros in the array once only I
would bite the bullet and get on with it.  But I have to fine tune the
placements 100 of times.  The consistent thing however is if I could
define the array out of sequence the groups of 1's and 0's would be in
small groups of 5 numbers (not consequtive numerically but
consequtively placed.
To use your example again 3,1,2 ; 1,2,5 and 2,5,4 etc would be the
small groupings inside the array.  The fact that it is so big is the
headache. Then I may be able to call the 1,2,5 : AN and the 2,5,4 :BN
etc,and just fine tune by moving the AN's and BN's etc around like a
kind of shorthand for each block of 1's ,defining others=> 0.


Regards
Brian



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

* Re: Two Dimensional Array
  2002-02-15 22:50   ` Brian A Crawford
@ 2002-02-16  2:05     ` Mike Silva
  2002-02-16  9:38       ` Brian A Crawford
  0 siblings, 1 reply; 7+ messages in thread
From: Mike Silva @ 2002-02-16  2:05 UTC (permalink / raw)


brianc@billybob.demon.co.uk (Brian A Crawford) wrote in message news:<3c6d8b32.18156525@news.demon.co.uk>...
><...>
>My problem is the array is so big and is out of sequence.
> 
> If I had to just place the ones and zeros in the array once only I
> would bite the bullet and get on with it.  But I have to fine tune the
> placements 100 of times.  The consistent thing however is if I could
> define the array out of sequence the groups of 1's and 0's would be in
> small groups of 5 numbers (not consequtive numerically but
> consequtively placed.
> To use your example again 3,1,2 ; 1,2,5 and 2,5,4 etc would be the
> small groupings inside the array.  The fact that it is so big is the
> headache. Then I may be able to call the 1,2,5 : AN and the 2,5,4 :BN
> etc,and just fine tune by moving the AN's and BN's etc around like a
> kind of shorthand for each block of 1's ,defining others=> 0.

How about starting with an empty array and then using a procedure to
set the '1's?  The procedure would be driven with data from another
array which holds the 5-sets (?) to be set.  This way you move from
the cumbersome notation of filling in bits of a sparse array to simply
filling in a fully-packed array with your sets of out-of-sequence
coordinates.

Mike



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

* Re: Two Dimensional Array
  2002-02-16  2:05     ` Mike Silva
@ 2002-02-16  9:38       ` Brian A Crawford
  2002-02-23 13:36         ` Nick Roberts
  0 siblings, 1 reply; 7+ messages in thread
From: Brian A Crawford @ 2002-02-16  9:38 UTC (permalink / raw)



On 15 Feb 2002 18:05:54 -0800, mjsilva697@earthlink.net (Mike Silva)
wrote:

>brianc@billybob.demon.co.uk (Brian A Crawford) wrote in message news:<3c6d8b32.18156525@news.demon.co.uk>...
>><...>
>>My problem is the array is so big and is out of sequence.
>> 
>> If I had to just place the ones and zeros in the array once only I
>> would bite the bullet and get on with it.  But I have to fine tune the
>> placements 100 of times.  The consistent thing however is if I could
>> define the array out of sequence the groups of 1's and 0's would be in
>> small groups of 5 numbers (not consequtive numerically but
>> consequtively placed.
>> To use your example again 3,1,2 ; 1,2,5 and 2,5,4 etc would be the
>> small groupings inside the array.  The fact that it is so big is the
>> headache. Then I may be able to call the 1,2,5 : AN and the 2,5,4 :BN
>> etc,and just fine tune by moving the AN's and BN's etc around like a
>> kind of shorthand for each block of 1's ,defining others=> 0.
>
>How about starting with an empty array and then using a procedure to
>set the '1's?  The procedure would be driven with data from another
>array which holds the 5-sets (?) to be set.  This way you move from
>the cumbersome notation of filling in bits of a sparse array to simply
>filling in a fully-packed array with your sets of out-of-sequence
>coordinates.
>
>Mike

Thanks Mike,
Very good idea.  
I was also giving some thought to writing a pre-program to read the
data and  optimise the array 1's prior to entering them into the array
and then resorting to shuffling the elements around.
I will have to give it a lot more thought as it is at the moment
beyond my limited coding knowledge.

Regards 
Brian



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

* Re: Two Dimensional Array
  2002-02-16  9:38       ` Brian A Crawford
@ 2002-02-23 13:36         ` Nick Roberts
  0 siblings, 0 replies; 7+ messages in thread
From: Nick Roberts @ 2002-02-23 13:36 UTC (permalink / raw)


Brian, I can give you an idea that you may or may not find useful.

If you have an array that you would like to index in some arbitrary order,
e.g.:

Data Array
	Index: 23 14  3 10 27 13 12 20 21  2 19 ...
	Value:  0  0  0  0  0  1  1  1  1  1  0 ...

The 'classic' way to do this is with a 'mapping' array, e.g.:

Mapping Array
	Index: 23 14  3 10 27 13 12 20 21  2 19 ...
	Value:  1  2  3  4  5  6  7  8  9 10 11 ...

This array 'maps' 23 -> 1, 14 -> 2, 3 -> 3, 10 -> 4, and so on.

In Ada, the mapping array could be defined as follows:

   type Index_Type is range 1..50;

   Map: constant array (Index_Type) of Index_Type :=
           ( 23 =>  1, 14 =>  2,  3 =>  3, 10 =>  4, 27 =>  5,
             13 =>  6, 12 =>  6  20 =>  8, ........... );

You could then declare an array convenient for declaring the data:

   Data: constant array (Index_Type) of Bit :=
            (  1.. 5 => 0,  6..10 => 1, 11..15 => 0, 16..20 => 1,
              21..25 => 0, 26..30 => 0, 31..35 => 1, 36..40 => 1,
              41..45 => 1, 46..50 => 0 );

You could then construct the actual array you need using the map:

	Work: array (Index_Type) of Bit;

   procedure Initialize_Work is
	begin
		for i in Work'Range loop
			Work(i) := Data(Map(i));
      end loop;
   end;

The 'tricky' bit here is how the Map array is used to generate an index for
indexing the Data array [Data(Map(i))]. It is important that you study this
construct until you understand how it works.

After Initialize_Work is called, the array Work will contain the bits in
the places they really should be.

Hope this helps.

You might be interested that this particular kind of map is called a
'bijection'. A bijection is a mapping that is both an 'injection' and a
'surjection'. In simple terms, an injection is a mapping which never maps
to the same thing twice, and a surjection is a mapping that is complete.

In order to help ensure that you have not made a mistake, you may wish to
write procedures that check the Map array for being an injection and a
surjection. I'll leave how to code these tests as an exercise!

-- 
Nick Roberts



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

end of thread, other threads:[~2002-02-23 13:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-15 12:30 Two Dimensional Array Brian A Crawford
2002-02-15 17:28 ` Jeffrey Carter
2002-02-15 18:15 ` Stephen Leake
2002-02-15 22:50   ` Brian A Crawford
2002-02-16  2:05     ` Mike Silva
2002-02-16  9:38       ` Brian A Crawford
2002-02-23 13:36         ` Nick Roberts

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