Advertisement
ricardorichsn

exer_i.c

Sep 2nd, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int pesqSeq(int elem, int vetor[], int tam);
  5. void bubblesort(int vetor[], int tam);
  6. void printVetor(int vetor[], int tam);
  7.  
  8. int main() {
  9.    
  10.     int out, x, vetor[] = {54, 43, 47, 59, 11};
  11.    
  12.     bubblesort(vetor, 5);
  13.     printVetor(vetor, 5);
  14.    
  15.     printf("\nDigite o valor a ser procurado: ");
  16.     scanf("%d", &x);
  17.    
  18.     out = pesqSeq(x, vetor, 5);
  19.     if(out < 0)
  20.         printf("\nValor não encontrado");
  21.     else
  22.         printf("\n>> Posição: %d", out);
  23.    
  24.     return 0;
  25. }
  26.  
  27. int pesqSeq(int elem, int vetor[], int tam) {
  28.    
  29.     int i = 0, k = -1; //k: Posição no vetor do elemento a ser encontrado
  30.    
  31.     while(i < tam && k == -1)
  32.         if(elem == vetor[i])
  33.             k = i;
  34.         else
  35.             if(vetor[i] < elem)
  36.                 i += 1;
  37.             else
  38.                 k = -2;
  39.    
  40.     return(k);
  41. }
  42.  
  43. void bubblesort(int vetor[], int tam) {
  44.    
  45.     int i, j, tmp;
  46.    
  47.     for(i = 0; i < tam; i++)
  48.         for(j = 0; j < (tam-i-1); j++)
  49.             if(vetor[j] > vetor[j+1]) {
  50.                 tmp = vetor[j];
  51.                 vetor[j] = vetor[j+1];
  52.                 vetor[j+1] = tmp;
  53.             }
  54.    
  55. }
  56.  
  57. void printVetor(int vetor[], int tam) {
  58.     int i;
  59.    
  60.     printf("\n>> ");
  61.     for (i = 0; i < tam; i++){
  62.         printf("%d\t", vetor[i]);
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement