Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 1 > Newsgroup comp.lang.ada

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 02-28-2012, 07:15 PM
Will
Guest
 
Posts: n/a
Default Array Help?

Hello all,

I am looking for your help again. I know how to refer to a certain
index in an array. for example if some array A is (53,83,3,62,3,13)
then A(4) = 62 but what if I wanted to go through an array and
identify certain indexes by what they contain? In my particular case
I need to identify the indexes that have numbers in them that do not
end in 1 or 2. These numbers range from 1 to 4 digits. Any ideas on
how I can do this?

Thanks a bunch
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 02-28-2012, 07:47 PM
Ludovic Brenta
Guest
 
Posts: n/a
Default Re: Array Help?

Will writes on comp.lang.ada:
> Hello all,
>
> I am looking for your help again. I know how to refer to a certain
> index in an array. for example if some array A is (53,83,3,62,3,13)
> then A(4) = 62 but what if I wanted to go through an array and
> identify certain indexes by what they contain? In my particular case
> I need to identify the indexes that have numbers in them that do not
> end in 1 or 2. These numbers range from 1 to 4 digits. Any ideas on
> how I can do this?
>
> Thanks a bunch


Since this looks very much like a school assignment, my answer will be
in English rather than Ada

Traverse the array. Each element in the array is an integer; take that
integer modulo 10. If the integer modulo 10 is neither 1 nor 2 then
output the index of the element (or add that index to a vector of
indexes for further processing).

HTH

--
Ludovic Brenta.
Reply With Quote
  #3 (permalink)  
Old 02-28-2012, 07:50 PM
Gautier write-only
Guest
 
Posts: n/a
Default Re: Array Help?

Will <willmann...@gmail.com> wrote:

>... A(4) = 62

Just to avoid confusion, 4 is the index, 62 is the element.

>... if I wanted to go through an array

easy man: for index in A'range loop [your nice code] end loop;

>... numbers in them that do not end in 1 or 2.

no need to worry about how many digits number N has: (N mod 10) has
guaranteed one :-)

HTH
_________________________
Gautier's Ada programming
http://freecode.com/users/gdemont
Reply With Quote
  #4 (permalink)  
Old 02-28-2012, 08:33 PM
Simon Wright
Guest
 
Posts: n/a
Default Re: Array Help?

Will <willmann817@gmail.com> writes:

> Hello all,
>
> I am looking for your help again. I know how to refer to a certain
> index in an array. for example if some array A is (53,83,3,62,3,13)
> then A(4) = 62 but what if I wanted to go through an array and
> identify certain indexes by what they contain? In my particular case
> I need to identify the indexes that have numbers in them that do not
> end in 1 or 2. These numbers range from 1 to 4 digits. Any ideas on
> how I can do this?


I presume A is an array of Integer, and that you're interested in the
decimal representation (remember, modern computers run in binary so
in a very real sense numbers have to "end in" 0 or 1!)

for J in A'Range loop
declare
Decimal_Units : constant Natural := abs (A (J) mod 10);
begin
if Decimal_Units /= 1 and Decimal_Units /= 2 then
-- A (J) doesn't end in 1 or 2.
end if;
end;
end loop;
Reply With Quote
  #5 (permalink)  
Old 02-28-2012, 09:11 PM
Simon Wright
Guest
 
Posts: n/a
Default Re: Array Help?

Ludovic Brenta <ludovic@ludovic-brenta.org> writes:

> Since this looks very much like a school assignment, my answer will be
> in English rather than Ada


I think you're probably right. Damn.
Reply With Quote
  #6 (permalink)  
Old 02-28-2012, 10:09 PM
Will
Guest
 
Posts: n/a
Default Re: Array Help?

Thanks guys... I appreciate your replies. For future reference all
you had to say was something along these lines. "Here's a hint, if
you divide each number by 10 what remainder do you get?" I am a
student and working on a more complex problem than just returning
numbers, I just needed to know how to do this in order to continue the
problem. I do not want to be spoon fed, I want to learn and yes I
probably could have phrased my question in a better fashion. But I do
understand basic Ada. I am just not a great problem solver yet.
Thanks again.

Will
Reply With Quote
  #7 (permalink)  
Old 02-28-2012, 11:22 PM
Adam Beneschan
Guest
 
Posts: n/a
Default Re: Array Help?

