Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void Invert(double *a, unsigned int n);
- void ShiftLeft(double *a, unsigned int n);
- int RemoveNegative(double *a, unsigned int n);
- void ShiftLeftForK(double *a, unsigned int n, unsigned int k);
- void printf_array(double *a, unsigned int n);
- void printf_array(double *a, unsigned int n)
- {
- unsigned int i;
- for(i = 0; i<n; i++)
- printf("a[%u]=%lf\t", i, a[i]);
- printf("\n");
- }
- // Сдвинуть массив на k позиций влево
- void ShiftLeftForK(double *a, unsigned int n, unsigned int k)
- {
- unsigned int i0, i, j, cnt = 0;
- double c;
- for(i0 = 0; cnt<n; i0++)
- {
- c = a[i0];
- for(i = i0, j = (i + k)%n; j!=i0; i = j, j = (j + k)%n)
- {
- a[i] = a[j];
- cnt++;
- }
- a[i] = c;
- cnt++;
- }
- }
- // Все не отрицательные группируются в начале, возвращается их количество
- int RemoveNegative(double *a, unsigned int n)
- {
- unsigned int i, j;
- i = 0;
- for(j = 0; j<n; j++)
- {
- if(a[j]>=0)
- {
- a[i] = a[j];
- i++;
- }
- }
- return (i - 1);
- }
- // Расположить элементы массива в обратном порядке
- /* Пример использования:
- * Invert(a,n);
- * Invert(a, n - k);
- * Invert(a + n - k, k);
- * Здесь реализована перестановка подмассива, стоящего в начала, и его дополнения.
- * */
- void Invert(double *a, unsigned int n)
- {
- double c;
- unsigned int i;
- if(n<2) return;
- for(i = 0; i<n/2; i++)
- {
- c = a[i];
- a[i] = a[n - i - 1];
- a[n - i - 1] = c;
- }
- }
- // Циклический сдвиг
- void ShiftLeft(double *a, unsigned int n)
- {
- double c;
- unsigned int i;
- if(n<2) return;
- c = a[0];
- for(i = 1; i<n; i++)
- {
- a[i-1] = a[i];
- }
- a[n-1] = c;
- }
- int main(void)
- {
- FILE *input_file;
- double *a;
- unsigned int i,n,k;
- if((input_file = fopen("input", "r"))==NULL)
- {
- printf("Open file error!\n");
- return 1;
- }
- if(fscanf(input_file, "%d", &n)<=0)// Первое считываемое число - кол-во элементов в массиве
- {
- printf("Input from file error!\n");
- return 2;
- }
- if(n==0)
- {
- printf("Result: no elements!\n");
- return 0;
- }
- a = (double *)malloc(n*sizeof(double));
- for(i = 0; i<n; i++)
- {
- if(fscanf(input_file, "%lf", &a[i])<=0)
- {
- printf("Input from file error!\n");
- free(a);
- return 2;
- }
- }
- printf_array(a,n);
- printf("k=");
- scanf("%u", &k);
- ShiftLeftForK(a,n,k);
- printf_array(a,n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement