Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void f(unsigned int n, double *array);
- /* Предполагается, что по указателю array выделена память под n элементов.
- * Ф-ия переставляет все четные элементы в началоб нечетные - в конец, без изменения порядка.
- * */
- void f(unsigned int n, double *array)
- {
- unsigned int i, j;
- double buffer;
- for( i = 0; i<(unsigned int)((n + 1)/2); i++ )// Выбор номера четного элемента
- {
- for( j = 0; j<i; j++ )
- {
- buffer = array[2*i - j];
- array[2*i - j] = array[2*i - 1 - j];
- array[2*i - 1 - j] = buffer;
- }
- }
- }
- int main(int argc, const char *argv[])
- {
- FILE *input_file, *output_file;
- unsigned int n, i;
- double *array;
- if( argc!=3 )
- {
- fprintf(stderr, "Usage: %s file_with_array file_for_result\n", argv[0]);
- return -1;
- }
- if( !(input_file = fopen(argv[1], "r")) )
- {
- fprintf(stderr, "Can not open file %s\n", argv[1]);
- return -1;
- }
- if( fscanf(input_file, "%u", &n)!=1 )
- {
- fprintf(stderr, "Can not read size of array from %s\n", argv[1]);
- 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 %s\n", argv[1]);
- fclose(input_file);
- return -1;
- }
- }
- // Массив считан
- fclose(input_file);
- f(n, array);
- if( !(output_file = fopen(argv[2], "w")) )
- {
- fprintf(stderr, "Can not open/create file %s\n", argv[2]);
- return -1;
- }
- fprintf(output_file, "%d\t", n);
- for( i = 0; i<n; i++ )
- {
- fprintf(output_file, "%lf\t", array[i]);
- }
- fclose(output_file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement