Guest User

Rozpacz 2

a guest
Dec 29th, 2011
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. /* rozpacz2.c */
  2. #include <stdio.h>
  3.  
  4. void minmax(double tab[], int n) {
  5.     double min = tab[0];
  6.     double max = tab[0];
  7.     int i;
  8.     int counter = 0;
  9.    
  10.    
  11.     /* Petla wykonuje sie [(n-1)/2] razy, za kazdym razem dokonujac  */
  12.     /* trzech porownan. Zatem calkowity koszt petli to: 3*[(n-1)/2]. */
  13.     for (i=1; i+1<n; i+=2)
  14.         if (tab[i] < tab[i+1]) {
  15.             if (tab[i] < min) min = tab[i];
  16.             if (max < tab[i+1]) max = tab[i+1];
  17.             counter += 3;
  18.         } else {
  19.             if (tab[i+1] < min) min = tab[i+1];
  20.             if (max < tab[i]) max = tab[i];
  21.             counter += 3;
  22.         }
  23.    
  24.     /* Jesli n - parzyste to mamy 1-2 porownania wiecej w zaleznosci */
  25.     /* od danych. Zakladamy najgorszy przypadek, zatem calkowity     */
  26.     /* maksymalny koszt to najwyzej: 3*[(n-1)/2] + 2.                */
  27.     if (i<n) {
  28.         counter++;
  29.         if (tab[i] < min) min = tab[i];
  30.         else {
  31.             counter++;
  32.             if (max < tab[i]) max = tab[i];
  33.         }
  34.     }
  35.    
  36.    
  37.     printf("Min = %g.\n", min);
  38.     printf("Max = %g.\n", max);
  39.     printf("n = %d.\n", n);
  40.     printf("Wykonano %d porownan.\n", counter);
  41. }
  42.  
  43.  
  44. int main(void) {
  45.     double tab[] = {1,2,3,4,5,6,7,8,9,0,99,88,77,66,55,44,33,22,11,22,3};
  46.     int dlugosc = sizeof(tab)/sizeof(*tab);
  47.     minmax(tab, dlugosc);
  48.    
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment