comp.lang.ada
 help / color / mirror / Atom feed
* basic questions on using Ada arrays
@ 2010-10-06 15:43 Nasser M. Abbasi
  2010-10-06 16:17 ` Pascal Obry
                   ` (4 more replies)
  0 siblings, 5 replies; 11+ messages in thread
From: Nasser M. Abbasi @ 2010-10-06 15:43 UTC (permalink / raw)


Ada experts:

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?

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

This below does not work, last statement is wrong

---------------------------------------
with ada.text_io; use ada.text_io;
with Ada.Numerics; use  Ada.Numerics;
with Ada.Numerics.Elementary_Functions;
use  Ada.Numerics.Elementary_Functions;
with Ada.float_Text_IO; use Ada.float_Text_IO;

procedure test2  is
    nPoints : constant :=100;
    del : float := 2.0*Pi/float(nPoints-1);
    x   : array(1..nPoints) of float;
    y   : array(1..nPoints) of float;
begin

    x(1):=0.0;
    for i in 2..nPoints loop
       x(i)  := x(i-1)+del;
    end loop;

    x:=sin(x(1..nPoints));

end test2;
------------------------------------

Also, is there a way to initialize an array using a formula or a 
function call? I know I can do

  x   : array(1..nPoints) of float := (others => 0.0)

But I wanted to make each entry in the array to have some value 
depending on the index value.  I did not know how, so that is what the 
above small loop does, to initialize x array.

It would be nice to have been to do this initialization at declaration time.

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 :)

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;
---------------

$ gnatmake test3.adb
test3.adb:4:12: expected type universal integer
test3.adb:4:12: found type of x declared at line 2

I undertand exactly the error and why. My question is how to code it in 
Ada to the same, without using loops?

thanks,
--Nasser





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

end of thread, other threads:[~2010-10-26  2:20 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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