From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: *** X-Spam-Status: No, score=3.8 required=5.0 tests=BAYES_00,INVALID_MSGID, RATWARE_MS_HASH,RATWARE_OUTLOOK_NONAME,WEIRD_QUOTING autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,5d3a1501d97dab65 X-Google-Attributes: gid103376,public From: "David C. Hoos, Sr." Subject: Re: C to Ada : a piece of code Date: 1996/09/07 Message-ID: <01bb9cf7$0e317cc0$488371a5@dhoossr.iquest.com>#1/1 X-Deja-AN: 179123507 distribution: world references: <3231732C.2781@virgoa4.in2p3.fr> content-type: text/plain; charset=ISO-8859-1 organization: DBH Enterprises, Inc. mime-version: 1.0 newsgroups: comp.lang.ada Date: 1996-09-07T00:00:00+00:00 List-Id: Hi, Grave Xavier 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 {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 ---------