Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. // uloha-4-1.c -- Tyzden 4 - Uloha 1
  2. // Lukas Belaj, 15.10.2014 14:12:20
  3.  
  4. #include <stdio.h>
  5.  
  6. // vrati index cisla x vo vzostupne usporiadanej postupnosti cisel. Vrati -1 ak sa x v postupnosti nenachadza.
  7. int search(int cisla[], int n, int x)
  8. {
  9.   // sem napis svoje riesenie
  10.   int mid, first, last;
  11.  
  12.   //n velkost pola;
  13.   first=0;
  14.   last=n-1;
  15.   mid=(first+last)/2;
  16.  
  17.   while( first <= last )
  18.   {
  19.       if ( cisla[mid] < x )
  20.          first = mid + 1;    
  21.       else if ( cisla[mid] == x )
  22.       {
  23.          return(mid);
  24.       }
  25.       else
  26.         last = mid - 1;
  27.         mid = (first + last)/2;
  28.    }
  29.    if ( first > last )
  30.       return (-1);
  31. }
  32.  
  33. // ukazkovy test
  34. int main(void)
  35. {
  36.   int a[] = {10, 20, 30,40,50,60,70};
  37.  
  38.   printf("Cislo 30 je na pozicii %d\n", search(a, 7, 3000000));
  39.   printf("Cislo 25 je na pozicii %d\n", search(a, 7, -29525));
  40.   printf("Cislo 20 je na pozicii %d\n", search(a, 7, 0));
  41.   printf("Cislo 20 je na pozicii %d\n", search(a, 7, 70));
  42.  
  43.   return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement