Vector in C- Program received signal, Segementation fault

0

Hello, I made a function that checks if a number is present in a vector. THE function receives as parameters the vector, the value to be verified and the number of 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 for. At the end of the processing the program should tell the user if the constant was found or not.

But after the user enters the number to be searched the compiler hangs. I used the Debug of the Error of Program received signal, Segementation fault. I believe that the error is in the call of the function but I tried in other ways and also it does not give. Help please!

#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");
}
    
asked by anonymous 16.11.2016 / 18:21

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 that 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.

    
16.11.2016 / 18:31
0

Let's go, friend, I've improved your code a little and it's working 100% now

#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 out of search yes yes answer if your vector has that value requested. I passed some parameters by reference to avoid some type of error (like numbers that were not inserted).

    
16.11.2016 / 19:07