Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "vector_utils.h"
- int read_vector(int *tab, int size, int stop_value)
- {
- int i=0;
- if(size<=0 || tab==NULL)
- {
- return -1;
- }
- while(i<size)
- {
- int a;
- int x;
- x=scanf("%d", &a);
- if(x<=0)
- {
- return -2;
- }
- if(a!=stop_value)
- {
- *(tab+i)=a;
- i++;
- }
- else
- {
- int c;
- while ((c = getchar()) != '\n' && c != EOF) { }
- return i;
- }
- }
- int c;
- while ((c = getchar()) != '\n' && c != EOF) { }
- return i;
- }
- void display_vector(const int *tab, int size)
- {
- int i=0;
- if(size<=0 || tab==NULL)
- {
- return;
- }
- for(;i<size;i++)
- {
- printf("%d ", *(tab+i));
- }
- printf("\n");
- }
- static void swap(int* array, int a, int b)
- {
- int tmp = *(array + a);
- *(array + a) = *(array + b);
- *(array + b) = tmp;
- }
- int shift(int *array, int array_size, int positions_to_shift, enum direction dir)
- {
- int i;
- if (array == NULL || array_size <= 0) {
- return 1;
- }
- for(i=0;i<positions_to_shift;i++)
- {
- int j;
- switch (dir) {
- case ROTATE_LEFT:
- for (j = 0; j < array_size - 1; ++j) {
- swap(array, j, j+1);
- }
- break;
- case ROTATE_RIGHT:
- for (j = array_size - 1; j > 0; --j) {
- swap(array, j, j-1);
- }
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement