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=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,703c4f68db81387d X-Google-Thread: 109fba,703c4f68db81387d X-Google-Thread: 115aec,703c4f68db81387d X-Google-Thread: f43e6,703c4f68db81387d X-Google-Attributes: gid103376,gid109fba,gid115aec,gidf43e6,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news.glorb.com!wn13feed!worldnet.att.net!bgtnsc05-news.ops.worldnet.att.net.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.ada,comp.lang.c++,comp.realtime,comp.software-eng Subject: Re: Teaching new tricks to an old dog (C++ -->Ada) From: Jim Rogers References: <4229bad9$0$1019$afc38c87@news.optusnet.com.au> <1110032222.447846.167060@g14g2000cwa.googlegroups.com> <871xau9nlh.fsf@insalien.org> <3SjWd.103128$Vf.3969241@news000.worldonline.dk> <87r7iu85lf.fsf@insalien.org> <1110052142.832650@athnrd02> <1110284070.410136.205090@o13g2000cwo.googlegroups.com> <395uqaF5rhu2mU1@individual.net> <1110377260.350158.58730@z14g2000cwz.googlegroups.com> <422f0c08$1_1@baen1673807.greenlnk.net> <1110421853.766292@athnrd02> Followup-To: comp.lang.ada,comp.lang.c++ User-Agent: Xnews/5.04.25 Message-ID: <4%PXd.358789$w62.304065@bgtnsc05-news.ops.worldnet.att.net> Date: Thu, 10 Mar 2005 04:25:36 GMT NNTP-Posting-Host: 12.73.184.16 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc05-news.ops.worldnet.att.net 1110428736 12.73.184.16 (Thu, 10 Mar 2005 04:25:36 GMT) NNTP-Posting-Date: Thu, 10 Mar 2005 04:25:36 GMT Organization: AT&T Worldnet Xref: g2news1.google.com comp.lang.ada:9005 comp.lang.c++:44902 comp.realtime:1181 comp.software-eng:4744 Date: 2005-03-10T04:25:36+00:00 List-Id: Ioannis Vranos wrote in news:1110421853.766292 @athnrd02: >>>This is safer, but limiting. >>>In C++ may want to declare the variable outside the loop, >>>break out early and use the loop variable. Let me guess: You can't >>>break out early in Ada, right? >> >> >> Of course you can! > > > Can this be done in Ada? > > > #include > #include > #include > #include > > > int main() > { > using namespace std; > > vector vec(1000); > > // Seeds the random number generator > srand(time(0)); > > // Use rand() to fill vector with values > // As you see the operation is entirely safe. > generate(vec.begin(), vec.end(), rand); > > vector::size_type i; > > // Finds the first index where a value is smaller than 1000 > // in low level style > for(i=0; i if(vec[i]<1000) > break; > > i==vec.size()? cout<<"No number <1000 was found\n" > :cout<<"Number <1000 found at index "< } The idioms are somewhat different. For instance, the Ada for loop does not iterate one past the end of the array, nor is the value of the loop control variable visible outside the loop. I did not see where you set the range of values for your random number type. I am generating integer values in the range of 0 through 10,000. with Ada.Text_Io; with Ada.Numerics.Discrete_Random; procedure Random_Fill is type Rand_Values is range 0..10_000; package My_Rand is new Ada.Numerics.Discrete_Random(Rand_Values); Seed : My_Rand.Generator; subtype Index_Type is Natural range 1..1000; Vec : array(Index_Type) of Rand_Values; Found : Natural := 0; begin My_Rand.Reset(Seed); -- seeds the random number generator -- fill the array with random values in the range of 0 -- through 10,000 for I in Vec'range loop Vec(I) := My_Rand.Random(Seed); end loop; -- finds first index where a value is less than 1000 for I in Vec'range loop if Vec(I) < 1000 then Found := I; exit; end if; end loop; if Found > 0 then Ada.Text_Io.Put_Line("Number < 1000 found at index" & Natural'Image(Found)); else Ada.Text_Io.Put_Line("No number < 1000 was found"); end if; end Random_Fill; Jim Rogers