comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
Subject: Re: C to Ada : a piece of code
Date: 1996/09/07
Date: 1996-09-07T00:00:00+00:00	[thread overview]
Message-ID: <01bb9cf7$0e317cc0$488371a5@dhoossr.iquest.com> (raw)
In-Reply-To: 3231732C.2781@virgoa4.in2p3.fr


Hi,
Grave Xavier <xavier@virgoa4.in2p3.fr> wrote in article
<3231732C.2781@virgoa4.in2p3.fr>...
> Hi,
> 
> Can somebody explain me a way to translate this C code to
> Ada ? I just want to avoid using chained list for a vector
> of double.
> 
> -------------------------
> double *vect;
> long size,i;
> 
> scanf("%ld",&size);
> vect = (double *) calloc(size,sizeof(double));
> for(i=0;i<size;i++)
>    {vect[i] = ....;
>    }
> -------------------------
> 
> Thanks.
> 
> -- 
> xavier@virgoa4.in2p3.fr
> 
> De chacun selon ses forces, a chacun selon ses besoins.
> 

The file at the end of this message does waht you ask.  It is, however not
a faithful translation in one respect.  The C function scanf is notorious
for failing with invalid inputs.  The ada code shown here is tolerant of
user input errors, which the scanf code is not.

Hope this illustrates a few other useful Ada features, in addition to your
specific request.
-- 
David C. Hoos, Sr.,
http://www.dbhwww.com
http://www.ada95.com

------ File xavier.adb begins ---------

with Text_IO;
procedure Xavier is
  Standard_Input    : Text_IO.FILE_TYPE;
  Temporary_String  : STRING (1 .. 256);
  The_Line_Length   : NATURAL;
  The_Vector_Length : NATURAL;
  type FLOAT_VECTOR_TYPE is array (INTEGER range <>) of LONG_FLOAT;
  type FLOAT_VECTOR_ACCESS_TYPE is access FLOAT_VECTOR_TYPE;
  The_Vector_Access : FLOAT_VECTOR_ACCESS_TYPE;
  package Long_Float_IO is new Text_IO.Float_IO (LONG_FLOAT);
begin
  loop
    Text_IO.Get_Line (
	  Item => Temporary_String,
        Last => The_Line_Length
        );
    begin
      if The_Line_Length > 0 then
        The_Vector_Length := NATURAL'VALUE (
            Temporary_String (1 .. The_Line_Length)
            );
      else
        The_Vector_Length := 0;
      end if;
      exit;
    exception
      when Constraint_Error | Numeric_Error =>
        Text_IO.Put_Line (
            """" &
            Temporary_String (1 .. The_Line_Length) &
            """ is not a valid integer value representation.  Re-enter"
            );
    end;
  end loop;
  The_Vector_Access := new FLOAT_VECTOR_TYPE' (
      (0 .. The_Vector_Length - 1 => 0.0)
      );
  for i in The_Vector_Access'RANGE loop
    The_Vector_Access (i) := LONG_FLOAT (i);
  end loop;

  for i in The_Vector_Access'RANGE loop
    Text_IO.Put (
        "The_Vector (" &
        INTEGER'IMAGE (i) &
        " ) => "
        ); 
        Long_Float_IO.Put (The_Vector_Access (i));
    Text_IO.New_Line;
    if i mod 5 = 4 then
      Text_IO.New_Line;
    end if;
  end loop;
end Xavier;

------ File xavier.adb ends ---------





  parent reply	other threads:[~1996-09-07  0:00 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-09-07  0:00 C to Ada : a piece of code Grave Xavier
1996-09-07  0:00 ` Robert A Duff
1996-09-07  0:00 ` David C. Hoos, Sr. [this message]
1996-09-08  0:00 ` Jon S Anthony
1996-09-08  0:00   ` David C. Hoos, Sr.
1996-09-09  0:00     ` nasser
1996-09-09  0:00   ` Jon S Anthony
1996-09-09  0:00     ` David C. Hoos, Sr.
1996-09-09  0:00       ` John G. Volan
1996-09-09  0:00       ` Robert Dewar
1996-09-10  0:00         ` Geert Bosch
1996-09-11  0:00           ` Robert Dewar
1996-09-11  0:00             ` Jonas Nygren
1996-09-13  0:00             ` Geert Bosch
1996-09-14  0:00               ` Robert Dewar
1996-09-11  0:00           ` Robert Dewar
1996-09-13  0:00             ` Geert Bosch
1996-09-14  0:00               ` Robert Dewar
1996-09-10  0:00 ` Jon S Anthony
replies disabled

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