Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "f.h"
- /* Предполагается, что по указателю array выделена память под n элементов.
- * Ф-ия переставляет все четные элементы в началоб нечетные - в конец, без изменения порядка.
- * */
- int main(void)
- {
- FILE *input_file, *output_file;
- unsigned int n, i;
- double *array;
- if( !(input_file = fopen("input.txt", "r")) )
- {
- fprintf(stderr, "Can not open file input.txt\n");
- return -1;
- }
- if( fscanf(input_file, "%u", &n)!=1 )
- {
- fprintf(stderr, "Can not read size of array from input.txt\n");
- fclose(input_file);
- return -1;
- }
- if( !(array = (double *)malloc(n*sizeof(double))) )
- {
- fprintf(stderr, "Can not allocate memory!\n");
- fclose(input_file);
- return -1;
- }
- for( i = 0; i<n; i++ )
- {
- if( fscanf(input_file, "%lf", array + i)!=1 )
- {
- fprintf(stderr, "Can not read element from input.txt\n");
- fclose(input_file);
- free(array);
- return -1;
- }
- }
- // Массив считан
- fclose(input_file);
- for(i=0;i<n;i++)
- printf("array[%d] = %lf\n",i,array[i]);
- f(n, array);
- if( !(output_file = fopen("output.txt", "w")) )
- {
- fprintf(stderr, "Can not open/create file output.txt\n");
- free(array);
- return -1;
- }
- fprintf(output_file, "%d\t", n);
- for( i = 0; i<n; i++ )
- {
- fprintf(output_file, "%lf\t", array[i]);
- }
- fclose(output_file);
- free(array);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement