#include #include void merge(double *a, int sizeA, double *b, int sizeB, double *c); int main(void) { FILE *INA, *INB, *OUT; double x, *A, *B, *C; int NA = 0, NB = 0, NC, i; /* Count numbers in ina.txt */ INA = fopen("ina.txt", "r"); if (INA == NULL) { printf("File ina.txt not opened\n"); return -1; } while (fscanf(INA, "%lf", &x) == 1) ++NA; fclose(INA); if (NA <= 0) { printf("File ina.txt empty\n"); return -1; } /* Allocate memory for array A */ A = (double*)malloc(NA * sizeof(A[0])); if (A == NULL) { printf("Memory not allocated\n"); return -1; } /* Read array A from ina.txt */ INA = fopen("ina.txt", "r"); if (INA == NULL) { printf("File ina.txt not opened\n"); return -1; } for (i = 0; i < NA; i++) { if (fscanf(INA, "%lf", &A[i]) != 1) { printf("Not enough numbers\n"); return -1; } } fclose(INA); /* Count numbers in inb.txt */ INB = fopen("inb.txt", "r"); if (INB == NULL) { printf("File ina.txt not opened\n"); return -1; } while (fscanf(INB, "%lf", &x) == 1) ++NB; fclose(INB); if (NA <= 0) { printf("File ina.txt empty\n"); return -1; } /* Allocate memory for array A */ B = (double*)malloc(NB * sizeof(B[0])); if (B == NULL) { printf("Memory not allocated\n"); return -1; } /* Read array A from ina.txt */ INB = fopen("inb.txt", "r"); if (INB == NULL) { printf("File inb.txt not opened\n"); return -1; } for (i = 0; i < NB; i++) { if (fscanf(INB, "%lf", &B[i]) != 1) { printf("Not enough numbers\n"); return -1; } } fclose(INB); NC = NA + NB; C = (double*)malloc(NC * sizeof(C[0])); if (C == NULL) { free(A); free(B); printf("Memory not allocated\n"); return -1; } /* Create file output.txt */ OUT = fopen("output.txt", "w"); if (OUT == NULL) { free(A); free(B); printf("File output.txt not opened\n"); return -1; } /* Merge arrays A and B to array C */ merge(A, NA, B, NB, C); /* Output array C to output.txt */ for (i = 0; i < NC; i++) { fprintf(OUT, "%lf ", C[i]); } fclose(OUT); free(A); free(B); free(C); return 0; } void merge(double *a, int sizeA, double *b, int sizeB, double *c) { int sizeC = sizeA + sizeB; int i = 0, j = 0, k; for (k = 0; k < sizeC; k++) { if ((i < sizeA) && ((j >= sizeB) || (a[i] < b[j]))) { c[k] = a[i++]; } else { c[k] = b[j++]; } } }