Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- int binarnotrazi(float *polje, int n, float x){
- int donji=0, srednji, gornji=n-1;
- while(donji<=gornji){
- srednji=(donji+gornji)/2;
- if(polje[srednji]<x)
- donji=srednji+1;
- else if(polje[srednji]>x)
- gornji=srednji-1;
- else
- return srednji;
- }
- return -1;
- }
- int main(void){
- float *poljeA, x;
- int n, i, indeks;
- printf("Ucitajte broj elemenata polja> ");
- scanf("%d", &n);
- poljeA=malloc(n*sizeof(int));
- for(i=0; i<n; i++){
- poljeA[i]=i*1.1;
- }
- for(i=0; i<n; i++){
- printf("%3f", poljeA[i]);
- }
- printf("Ucitajte trazeni element> ");
- scanf("%f", &x);
- indeks=binarnotrazi(&poljeA[0], n, x);
- free(poljeA);
- if(indeks<0){
- printf("Broj se ne nalazi u polju.");
- } else {
- printf("Vrijednost %f pronađena je na poziciji %d.", x, indeks);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement