#include <stdio.h>
#define MAX 1000

int busca_binaria(int valores[], int n, int chave){
    int inicio = 0;
    int fim = n - 1;

    while(inicio != fim){
        int meio = (inicio + fim) / 2;
        
        if(chave <= valores[meio]){
            fim = meio;
        } else{
            inicio = meio + 1;
        }
    }

    if(valores[inicio] == chave){
        return inicio;
    } else{
        return 0;
    }
}

int le_vetor(int v[], int n){
    for(int i = 0; i < n; i++){
        scanf("%d", &v[i]);
    }
}

int main() {
    int v[MAX], n, chave;
    scanf("%d", &n);

    le_vetor(v, n);
    scanf("%d ", &chave);
    
    printf("%d", busca_binaria(v, n, chave));
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: