Pushok

task5

Jun 17th, 2014
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. void main()
  2. {
  3.     FILE *f = fopen("file.txt", "r");
  4.     int s1, s2, s3;
  5.     int *a1, *a2, *a3;
  6.  
  7.     fscanf(f, "%d", &s1);
  8.     fscanf(f, "%d", &s2);
  9.     s3 = s1 + s2;
  10.  
  11.     a1 = (int*)malloc(sizeof(int)*s1);
  12.     a2 = (int*)malloc(sizeof(int)*s2);
  13.     a3 = (int*)malloc(sizeof(int)*s3);
  14.  
  15.     printf("array 1 = ");
  16.     for (int i = 0; i < s1; ++i)
  17.     {
  18.         fscanf(f, "%d", &a1[i]);
  19.         printf("%d ", a1[i]);
  20.     }
  21.     printf("\narray 2 = ");
  22.     for (int i = 0; i < s2; ++i)
  23.     {
  24.         fscanf(f, "%d", &a2[i]);
  25.         printf("%d ", a2[i]);
  26.     }
  27.  
  28.     int i = 0, j = 0, k = 0;
  29.     while(k < s3)
  30.     {
  31.         if(i >= s1)
  32.             a3[k++] = a2[j++];
  33.         else if(j >= s2)
  34.             a3[k++] = a1[i++];
  35.         else if (a1[i] >= a2[j])
  36.             a3[k++] = a2[j++];
  37.         else if (a1[i] < a2[j])
  38.             a3[k++] = a1[i++];
  39.     }
  40.    
  41.     printf("\n-----------\nresult = ");
  42.     for(int i = 0; i < s3; ++i)
  43.         printf("%d ", a3[i]);
  44.  
  45.     fclose(f);
  46.     free(a1);
  47.     free(a2);
  48.     free(a3);
  49.     getchar();
  50.     getchar();
  51. }
Advertisement
Add Comment
Please, Sign In to add comment