Advertisement
Alvetarn

[Vetores] Exercício 1

Sep 30th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. /* Faça um algoritmo para verificar se um número real lido pelo teclado encontra-se ou não em um vetor com 30 números reais
  2. (também lido pelo teclado). Utilize um procedimento para preencher o vetor e uma função para verificar se o número pertence
  3. ou não ao vetor. A impressão desta informação (se o número pertence ou não ao vetor) deve ser na função main. */
  4.  
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. #include<locale.h>
  8.  
  9. void preencher(int *vetor);
  10. int  verificar(int *vetor);
  11.  
  12. int main(){
  13.     setlocale(LC_ALL,"Portuguese");
  14.  
  15.     int vetor[30];
  16.  
  17.     preencher(vetor);
  18.     if(verificar(vetor)){
  19.         printf("\nO número foi encontrado no vetor.\n");
  20.     }else{
  21.         printf("\nO número NÃO foi encontrado no vetor.\n");
  22.     }
  23.  
  24.     return 0;
  25. }
  26.  
  27. void preencher(int *vetor){
  28.     int i;
  29.  
  30.     for(i=0; i<3; i++){
  31.         printf("Digite o número do %dº elemento do vetor: ",i+1);
  32.         scanf("%d",&vetor[i]);
  33.     }
  34. }
  35.  
  36. int verificar(int *vetor){
  37.     int i, verificador;
  38.  
  39.     printf("\nDigite um número para ser verificado dentro do vetor: ");
  40.     scanf("%d",&verificador);
  41.  
  42.     for(i=0; i<=3; i++){
  43.         if(vetor[i]==verificador){
  44.             return 1;
  45.         }else{
  46.             return 0;
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement