Error in binary vector search

Asked

Viewed 55 times

0

Hello,

I’m having problems in an exercise where I want to pursue a certain age in an ordered vector. The error you are giving is "Expression: result_pointer != nullptr"

I did a search, and I saw that it is a pointer error, however, I am not using pointer in variable X, and even when I use, it still gives problem. Could be some library error?

Follow the code below:

#include <stdio.h>

struct dados
{
    int idade;
    int peso;
};
typedef struct dados Dados;

int compara(Dados d1, Dados d2) {
    int dif;
    dif = d1.idade - d2.idade;
    if (dif != 0)
        return dif;
    return d1.peso - d2.peso;
}

void ordenaDados(Dados *v, int n) {
    int a, b;
    Dados temp, pivo;
    if (n > 1) {
        a = 1; b = n - 1; pivo = v[0];
        do
        {
            while (a < n && compara(v[a], pivo) <= 0) a++;
            while (compara(pivo, v[b]) < 0) b--;
            if (a < b)
            {
                temp = v[a];
                v[a] = v[b];
                v[b] = temp;
                a++; b--;
            }
        } while (a <= b);
        v[0] = v[b];
        v[b] = pivo;
        ordenaDados(v, b);
        ordenaDados(&v[a], n - a);
    }
}

int buscaPesoMenor(float *v, int n, int el) {
    int ini, fim, meio;
    ini = 0; fim = n - 1;
    while (ini <= fim)
    {
        meio = (ini + fim) / 2;
        if (el < v[meio])
            fim = meio - 1;
        else if (el > v[meio])
            ini = meio + 1;
        else
            return meio;
    }
    return -1;
}

int main(void) {
    Dados v[] = { { 25 , 50 }, { 20 , 30 }, { 30 , 40 }, { 20 , 65 }, { 20 , 40 }, { 18 , 60 }, { 30 , 45 }, { 18 , 65 } };
    int i, x, k;
    x = 0;
    printf("Dados iniciais:\n\n");
    for (i = 0; i < 8; i++)
        printf("Idade:%d Peso:%d\n", v[i].idade, v[i].peso);
    ordenaDados(v, 8);
    printf("\nDados ordenados:\n\n");
    for (i = 0; i < 8; i++)
        printf("Idade:%d Peso:%d\n", v[i].idade, v[i].peso);

    printf("Digite a idade que voce quer procurar");
    scanf("%d", x);
    k = buscaPesoMenor(v, 8, x);
    if (k == -1)
        printf("A idade procurada nao foi encontrada");
    else
        printf("A posicao eh %d\n", k);
        printf("Referente a Idade:%d Peso:%d\n", v[k].idade, v[k].peso);

    return 0;
}

1 answer

1

Hello, your error is in scanf, the correct is to pass the address of x and not just x, because for being a function, to change the value of x it needs to be passed by reference.

the correct then is:

scanf("%d", &x);

I also took the liberty of correcting your binary search. In the arguments, v is not float but its Data struct and the v[middle] access to the search must actually access v[middle]. age.

int buscaPesoMenor(Dados v[], int n, int el) {
    int ini, fim, meio;
    ini = 0; fim = n - 1;
    while (ini <= fim)
    {
        meio = (ini + fim) / 2;
        if (el < v[meio].idade)
            fim = meio - 1;
        else if (el > v[meio].idade)
            ini = meio + 1;
        else
            return meio;
    }
    return -1;
}
  • Hello, I was able to understand the mistakes! Thank you so much for your help! I just copied the search function from another exercise and did not pay attention, my mistake! Thanks!

  • Hello friend, for nothing! Remember if your problem has been solved by my reply, please accept it and mark that it was useful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.