On Feb 28, 12:15*pm, Will <willmann...@gmail.com> wrote:
> Hello all,
>
> I am looking for your help again. *I know how to refer to a certain
> index in an array. for example if some array A is (53,83,3,62,3,13)
> then A(4) = 62


FYI, this is not necessarily true. In Ada, the starting and ending
indexes of an array can be anything you want. You can declare your
array like this:

A : array (2 .. 7) of Integer;

or, since I don't like anonymous array types,

type Integer_Array is array (Integer range <>) of Integer;
A : Integer_Array (2 .. 7);

Now, A(4) = 3, not 62.

You may be aware of this already, but I wanted to point it out in case
you weren't.

-- Adam
Reply With Quote
  #8 (permalink)  
Old 02-28-2012, 11:24 PM
Adam Beneschan
Guest
 
Posts: n/a
Default Re: Array Help?

On Feb 28, 12:47*pm, Ludovic Brenta <ludo...@ludovic-brenta.org>
wrote:
> Will writes on comp.lang.ada:
>
> > Hello all,

>
> > I am looking for your help again. *I know how to refer to a certain
> > index in an array. for example if some array A is (53,83,3,62,3,13)
> > then A(4) = 62 * but what if I wanted to go through an array and
> > identify certain indexes by what they contain? *In my particular case
> > I need to identify the indexes that have numbers in them that do not
> > end in 1 or 2. * These numbers range from 1 to 4 digits. *Any ideason
> > how I can do this?

>
> > Thanks a bunch

>
> Since this looks very much like a school assignment, my answer will be
> in English rather than Ada
>
> Traverse the array. *Each element in the array is an integer; take that
> integer modulo 10.


That'll work if each element of the array is a NATURAL or a POSITIVE.
If it's possible for A to contain any negative integers, then a more
careful statement of the problem is needed (does "ends in 1 or 2" mean
what it looks like?), and some extra thought is needed to get that
case right.

-- Adam
Reply With Quote
  #9 (permalink)  
Old 02-28-2012, 11:27 PM
Adam Beneschan
Guest
 
Posts: n/a
Default Re: Array Help?

On Feb 28, 1:33*pm, Simon Wright <si...@pushface.org> wrote:
> Will <willmann...@gmail.com> writes:
> > Hello all,

>
> > I am looking for your help again. *I know how to refer to a certain
> > index in an array. for example if some array A is (53,83,3,62,3,13)
> > then A(4) = 62 * but what if I wanted to go through an array and
> > identify certain indexes by what they contain? *In my particular case
> > I need to identify the indexes that have numbers in them that do not
> > end in 1 or 2. * These numbers range from 1 to 4 digits. *Any ideason
> > how I can do this?

>
> I presume A is an array of Integer, and that you're interested in the
> decimal representation (remember, modern computers run in binary so
> in a very real sense numbers have to "end in" 0 or 1!)
>
> * *for J in A'Range loop
> * * * declare
> * * * * *Decimal_Units : constant Natural := abs (A (J) mod 10);


If this is a school assignment, then F for you. OK, B-, since you
almost got it right, so I'll give you part credit.

> * * * begin
> * * * * *if Decimal_Units /= 1 and Decimal_Units /= 2 then
> * * * * * * -- *A (J) doesn't end in 1 or 2.
> * * * * *end if;
> * * * end;
> * *end loop;


-- Adam
Reply With Quote
  #10 (permalink)  
Old 02-29-2012, 07:00 AM
Simon Wright
Guest
 
Posts: n/a
Default Re: Array Help?

Adam Beneschan <adam@irvine.com> writes:

> On Feb 28, 1:33Â*pm, Simon Wright <si...@pushface.org> wrote:


>> Â* Â*for J in A'Range loop
>> Â* Â* Â* declare
>> Â* Â* Â* Â* Â*Decimal_Units : constant Natural := abs (A (J) mod 10);

>
> If this is a school assignment, then F for you. OK, B-, since you
> almost got it right, so I'll give you part credit.


:blush:

All these years and I still only know enough to realise there's a
problem and go off to the ARM to check. I certainly haven't had to solve
a problem like this IRL often enough for it to sink in.
Reply With Quote
  #11 (permalink)  
Old 02-29-2012, 07:48 AM
Simon Wright
Guest
 
Posts: n/a
Default Re: Array Help?

Simon Wright <simon@pushface.org> writes:

