Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "f.h"
  4.  
  5. /* Предполагается, что по указателю array выделена память под n элементов.
  6.  * Ф-ия переставляет все четные элементы в началоб нечетные - в конец, без изменения порядка.
  7.  * */
  8.  
  9. int main(void)
  10. {
  11.     FILE *input_file, *output_file;
  12.     unsigned int n, i;
  13.     double *array;
  14.  
  15.     if( !(input_file = fopen("input.txt", "r")) )
  16.     {
  17.         fprintf(stderr, "Can not open file input.txt\n");
  18.         return -1;
  19.     }
  20.  
  21.     if( fscanf(input_file, "%u", &n)!=1 )
  22.     {
  23.         fprintf(stderr, "Can not read size of array from input.txt\n");
  24.         fclose(input_file);
  25.         return -1;
  26.     }
  27.  
  28.     if( !(array = (double *)malloc(n*sizeof(double))) )
  29.     {
  30.         fprintf(stderr, "Can not allocate memory!\n");
  31.         fclose(input_file);
  32.         return -1;
  33.     }
  34.  
  35.     for( i = 0; i<n; i++ )
  36.     {
  37.         if( fscanf(input_file, "%lf", array + i)!=1 )
  38.         {
  39.             fprintf(stderr, "Can not read element from input.txt\n");
  40.             fclose(input_file);
  41.             free(array);
  42.             return -1;
  43.         }
  44.     }
  45.     // Массив считан
  46.  
  47.     fclose(input_file);
  48.     for(i=0;i<n;i++)
  49.         printf("array[%d] = %lf\n",i,array[i]);
  50.  
  51.     f(n, array);
  52.  
  53.     if( !(output_file = fopen("output.txt", "w")) )
  54.     {
  55.         fprintf(stderr, "Can not open/create file output.txt\n");
  56.         free(array);
  57.         return -1;
  58.     }
  59.  
  60.     fprintf(output_file, "%d\t", n);
  61.     for( i = 0; i<n; i++ )
  62.     {
  63.         fprintf(output_file, "%lf\t", array[i]);
  64.     }
  65.  
  66.     fclose(output_file);
  67.     free(array);
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement