Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "vector_utils.h"
  4.  
  5. int read_vector(int *tab, int size, int stop_value)
  6. {
  7. int i=0;
  8. if(size<=0 || tab==NULL)
  9. {
  10. return -1;
  11. }
  12. while(i<size)
  13. {
  14. int a;
  15. int x;
  16. x=scanf("%d", &a);
  17. if(x<=0)
  18. {
  19. return -2;
  20. }
  21. if(a!=stop_value)
  22. {
  23. *(tab+i)=a;
  24. i++;
  25. }
  26. else
  27. {
  28. int c;
  29. while ((c = getchar()) != '\n' && c != EOF) { }
  30. return i;
  31. }
  32. }
  33. int c;
  34. while ((c = getchar()) != '\n' && c != EOF) { }
  35. return i;
  36. }
  37.  
  38.  
  39. void display_vector(const int *tab, int size)
  40. {
  41. int i=0;
  42. if(size<=0 || tab==NULL)
  43. {
  44. return;
  45. }
  46. for(;i<size;i++)
  47. {
  48. printf("%d ", *(tab+i));
  49. }
  50. printf("\n");
  51. }
  52.  
  53. static void swap(int* array, int a, int b)
  54. {
  55. int tmp = *(array + a);
  56. *(array + a) = *(array + b);
  57. *(array + b) = tmp;
  58. }
  59.  
  60. int shift(int *array, int array_size, int positions_to_shift, enum direction dir)
  61. {
  62. int i;
  63.  
  64. if (array == NULL || array_size <= 0) {
  65. return 1;
  66. }
  67.  
  68. for(i=0;i<positions_to_shift;i++)
  69. {
  70. int j;
  71. switch (dir) {
  72. case ROTATE_LEFT:
  73. for (j = 0; j < array_size - 1; ++j) {
  74. swap(array, j, j+1);
  75. }
  76. break;
  77.  
  78. case ROTATE_RIGHT:
  79. for (j = array_size - 1; j > 0; --j) {
  80. swap(array, j, j-1);
  81. }
  82. break;
  83. }
  84. }
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement