home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / programm / 3570 < prev    next >
Encoding:
Text File  |  1993-01-24  |  1.7 KB  |  55 lines

  1. Path: sparky!uunet!ukma!gatech!destroyer!cs.ubc.ca!unixg.ubc.ca!erich.triumf.ca!ivan
  2. From: ivan@erich.triumf.ca (Ivan D. Reid)
  3. Newsgroups: comp.programming
  4. Subject: Re: is it possible to find max & next smallest in one pass?
  5. Date: 24 Jan 1993 07:29 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Lines: 41
  8. Distribution: world
  9. Message-ID: <24JAN199307290872@erich.triumf.ca>
  10. References: <1993Jan24.040702.6193@emr1.emr.ca> <51915@seismo.CSS.GOV>
  11. NNTP-Posting-Host: erich.triumf.ca
  12. News-Software: VAX/VMS VNEWS 1.41    
  13.  
  14. In article <51915@seismo.CSS.GOV>, black@seismo.CSS.GOV (Mike Black) writes...
  15. ]Seems to me that life is simpler than everyone makes it :-)
  16.  
  17. ]Why compare BOTH numbers??  If we find a new max, then the old max is
  18. ]obviously the next highest, isn't it?? The same would follow for however
  19. ]many maxes you wish to find.  No more than one comparison necessary.
  20.     Unfortunately, no.  See comments below.
  21.  
  22.  
  23. ]/* Finds the maximum and next-maximum of of first 1000 pseudo-random numbers. 
  24. ] * We'll divide by 1000.0 just to get a floating point number. 
  25. ] * Add a similar primer and one more if-check and you can find the min 
  26. ] *   and next-min too. 
  27. ] */
  28.  
  29. ]main()
  30. ]{
  31. ]    float value, max1, max2, tmp;
  32. ]    int i;
  33. ]    max1 = random() / 1000.0; /* Prime the pump */
  34. ]    max2 = random() / 1000.0;
  35. ]    if (max1 > max2) { /* Swap the first two if necessary */
  36. ]        tmp = max1;
  37. ]        max1 = max2;
  38. ]        max2 = tmp;
  39. ]    }
  40. ]    for (i = 2; i < 1000; i++) {
  41. ]        value = random() / 1000.0;
  42. ]        ivalue(value > max1) { /* New max and demote old one */
  43.  
  44.     Presume you mean "if (value > max1)..."
  45.     But what happens if max1>value>max2?  :-(
  46.  
  47. ]            max2 = max1;
  48. ]            max1 = value;
  49. ]        }
  50. ]    }
  51. ]    printf("%g %g\n", max1, max2);
  52. ]}
  53.  
  54. Ivan Reid, Paul Scherrer Institute, CH.                 ivan@cvax.psi.ch
  55.