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");
}
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++)
– ADR