Advertisement
semenrbt

2.2 dinamic v12491414

Feb 19th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 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.     if(N < 0) return NULL;
  8.     int *mas = NULL;                      
  9.     if(max < min)
  10.     {
  11.         int s = min;
  12.         min = max;
  13.         max = s;
  14.     }
  15.     mas = (int*) malloc(sizeof(int)*N);
  16.     if(mas == NULL) return NULL;
  17.     for(int k = 0; k < N; k++)
  18.         mas[k] = rand()%(max - min + 1) + min;
  19.     return mas;
  20. }
  21.  
  22. int main()
  23. {
  24.   srand(time(NULL));
  25.   const int N = rand()%10 + 1;
  26.   const int M = rand()%10 + 1;
  27.   int * x = CreateArray(N, 0, 15);
  28.   int * y = CreateArray(M, 0, 15);  
  29.    if(x == NULL){
  30.         printf("Error.\n");
  31.         return 0;
  32.   }
  33.   if(y == NULL){
  34.         printf("Error.\n");
  35.         return 0;
  36.   }
  37.  
  38.   int s = 0, counter = 0;
  39.   int K = 0;
  40.   if(N <= M) K = N;
  41.   else K = M;
  42.  
  43.   int Z[K];
  44.     for(int i = 0; i < N; i++)
  45.     {
  46.       for(int j = 0; j < M; j++)
  47.       {
  48.         if(x[i] == y[j]) s++;
  49.       }
  50.       if(s == 1)
  51.       {
  52.         Z[counter] = x[i];
  53.         counter++;
  54.       }
  55.       if(s > 1)
  56.       {
  57.         printf("Elementi povtoryayutsya\n");
  58.         return 0;
  59.       }
  60.       s = 0;
  61.     }
  62.     printf("Z[%d] = {", counter);
  63.     for(int k = 0; k < counter; k++)
  64.       printf("%d, ", Z[k]);
  65.     printf("};\n");
  66.  
  67.   free(x);
  68.   free(y);
  69.  
  70.   return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement