home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ukma!gatech!destroyer!cs.ubc.ca!unixg.ubc.ca!erich.triumf.ca!ivan
- From: ivan@erich.triumf.ca (Ivan D. Reid)
- Newsgroups: comp.programming
- Subject: Re: is it possible to find max & next smallest in one pass?
- Date: 24 Jan 1993 07:29 PST
- Organization: TRIUMF: Tri-University Meson Facility
- Lines: 41
- Distribution: world
- Message-ID: <24JAN199307290872@erich.triumf.ca>
- References: <1993Jan24.040702.6193@emr1.emr.ca> <51915@seismo.CSS.GOV>
- NNTP-Posting-Host: erich.triumf.ca
- News-Software: VAX/VMS VNEWS 1.41
-
- In article <51915@seismo.CSS.GOV>, black@seismo.CSS.GOV (Mike Black) writes...
- ]Seems to me that life is simpler than everyone makes it :-)
-
- ]Why compare BOTH numbers?? If we find a new max, then the old max is
- ]obviously the next highest, isn't it?? The same would follow for however
- ]many maxes you wish to find. No more than one comparison necessary.
- Unfortunately, no. See comments below.
-
-
- ]/* Finds the maximum and next-maximum of of first 1000 pseudo-random numbers.
- ] * We'll divide by 1000.0 just to get a floating point number.
- ] * Add a similar primer and one more if-check and you can find the min
- ] * and next-min too.
- ] */
-
- ]main()
- ]{
- ] float value, max1, max2, tmp;
- ] int i;
- ] max1 = random() / 1000.0; /* Prime the pump */
- ] max2 = random() / 1000.0;
- ] if (max1 > max2) { /* Swap the first two if necessary */
- ] tmp = max1;
- ] max1 = max2;
- ] max2 = tmp;
- ] }
- ] for (i = 2; i < 1000; i++) {
- ] value = random() / 1000.0;
- ] ivalue(value > max1) { /* New max and demote old one */
-
- Presume you mean "if (value > max1)..."
- But what happens if max1>value>max2? :-(
-
- ] max2 = max1;
- ] max1 = value;
- ] }
- ] }
- ] printf("%g %g\n", max1, max2);
- ]}
-
- Ivan Reid, Paul Scherrer Institute, CH. ivan@cvax.psi.ch
-