Advertisement
semenrbt

2.2dinamic

Feb 18th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5.  
  6. int *CreateArray(int N, int min, int max){
  7.     int *mas = NULL;                      
  8.     if(max < min)
  9.     {
  10.         int s = min;
  11.         min = max;
  12.         max = s;
  13.     }
  14.     mas = (int*) malloc(sizeof(int)*N);
  15.     if(mas == NULL) return NULL;
  16.     for(int k = 0; k < N; k++)
  17.         mas[k] = rand()%(max - min + 1) + min;
  18.     return mas;
  19. }
  20.  
  21. int main()
  22. {
  23.   srand(time(NULL));
  24.   const int N = rand()%10 + 1;
  25.   const int M = rand()%10 + 1;
  26.   int * x = CreateArray(N, 0, 15);
  27.   int * y = CreateArray(M, 0, 15);  
  28.    if(x == NULL){
  29.         printf("Error.\n");
  30.         return 0;
  31.   }
  32.   if(y == NULL){
  33.         printf("Error.\n");
  34.         return 0;
  35.   }
  36.  
  37.   int s = 0, counter = 0;
  38.   if(N <= M)
  39.   {
  40.     int Z[N];
  41.     for(int i = 0; i < N; i++)
  42.     {
  43.       for(int j = 0; j < M; j++)
  44.       {
  45.         if(x[i] == y[j]) s++;
  46.       }
  47.       if(s == 1)
  48.       {
  49.         Z[counter] = x[i];
  50.         counter++;
  51.       }
  52.       if(s > 1)
  53.       {
  54.         printf("Elementi povtoryayutsya\n");
  55.         return 0;
  56.       }
  57.       s = 0;
  58.     }
  59.     printf("x[%d] = {", N);
  60.     for(int k = 0; k < N; k++)
  61.       printf("%d, ", x[k]);
  62.     printf("};\n");
  63.  
  64.     printf("y[%d] = {", M);
  65.     for(int k = 0; k < M; k++)
  66.       printf("%d, ", y[k]);
  67.     printf("};\n");
  68.  
  69.     printf("Z[%d] = {", counter);
  70.     for(int k = 0; k < counter; k++)
  71.       printf("%d, ", Z[k]);
  72.     printf("};\n");
  73.   }
  74.   else
  75.   {
  76.     int Z[M];
  77.     for(int i = 0; i < N; i++)
  78.     {
  79.       for(int j = 0; j < M; j++)
  80.       {
  81.         if(x[i] == y[j]) s++;
  82.       }
  83.       if(s == 1)
  84.       {
  85.         Z[counter] = x[i];
  86.         counter++;
  87.       }
  88.       if(s > 1)
  89.       {
  90.         printf("Elementi povtoryayutsya\n");
  91.         return 0;
  92.       }
  93.       s = 0;
  94.     }
  95.     printf("x[%d] = {", N);
  96.     for(int k = 0; k < N; k++)
  97.       printf("%d, ", x[k]);
  98.     printf("};\n");
  99.  
  100.     printf("y[%d] = {", M);
  101.     for(int k = 0; k < M; k++)
  102.       printf("%d, ", y[k]);
  103.     printf("};\n");
  104.  
  105.     printf("Z[%d] = {", counter);
  106.     for(int k = 0; k < counter; k++)
  107.       printf("%d, ", Z[k]);
  108.     printf("};\n");
  109.   }
  110.  
  111.   free(x);
  112.   free(y);
  113.  
  114.   return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement