Search code examples
cbubble-sort

Undefined reference to pesquisar_bin


//Ordenacao por insercao/selecao

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>


int vet[10]={8,16,34,13,19,7,45,3,12,9},a,b,aux;

void selecao();
int pesquisar_bin(int,int,int,int);

int main (){
    int opt=0,num,max;
    char continuar;

    printf("ESCOLHA O METODO DE ORDENACAO:\n\
    1- INSERCAO\n\
    2- SELECAO\n\
    3- SAIR\n");
    do{
        scanf("%d",&opt);
    }while(opt!=1 && opt!=2 && opt!=3);

    switch(opt){
        case 1:
            break;
        case 2:
            selecao();
            break;
        case 3:
            exit(1);
            break;
    }

    printf("\n\n1- PESQUISA SEQUENCIAL\n\n\
2- PESQUISA BINARIA\n");
    do{
        aux=0;
        scanf("%d",&aux);
    }while(aux!=1&&aux!=2);

    printf("DIGITE O VALOR A SER PESQUISADO:\n");
    scanf("%d",&num);

    else if(aux==2){
        max=sizeof(vet)/sizeof(int);
        pesquisar_bin(vet[max],0,max,num);
    }
}

//ORDENACAO POR SELECAO

void selecao(){

    int i=1;

    do{
        b=0;

        for (a=0;a<9;a++){
            if(vet[a+1]<vet[a]){
                aux=vet[a];
                vet[a]=vet[a+1];
                vet[a+1]=aux;
                b=1;
            }
        }

        if(b==1){
            printf("Fase %d: [",i);
            for(a=0;a<10;a++){
                printf("%d ",vet[a]);
            }
            printf("]\n\n");

            i++;
        }

    }while(b==1);
}


//PESQUISA BINARIA

int pesquisar_bin(int vetor[],int ini,int fim,int numero){

    int pos;

    pos=((fim-ini)/2)+ini;

    if (ini>fim){
        printf("Valor %d nao encontrado no vetor\n",numero);
        return 0;
    }

    if(numero>vet[pos]){
        return (pesquisar_bin(vet,pos+1,fim,numero));   
    }
    else if(numero<vet[pos]){
        return (pesquisar_bin(vet,ini,pos-1,numero));
    }
    else if(numero==vet[pos]){
        printf("O valor %d se encontra na posicao %d do vetor.",numero,pos);
        return 0;
    }
}

I've been doing some exercises of C but I really don't understand why dev C++ is returning this error.

I already tried to do many things like to change the reference including a point and other things.

C:\Users\ANONYM~1\AppData\Local\Temp\ccguUdp9.o ordenacao.cpp:(.text+0x128): undefined reference to `pesquisar_bin(int, int, int, int)'

\Mac\Home\Desktop\EXERCICIOS ED\collect2.exe [Error] ld returned 1 exit status


Solution

  • Your declaration and use of pesquisar_bin, does not match your implementation. As @JMichelB points out, vetor is declared to be an int and you pass it an int when calling pesquisar_bin, but your implementation defines vetor as an int[]. In the absence of an MCVE, that's the best we can surmise at this point in time.

    Your implementation of pesquisar_bin is ignoring the vetor parameter and using the vet variable from file scope instead. The code is a mess due to your thrashing about and not actually posting an MCVE. Pass vet to pesquisar_bin and change the implementation to use the vetor parameter.