Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // uloha-4-1.c -- Tyzden 4 - Uloha 1
- // Lukas Belaj, 15.10.2014 14:12:20
- #include <stdio.h>
- // vrati index cisla x vo vzostupne usporiadanej postupnosti cisel. Vrati -1 ak sa x v postupnosti nenachadza.
- int search(int cisla[], int n, int x)
- {
- // sem napis svoje riesenie
- int mid, first, last;
- //n velkost pola;
- first=0;
- last=n-1;
- mid=(first+last)/2;
- while( first <= last )
- {
- if ( cisla[mid] < x )
- first = mid + 1;
- else if ( cisla[mid] == x )
- {
- return(mid);
- }
- else
- last = mid - 1;
- mid = (first + last)/2;
- }
- if ( first > last )
- return (-1);
- }
- // ukazkovy test
- int main(void)
- {
- int a[] = {10, 20, 30,40,50,60,70};
- printf("Cislo 30 je na pozicii %d\n", search(a, 7, 3000000));
- printf("Cislo 25 je na pozicii %d\n", search(a, 7, -29525));
- printf("Cislo 20 je na pozicii %d\n", search(a, 7, 0));
- printf("Cislo 20 je na pozicii %d\n", search(a, 7, 70));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement