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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,703c4f68db81387d X-Google-Thread: 109fba,703c4f68db81387d X-Google-Attributes: gid103376,gid109fba,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.megapath.net!news.megapath.net.POSTED!not-for-mail NNTP-Posting-Date: Thu, 10 Mar 2005 17:23:13 -0600 From: "Randy Brukardt" Newsgroups: comp.lang.ada,comp.lang.c++ 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> <4%PXd.358789$w62.304065@bgtnsc05-news.ops.worldnet.att.net> Subject: Re: Teaching new tricks to an old dog (C++ -->Ada) Date: Thu, 10 Mar 2005 17:25:24 -0600 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4927.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4927.1200 Message-ID: NNTP-Posting-Host: 64.32.209.38 X-Trace: sv3-HPj7Zb/q9fxgX5FgRxEDTm4XjKgBbGZEEXEc/tCmsnQyhIg+L99zpVKEYZ6UfCWuTCdDbegq3YHIXHM!6qc1+CLCGqEAPHIQTfdk8aibXp6D5ArLD1BqcW2s82limb5CZgJ84JEdCIuXUrS2nMgygFPej9dr X-Complaints-To: abuse@megapath.net X-DMCA-Complaints-To: abuse@megapath.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: g2news1.google.com comp.lang.ada:9074 comp.lang.c++:45021 Date: 2005-03-10T17:25:24-06:00 List-Id: "Jim Rogers" wrote in message news:4%PXd.358789$w62.304065@bgtnsc05-news.ops.worldnet.att.net... Jim Rogers wrote: I would have done this program somewhat differently. > 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; Much better to use an aggregate here: Vec := (others => My_Rand.Random(Seed)); The random function will get called once for each component. Let the compiler write the loop; it can't do a worse job, and it might actually do better. > -- 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; I generally stick a test for 'Last into the loop. But that's more because I'm lazy. -- finds first index where a value is less than 1000 for I in Vec'range loop if Vec(I) < 1000 then Ada.Text_Io.Put_Line("Number < 1000 found at index" & Natural'Image(I)); exit; elsif I = Vec'Last then Ada.Text_Io.Put_Line("No number < 1000 was found"); exit; end if; end loop; But in this case, you really ought to not use a for loop at all: declare I : Index_Type'Base := Vec'First; begin while I <= Vec'Last loop if Vec(I) < 1000 then Ada.Text_Io.Put_Line("Number < 1000 found at index" & Natural'Image(I)); exit; end if; I := I + 1; end loop; if I <= Vec'Last then Ada.Text_Io.Put_Line("Number < 1000 found at index" & Natural'Image(I)); else Ada.Text_Io.Put_Line("No number < 1000 was found"); end if; end; But of course this is a bit more prone to errors. The best plan is to use a container function that does the searching, so you don't have to. That's probably more on this topic than anyone wanted to know. :-) > end Random_Fill; Randy Brukardt