Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* rozpacz2.c */
- #include <stdio.h>
- void minmax(double tab[], int n) {
- double min = tab[0];
- double max = tab[0];
- int i;
- int counter = 0;
- /* Petla wykonuje sie [(n-1)/2] razy, za kazdym razem dokonujac */
- /* trzech porownan. Zatem calkowity koszt petli to: 3*[(n-1)/2]. */
- for (i=1; i+1<n; i+=2)
- if (tab[i] < tab[i+1]) {
- if (tab[i] < min) min = tab[i];
- if (max < tab[i+1]) max = tab[i+1];
- counter += 3;
- } else {
- if (tab[i+1] < min) min = tab[i+1];
- if (max < tab[i]) max = tab[i];
- counter += 3;
- }
- /* Jesli n - parzyste to mamy 1-2 porownania wiecej w zaleznosci */
- /* od danych. Zakladamy najgorszy przypadek, zatem calkowity */
- /* maksymalny koszt to najwyzej: 3*[(n-1)/2] + 2. */
- if (i<n) {
- counter++;
- if (tab[i] < min) min = tab[i];
- else {
- counter++;
- if (max < tab[i]) max = tab[i];
- }
- }
- printf("Min = %g.\n", min);
- printf("Max = %g.\n", max);
- printf("n = %d.\n", n);
- printf("Wykonano %d porownan.\n", counter);
- }
- int main(void) {
- double tab[] = {1,2,3,4,5,6,7,8,9,0,99,88,77,66,55,44,33,22,11,22,3};
- int dlugosc = sizeof(tab)/sizeof(*tab);
- minmax(tab, dlugosc);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment