comp.lang.ada
 help / color / mirror / Atom feed
From: Jacob Sparre Andersen <sparre@nbi.dk>
Subject: Re: basic questions on using Ada arrays
Date: Wed, 06 Oct 2010 18:22:18 +0200
Date: 2010-10-06T18:22:18+02:00	[thread overview]
Message-ID: <871v838f5x.fsf@hugsarin.sparre-andersen.dk> (raw)
In-Reply-To: i8i5ga$ejc$1@speranza.aioe.org

Nasser M. Abbasi wrote:

> I am really rusty with Ada. Wanted to find if I apply a function to an
> array in one call without having to loop calling the function for each
> entry in the array?

Only if the function/operation is declared for the array type.

The operator "and" is automatically declared for arrays of Booleans.

> Suppose I have an array of values, and I want to take the sin() of
> each value in the array?

The function "Sin" is not declared automatically for arrays of Floats.
You will have to declare it yourself:

   function Sin (Item : in The_Array_Type) return The_Array_Type is
   begin
      return Result : The_Array_Type do
         for I in Item'Range loop
            Result (I) := Sin (Item (I));
         end loop;
      end return;
   end Sin;      

> But I wanted to make each entry in the array to have some value
> depending on the index value.

You have to use a loop to do that (you may of course hide the loop in a
function).

> The bigger question really, is if one map functions on array in Ada,
> or do operations on array in one call, say like multiply a constant by
> array, without using loops all the time. loops are so out of fashion
> these days :)

There are generally no predefined mathematical operations on arrays of
scalars in Ada.  You could propose it to the ARG.  They might even
accept it.

> Something like this in Fortran
>
> ----------------------
> program main
>    integer :: x(10)=1 ;
>    x = 5 * x;
> end program
> -----------------------
>
> compiles, runs ok, but The Ada code
>
> ----------------------
> procedure test3  is
>    x   : array(1..10) of integer :=(others=>1);
> begin
>    x:= 5 * x;
> end test3;
> ---------------

... should be:

   procedure Test_3 is
      type Demo_Array is array (1 .. 10) of Integer;

      function "*" (Left : Integer; Right : Demo_Array)
        return Demo_Array is
      begin
         return Result : Demo_Array do
            for I in Right'Range loop
               Result (I) := Left * Right (I);
            end loop;
         end return;
      end "*";

      X : Demo_Array := (others => 1);
   begin
      X := 5 * X;
   end Test_3;

Jacob
-- 
Jacob Sparre Andersen Research & Innovation
Vesterbrogade 148K, 1. th.
1620 K�benhavn V
Danmark



  parent reply	other threads:[~2010-10-06 16:22 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-06 15:43 basic questions on using Ada arrays Nasser M. Abbasi
2010-10-06 16:17 ` Pascal Obry
2010-10-06 16:22 ` Jacob Sparre Andersen [this message]
2010-10-07  1:55   ` Keith Thompson
2010-10-08  0:04     ` Randy Brukardt
2010-10-08 14:47       ` Jacob Sparre Andersen
2010-10-09  6:35         ` Randy Brukardt
2010-10-26  2:20           ` Yannick Duchêne (Hibou57)
2010-10-06 17:03 ` Jeffrey Carter
2010-10-06 19:54 ` Simon Wright
2010-10-08  8:02 ` Alex Mentis
replies disabled

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