Vector in C- Program Received Signal, Segementation fault

Asked

Viewed 54 times

0

Hello, I designed a function that checks whether a number is present in a vector. A function takes as parameters the vector, the value to be checked and the number of existing elements in it. E returns 1 (one) if the element exists in the vector or zero if it does not exist. Initialize the vector with the values you want and allow the user to enter, through the keyboard, the constant to be searched. At the end of the processing the program must communicate to the user whether the constant was found or not.

But, after the user enters the number to be searched the compiler hangs. I used Debug of Program Received error Signal, Segementation fault.. I believe the error is in the function call but I tried other ways and it also does not give. Please help!

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 100

int verifica_vetor(int vet[MAX],int n, int tam){//funcao verifica se o vetor contem o numero
int i;
 for ( i=0;i<=tam;i++){//laco percorre o vetor
    if (n==vet[i]){//verifica se o num digitado esta em alguma das posicoes do vetor
      printf("O valor foi encontrado: %d",vet[i]);
      return 1;
    }else{
      printf("\nO valor não foi encontrado.");
      return 0;
    }
}

}
    main() {
    int k,i,num, vetor[MAX];
    srand(time(NULL));

    printf("\n Informe o tamanho do vetor: ");
    scanf("%d",&k);

    for ( i = 0; i < k; i++)    {//preenche o vetor com numero aleatorios de 0 ao numero digitado
        vetor[i] = rand() % k + 1;

        printf("Vetor[%d] : %d" , i+1,  vetor[i]);//vai mostrando o veto
        printf("\n");

    }

    printf("\n Informe o numero que deseja procurar no vetor: ");
    scanf("%d",num);

    verifica_vetor(vetor,num, k);//chama a funcao de verificar

    system("pause");
}

2 answers

1

When I compile your code, it shows the following Warning:

warning C4700: uninitialized local variable 'num' used

When you are reading this value, you need to pass the address of this variable, not its value, to scanf:

scanf("%d", &num);

Note that your search logic (verifica_vetor) also does not work: it only looks at the first element of the vector.

  • our , I had not noticed this, that gafe : scanf("%d", &num); As for the (verifica_vector) because it only looks at the first element if I did this one that goes through the whole vector? for ( i=0;i<=tam;i++)

0


Come on friend, I improved a little its code and now it’s running 100%

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 100

int verifica_vetor(int *num, int vetor[MAX], int k){
    int i, aux = 0;
    for (i = 0; i < k; i++){
        if (*num == vetor[i]){
            aux++;
        }
    }

    if (aux != 0){
        printf("O numero existe no vetor!\n");
        return 0;
        }else{
            printf("O numero nao existe no vetor\n.");
            return 1;
        }
}

void main() {
    int k,i,num, vetor[MAX];

    srand(time(NULL));

    printf("\n Informe o tamanho do vetor: ");
    scanf("%d",&k);

    for ( i = 0; i < k; i++)    {//preenche o vetor com numero aleatorios de 0 ao numero digitado
        vetor[i] = rand() % k + 1;

        printf("Vetor[%d] : %d" , i+1,  vetor[i]);//vai mostrando o veto
        printf("\n");

    }

    printf("\n Informe o numero que deseja procurar no vetor: ");
    scanf("%d",&num);

    verifica_vetor(&num, vetor, k);//chama a funcao de verificar

    system("pause");
}

All I did was add a new logic for it to check all the numbers inside the vector first and outside the search ai yes answer if your vector has that requested value. I passed some parameters by reference to avoid some kind of error (such as numbers that were not entered).

  • Thank you! You saved me

  • Leave that up in the answer to who it is I came to see this problem already see the answer that helped you. ;D

Browser other questions tagged

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