Guest User

Untitled

a guest
Jan 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. /* Exercício de Computação - Inteiros com Vetores */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "vetor.h"
  5.  
  6. int main ()
  7. {
  8.  
  9. int *n1 = NULL;
  10. int *n2 = NULL;
  11.  
  12. int n1_length;
  13. int n2_length;
  14.  
  15. n1_length = inicializa_vetor(n1);
  16. n2_length = inicializa_vetor(n2);
  17.  
  18. imprime_vetor(n1, n1_length);
  19. imprime_vetor(n2, n2_length);
  20.  
  21. free(n1);
  22. free(n2);
  23. }
  24.  
  25. int inicializa_vetor (int *vetor)
  26. /* Função para inicializar um vetor. Vai preenchendo com os números da entrada padrão.
  27. ** São aceitos apenas números entre 0 e 9, além de que um -1 indica o fim da leitura.
  28. ** O valor retornado é o tamanho do vetor alocado */
  29. {
  30. int a = 0;
  31. int i = 0;
  32.  
  33. while (a != -1) {
  34. scanf("%d", &a);
  35. if (0 <= a && a < 10) {
  36. vetor = realloc(vetor, i+1);
  37. vetor[i] = a;
  38. i++;
  39. }
  40. /* Esta mensagem de erro não faz sentido se supormos que a entrada não terá números "indesejados". */
  41. /* else if (buffer != -1) {
  42. print("São aceitos apenas números entre 0 e 9. -1 indica o fim da leitura");
  43. }*/
  44. }
  45. return i;
  46. }
  47.  
  48. void imprime_vetor (int *vetor, int v_length)
  49. /* Função para imprimir um vetor.*/
  50. {
  51. int i;
  52.  
  53. for (i = 0; i < v_length; i++)
  54. printf("%d",vetor[i]);
  55. }
Add Comment
Please, Sign In to add comment