> All these years and I still only know enough to realise there's a
> problem and go off to the ARM to check.


.... but not enough to write a test case to make sure I've actually
understood it.
Reply With Quote
  #12 (permalink)  
Old 02-29-2012, 03:09 PM
Robert A Duff
Guest
 
Posts: n/a
Default Re: Array Help?

Adam Beneschan <adam@irvine.com> writes:

> type Integer_Array is array (Integer range <>) of Integer;
> A : Integer_Array (2 .. 7);


Right, and this is a rich source of bugs. You usually want
arrays to start at 1, or sometimes 0 (assuming the index type
is a signed integer type, which is almost always the case
for unconstrained arrays). In Ada 2012, you can say:

type Integer_Array is array (Positive range <>) of Integer
with Dynamic_Predicate => Integer_Array'First = 1;

or:

type Integer_Array is array (Natural range <>) of Integer
with Dynamic_Predicate => Integer_Array'First = 0;

Now 'Last can be anything you like, but 'First is fixed.

In GNAT, you can write "Predicate" instead of "Dynamic_Predicate".

- Bob
Reply With Quote
  #13 (permalink)  
Old 02-29-2012, 03:50 PM
Ludovic Brenta
Guest
 
Posts: n/a
Default Re: Array Help?

Robert A Duff wrote on comp.lang.ada:
> You usually want arrays to start at 1, or sometimes 0 (assuming
> the index type is a signed integer type, which is almost always
> the case for unconstrained arrays). In Ada 2012, you can say:
>
> type Integer_Array is array (Positive range <>) of Integer
> with Dynamic_Predicate => Integer_Array'First = 1;
>
> or:
>
> type Integer_Array is array (Natural range <>) of Integer
> with Dynamic_Predicate => Integer_Array'First = 0;
>
> Now 'Last can be anything you like, but 'First is fixed.


Doesn't that preclude slices that don't start at 'First?

Supposing your declarations, can you call

procedure Foo (Param : in out Integer_Array);

like this:

declare
A : Integer_Array (1 .. 10) := (others => 0);
begin
Foo (A (3 .. 8));
end;

?

--
Ludovic Brenta.
Reply With Quote
  #14 (permalink)  
Old 02-29-2012, 05:24 PM
Robert A Duff
Guest
 
Posts: n/a
Default Re: Array Help?

Ludovic Brenta <ludovic@ludovic-brenta.org> writes:

> Doesn't that preclude slices that don't start at 'First?


Yes.

> Supposing your declarations, can you call
>
> procedure Foo (Param : in out Integer_Array);
>
> like this:
>
> declare
> A : Integer_Array (1 .. 10) := (others => 0);
> begin
> Foo (A (3 .. 8));
> end;
>
> ?


That will raise C_E. IMHO, that's a language design flaw -- inside
Foo, Param'First ought to be 1. The fact that Foo can see that
it came from a slice is a leak of abstraction.

The fact that arrays slide in many situations is proof that
people don't really care too much about the bounds -- they
care about the length.

Anyway, slices aren't really all that useful -- for many array types,
you don't need them at all. And slices as l-values, as in your
example, are quite rare, because you really want to be able to
change the length of the slice, if you want to change it at all.
The following won't work:

X : String := "Hello, world.";

X(1..5) := "Goodbye"; -- raises C_E

- Bob
Reply With Quote
  #15 (permalink)  
Old 02-29-2012, 05:35 PM
Jeffrey Carter
Guest
 
Posts: n/a
Default Re: Array Help?

On 02/29/2012 09:09 AM, Robert A Duff wrote:
>
> Right, and this is a rich source of bugs. You usually want
> arrays to start at 1, or sometimes 0 (assuming the index type
> is a signed integer type, which is almost always the case
> for unconstrained arrays).


My 1st programming job, using FORTRAN 66, dealt with tree-ring data. We had data
from 1600 to 1970. Calculating the correct array index for a year was a rich
source of bugs for us; if we'd been able to have the lower bounds be 1600 rather
than 1 life would have been a lot easier.

--
Jeff Carter
"Sir Robin the not-quite-so-brave-as-Sir-Lancelot,
who had nearly fought the Dragon of Angnor,
who nearly stood up to the vicious Chicken of Bristol,
and who had personally wet himself at the
Battle of Badon Hill."
Monty Python & the Holy Grail
68

--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 03:23 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.