home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1990 / 10 / duvaneko.asc < prev    next >
Text File  |  1990-08-16  |  2KB  |  73 lines

  1. _OPTIMAL DETERMINATION OF OBJECT EXTENTS_
  2. by by Victor J. Duvanenko, W.E. Robbins, and Ronald S. Gyurcsik
  3.  
  4.  
  5. [LISTIN╟ ONE]
  6.  
  7. /* min.c -- Procedure to find the smallest element of the array.
  8.    The number of elements in the array must be > 0.  ( n > 0 ) */
  9. float find_min( array, n )
  10. float array[];        /* input array */
  11. int n;                /* number of elements in the array ( n > 0 ) */
  12. {
  13.   register i;  float min;
  14.  
  15.   min = array[0];
  16.   for( i = 1; i < n; i++ )
  17.      if ( min > array[i] )   min = array[i];
  18.   return( min );
  19. }
  20. /* Procedure to find the largest element of the array.
  21.    The number of elements in the array must be > 0.  ( n > 0 ) */
  22. float find_max( array, n )
  23. float array[];        /* input array */
  24. int n;                /* number of elements in the array ( n > 0 ) */
  25. {
  26.   register i;  float max;
  27.  
  28.   max = array[0];
  29.   for( i = 1; i < n; i++ )
  30.      if ( max < array[i] )   max = array[i];
  31.   return( max );
  32. }
  33.  
  34.  
  35. [LISTING TWO]
  36.  
  37. /* min_max.c  -- Procedure to find the smallest and the largest element of 
  38.    the array. The number of elements in the array must be > 0.  ( n > 0 ) */
  39. void find_min_max( array, n, min, max )
  40. float array[];        /* input array */
  41. int n;                /* number of elements in the array ( n > 0 ) */
  42. float *min, *max;     /* pointers to the return values */
  43. {
  44.   register i;
  45.  
  46.   if ( n <= 1 )
  47.      *min = *max = array[0];
  48.   else {
  49.      if ( array[0] > array[1] ) {    /* establish the basis min and max */
  50.         *max = array[0];
  51.         *min = array[1];
  52.      }
  53.      else {
  54.         *max = array[1];
  55.         *min = array[0];
  56.      }
  57.      for( i = 2; ( i + 2 ) <= n; i += 2 )
  58.         if ( array[ i ] > array[ i + 1 ] ) {
  59.            if ( array[ i     ] > *max )   *max = array[ i ];
  60.            if ( array[ i + 1 ] < *min )   *min = array[ i + 1 ];
  61.         }
  62.         else {
  63.            if ( array[ i + 1 ] > *max )   *max = array[ i + 1 ];
  64.            if ( array[ i     ] < *min )   *min = array[ i ];
  65.         }
  66.      if ( i < n )        /* handle the odd/last array element */
  67.        if ( *max < array[i] )  *max = array[i];
  68.        else if ( *min > array[i] )  *min = array[i];
  69.   }
  70. }
  71.  
  72.  
  73.