|
|||
|
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 |
|
|
||||
|
||||
|
|
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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; |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 youalmost 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 |
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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 --- |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|